-
Notifications
You must be signed in to change notification settings - Fork 10
/
merkleproof.go
97 lines (79 loc) · 2.15 KB
/
merkleproof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package bc
import (
"encoding/hex"
"github.com/libsv/go-bt/v2"
)
// A MerkleProof is a structure that proves inclusion of a
// Bitcoin transaction in a block.
type MerkleProof struct {
Index uint64 `json:"index"`
TxOrID string `json:"txOrId"`
Target string `json:"target"`
Nodes []string `json:"nodes"`
TargetType string `json:"targetType,omitempty"`
ProofType string `json:"proofType,omitempty"`
Composite bool `json:"composite,omitempty"`
}
// Bytes converts the JSON Merkle Proof
// into byte encoding.
//
// Check the following encoding:
//
// flags: byte,
// index: varint,
// txLength: varint, //omitted if flag bit 0 == 0 as it's a fixed length transaction ID
// txOrId: byte[32 or variable length],
// target: byte[32 or 80], //determined by flag bits 1 and 2
// nodeCount: varint,
// nodes: node[]
func (mp *MerkleProof) Bytes() ([]byte, error) {
index := bt.VarInt(mp.Index)
txOrID, err := hex.DecodeString(mp.TxOrID)
if err != nil {
return nil, err
}
txOrID = bt.ReverseBytes(txOrID)
target, err := hex.DecodeString(mp.Target)
if err != nil {
return nil, err
}
target = bt.ReverseBytes(target)
nodeCount := len(mp.Nodes)
nodes := []byte{}
for _, n := range mp.Nodes {
if n == "*" {
nodes = append(nodes, []byte{1}...)
continue
}
nodes = append(nodes, []byte{0}...)
nb, err := hex.DecodeString(n)
if err != nil {
return nil, err
}
nodes = append(nodes, bt.ReverseBytes(nb)...)
}
var flags uint8
var txLength []byte
if len(mp.TxOrID) > 64 { // tx bytes instead of txid
// set bit at index 0
flags |= 1 << 0
txLength = bt.VarInt(uint64(len(txOrID))).Bytes()
}
if mp.TargetType == "header" {
// set bit at index 1
flags |= 1 << 1
} else if mp.TargetType == "merkleRoot" {
// set bit at index 2
flags |= 1 << 2
}
// ignore proofType and compositeType for this version
bytes := []byte{}
bytes = append(bytes, flags)
bytes = append(bytes, index.Bytes()...)
bytes = append(bytes, txLength...)
bytes = append(bytes, txOrID...)
bytes = append(bytes, target...)
bytes = append(bytes, byte(nodeCount))
bytes = append(bytes, nodes...)
return bytes, nil
}