mirror of
https://github.com/wader/fq.git
synced 2024-12-18 02:41:44 +03:00
1ddea1ada3
Move registry to interp and add support for functions and filesystems. This will be used later for allow formats to add own functions and fq code. Add gojqextra function helpers to have more comfortable API to add functions. Takes care of argument type casting and JQValue:s and some more things. Refactor interp package to use new function helper and registry. Probably fixes a bunch of JQValue bugs and other type errors. Refactor out some mpeg nal things to mpeg format. Refactor interp jq code into display.q and init.jq. Remove undocumented aes_ctr funciton, was a test. Hopefully will add more crypto things laster.
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package bitcoin
|
|
|
|
// TODO: coinbase transaction
|
|
|
|
// https://learnmeabitcoin.com/technical/blkdat
|
|
|
|
import (
|
|
"github.com/wader/fq/format"
|
|
"github.com/wader/fq/pkg/decode"
|
|
"github.com/wader/fq/pkg/interp"
|
|
"github.com/wader/fq/pkg/scalar"
|
|
)
|
|
|
|
var bitcoinScriptFormat decode.Group
|
|
|
|
func init() {
|
|
interp.RegisterFormat(decode.Format{
|
|
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{}
|
|
|
|
func decodeBitcoinTranscation(d *decode.D, in interface{}) interface{} {
|
|
d.Endian = decode.LittleEndian
|
|
|
|
d.FieldU32("version")
|
|
witness := false
|
|
if d.PeekBits(8) == 0 {
|
|
witness = true
|
|
d.FieldU8("marker")
|
|
d.FieldU8("flag")
|
|
}
|
|
inputCount := d.FieldUFn("input_count", decodeVarInt)
|
|
d.FieldArray("inputs", func(d *decode.D) {
|
|
for i := uint64(0); i < inputCount; i++ {
|
|
d.FieldStruct("input", func(d *decode.D) {
|
|
d.FieldRawLen("txid", 32*8, scalar.BytesToScalar{
|
|
{Bytes: txIDCoinbaseBytes[:], Scalar: scalar.S{Description: "coinbase"}},
|
|
}, rawHexReverse)
|
|
d.FieldU32("vout")
|
|
scriptSigSize := d.FieldUFn("scriptsig_size", decodeVarInt)
|
|
d.FieldFormatOrRawLen("scriptsig", int64(scriptSigSize)*8, bitcoinScriptFormat, nil)
|
|
// TODO: better way to know if there should be a valid script
|
|
d.FieldU32("sequence", scalar.ActualHex)
|
|
})
|
|
}
|
|
})
|
|
outputCount := d.FieldUFn("output_count", decodeVarInt)
|
|
d.FieldArray("outputs", func(d *decode.D) {
|
|
for i := uint64(0); i < outputCount; i++ {
|
|
d.FieldStruct("output", func(d *decode.D) {
|
|
d.FieldU64("value")
|
|
scriptSigSize := d.FieldUFn("scriptpub_size", decodeVarInt)
|
|
// 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) {
|
|
witnessSize := d.FieldUFn("witness_size", decodeVarInt)
|
|
d.FieldArray("items", func(d *decode.D) {
|
|
for j := uint64(0); j < witnessSize; j++ {
|
|
itemSize := d.FieldUFn("item_size", decodeVarInt)
|
|
d.FieldRawLen("item", int64(itemSize)*8)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
})
|
|
}
|
|
d.FieldU32("locktime")
|
|
|
|
return nil
|
|
}
|