-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbtc.go
169 lines (138 loc) · 3.49 KB
/
btc.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ipldbtc
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
cid "github.com/ipfs/go-cid"
node "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
)
type Block struct {
rawdata []byte
Version uint32 `json:"version"`
Parent cid.Cid `json:"parent"`
MerkleRoot cid.Cid `json:"tx"`
Timestamp uint32 `json:"timestamp"`
Difficulty uint32 `json:"difficulty"`
Nonce uint32 `json:"nonce"`
}
type Link struct {
Target cid.Cid
}
// assert that Block matches the Node interface for ipld
var _ node.Node = (*Block)(nil)
func (b *Block) Cid() cid.Cid {
h, _ := mh.Sum(b.header(), mh.DBL_SHA2_256, -1)
return cid.NewCidV1(cid.BitcoinBlock, h)
}
func (b *Block) RawData() []byte {
return b.header()
}
func (b *Block) Links() []*node.Link {
return []*node.Link{
{
Name: "tx",
Cid: b.MerkleRoot,
},
{
Name: "parent",
Cid: b.Parent,
},
}
}
func (b *Block) Loggable() map[string]interface{} {
// TODO: more helpful info here
return map[string]interface{}{
"type": "bitcoin_block",
}
}
// Resolve attempts to traverse a path through this block.
func (b *Block) Resolve(path []string) (interface{}, []string, error) {
if len(path) == 0 {
return nil, nil, fmt.Errorf("zero length path")
}
switch path[0] {
case "version":
return b.Version, path[1:], nil
case "timestamp":
return b.Timestamp, path[1:], nil
case "difficulty":
return b.Difficulty, path[1:], nil
case "nonce":
return b.Nonce, path[1:], nil
case "parent":
return &node.Link{Cid: b.Parent}, path[1:], nil
case "tx":
return &node.Link{Cid: b.MerkleRoot}, path[1:], nil
default:
return nil, nil, fmt.Errorf("no such link")
}
}
// ResolveLink is a helper function that allows easier traversal of links through blocks
func (b *Block) ResolveLink(path []string) (*node.Link, []string, error) {
out, rest, err := b.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := out.(*node.Link)
if !ok {
return nil, nil, fmt.Errorf("object at path was not a link")
}
return lnk, rest, nil
}
func cidToHash(c cid.Cid) []byte {
h := []byte(c.Hash())
return h[len(h)-32:]
}
func hashToCid(hv []byte, t uint64) cid.Cid {
h, _ := mh.Encode(hv, mh.DBL_SHA2_256)
return cid.NewCidV1(t, h)
}
// header generates a serialized block header for this block
func (b *Block) header() []byte {
buf := new(bytes.Buffer)
i := make([]byte, 4)
binary.LittleEndian.PutUint32(i, b.Version)
buf.Write(i)
buf.Write(cidToHash(b.Parent))
buf.Write(cidToHash(b.MerkleRoot))
binary.LittleEndian.PutUint32(i, b.Timestamp)
buf.Write(i)
binary.LittleEndian.PutUint32(i, b.Difficulty)
buf.Write(i)
binary.LittleEndian.PutUint32(i, b.Nonce)
buf.Write(i)
return buf.Bytes()
}
func (b *Block) Size() (uint64, error) {
return uint64(len(b.rawdata)), nil
}
func (b *Block) Stat() (*node.NodeStat, error) {
return &node.NodeStat{}, nil
}
func (b *Block) String() string {
return "[bitcoin block]"
}
func (b *Block) Tree(p string, depth int) []string {
// TODO: this isnt a correct implementation yet
return []string{"difficulty", "nonce", "version", "timestamp", "tx", "parent"}
}
func (b *Block) BTCSha() []byte {
blkmh, _ := mh.Sum(b.header(), mh.DBL_SHA2_256, -1)
return blkmh[2:]
}
func (b *Block) HexHash() string {
return hex.EncodeToString(revString(b.BTCSha()))
}
func (b *Block) Copy() node.Node {
nb := *b // cheating shallow copy
return &nb
}
func revString(s []byte) []byte {
b := make([]byte, len(s))
for i, v := range []byte(s) {
b[len(b)-(i+1)] = v
}
return b
}