2022-04-17 11:33:01 +03:00
|
|
|
package bitcoin
|
|
|
|
|
|
|
|
// TODO: coinbase transaction
|
|
|
|
|
|
|
|
// https://learnmeabitcoin.com/technical/blkdat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2022-04-17 11:33:01 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
|
|
|
)
|
|
|
|
|
|
|
|
var bitcoinScriptFormat decode.Group
|
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2022-04-17 11:33:01 +03:00
|
|
|
Name: format.BITCOIN_TRANSACTION,
|
|
|
|
Description: "Bitcoin transaction",
|
|
|
|
Dependencies: []decode.Dependency{
|
|
|
|
{Names: []string{format.BITCOIN_SCRIPT}, Group: &bitcoinScriptFormat},
|
|
|
|
},
|
|
|
|
DecodeFn: decodeBitcoinTranscation,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix with fd, and the next 2 bytes is the VarInt (in little-endian).
|
|
|
|
// Prefix with fe, and the next 4 bytes is the VarInt (in little-endian).
|
|
|
|
// Prefix with ff, and the next 8 bytes is the VarInt (in little-endian).
|
|
|
|
func decodeVarInt(d *decode.D) uint64 {
|
|
|
|
n := d.U8()
|
|
|
|
switch n {
|
|
|
|
case 0xfd:
|
|
|
|
return d.U16()
|
|
|
|
case 0xfe:
|
|
|
|
return d.U32()
|
|
|
|
case 0xff:
|
|
|
|
return d.U64()
|
|
|
|
default:
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all zero
|
|
|
|
var txIDCoinbaseBytes = [32]byte{}
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
func decodeBitcoinTranscation(d *decode.D) any {
|
2022-04-17 11:33:01 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
|
|
|
d.FieldU32("version")
|
|
|
|
witness := false
|
2023-03-10 03:14:53 +03:00
|
|
|
if d.PeekUintBits(8) == 0 {
|
2022-04-17 11:33:01 +03:00
|
|
|
witness = true
|
|
|
|
d.FieldU8("marker")
|
|
|
|
d.FieldU8("flag")
|
|
|
|
}
|
2022-09-30 14:58:23 +03:00
|
|
|
inputCount := d.FieldUintFn("input_count", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
d.FieldArray("inputs", func(d *decode.D) {
|
|
|
|
for i := uint64(0); i < inputCount; i++ {
|
|
|
|
d.FieldStruct("input", func(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldRawLen("txid", 32*8, scalar.RawBytesMap{
|
|
|
|
{Bytes: txIDCoinbaseBytes[:], Scalar: scalar.BitBuf{Description: "coinbase"}},
|
2022-04-17 11:33:01 +03:00
|
|
|
}, rawHexReverse)
|
|
|
|
d.FieldU32("vout")
|
2022-09-30 14:58:23 +03:00
|
|
|
scriptSigSize := d.FieldUintFn("scriptsig_size", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
d.FieldFormatOrRawLen("scriptsig", int64(scriptSigSize)*8, bitcoinScriptFormat, nil)
|
|
|
|
// TODO: better way to know if there should be a valid script
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldU32("sequence", scalar.UintHex)
|
2022-04-17 11:33:01 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-09-30 14:58:23 +03:00
|
|
|
outputCount := d.FieldUintFn("output_count", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
d.FieldArray("outputs", func(d *decode.D) {
|
|
|
|
for i := uint64(0); i < outputCount; i++ {
|
|
|
|
d.FieldStruct("output", func(d *decode.D) {
|
|
|
|
d.FieldU64("value")
|
2022-09-30 14:58:23 +03:00
|
|
|
scriptSigSize := d.FieldUintFn("scriptpub_size", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
// TODO: better way to know if there should be a valid script
|
|
|
|
d.FieldFormatOrRawLen("scriptpub", int64(scriptSigSize)*8, bitcoinScriptFormat, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if witness {
|
|
|
|
d.FieldArray("witnesses", func(d *decode.D) {
|
|
|
|
for i := uint64(0); i < inputCount; i++ {
|
|
|
|
d.FieldStruct("witness", func(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
witnessSize := d.FieldUintFn("witness_size", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
d.FieldArray("items", func(d *decode.D) {
|
|
|
|
for j := uint64(0); j < witnessSize; j++ {
|
2022-09-30 14:58:23 +03:00
|
|
|
itemSize := d.FieldUintFn("item_size", decodeVarInt)
|
2022-04-17 11:33:01 +03:00
|
|
|
d.FieldRawLen("item", int64(itemSize)*8)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
d.FieldU32("locktime")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|