-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtx_tree.go
99 lines (78 loc) · 1.86 KB
/
tx_tree.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
98
99
package ipldbtc
import (
"encoding/json"
"fmt"
cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
)
type TxTree struct {
Left *node.Link
Right *node.Link
}
func (t *TxTree) BTCSha() []byte {
return cidToHash(t.Cid())
}
func (t *TxTree) Cid() cid.Cid {
h, _ := mh.Sum(t.RawData(), mh.DBL_SHA2_256, -1)
return cid.NewCidV1(cid.BitcoinTx, h)
}
func (t *TxTree) Links() []*node.Link {
return []*node.Link{t.Left, t.Right}
}
func (t *TxTree) RawData() []byte {
out := make([]byte, 64)
lbytes := cidToHash(t.Left.Cid)
copy(out[:32], lbytes)
rbytes := cidToHash(t.Right.Cid)
copy(out[32:], rbytes)
return out
}
func (t *TxTree) Loggable() map[string]interface{} {
return map[string]interface{}{
"type": "bitcoin_tx_tree",
}
}
func (t *TxTree) Resolve(path []string) (interface{}, []string, error) {
if len(path) == 0 {
return nil, nil, fmt.Errorf("zero length path")
}
switch path[0] {
case "0":
return t.Left, path[1:], nil
case "1":
return t.Right, path[1:], nil
default:
return nil, nil, fmt.Errorf("no such link")
}
}
func (t *TxTree) MarshalJSON() ([]byte, error) {
return json.Marshal([]*Link{{t.Left.Cid}, {t.Right.Cid}})
}
func (t *TxTree) Copy() node.Node {
nt := *t
return &nt
}
func (t *TxTree) ResolveLink(path []string) (*node.Link, []string, error) {
out, rest, err := t.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := out.(*node.Link)
if ok {
return lnk, rest, nil
}
return nil, nil, fmt.Errorf("path did not lead to link")
}
func (t *TxTree) Size() (uint64, error) {
return uint64(len(t.RawData())), nil
}
func (t *TxTree) Stat() (*node.NodeStat, error) {
return &node.NodeStat{}, nil
}
func (t *TxTree) String() string {
return "[bitcoin transaction tree]"
}
func (t *TxTree) Tree(p string, depth int) []string {
return []string{"0", "1"}
}