-
Notifications
You must be signed in to change notification settings - Fork 10
/
blockheader.go
183 lines (157 loc) · 5.39 KB
/
blockheader.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package bc
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"math/big"
"github.com/libsv/go-bk/crypto"
"github.com/libsv/go-bt/v2"
)
/*
Field Purpose Size (Bytes)
----------------------------------------------------------------------------------------------------
Version Block version number 4
hashPrevBlock 256-bit hash of the previous block header 32
hashMerkleRoot 256-bit hash based on all of the transactions in the block 32
Time Current block timestamp as seconds since 1970-01-01T00:00 UTC 4
Bits Current target in compact format 4
Nonce 32-bit number (starts at 0) 4
*/
// A BlockHeader in the Bitcoin blockchain.
type BlockHeader struct {
Version uint32 `json:"version"`
Time uint32 `json:"time"`
Nonce uint32 `json:"nonce"`
HashPrevBlock []byte `json:"hashPrevBlock"`
HashMerkleRoot []byte `json:"merkleRoot"`
Bits []byte `json:"bits"`
}
type bhJSON struct {
Version uint32 `json:"version"`
Time uint32 `json:"time"`
Nonce uint32 `json:"nonce"`
HashPrevBlock string `json:"hashPrevBlock"`
HashMerkleRoot string `json:"merkleRoot"`
Bits string `json:"bits"`
}
// HashPrevBlockStr returns the Block Header encoded as hex string.
func (bh *BlockHeader) HashPrevBlockStr() string {
return hex.EncodeToString(bh.HashPrevBlock)
}
// HashMerkleRootStr returns the Block Header encoded as hex string.
func (bh *BlockHeader) HashMerkleRootStr() string {
return hex.EncodeToString(bh.HashMerkleRoot)
}
// BitsStr returns the Block Header encoded as hex string.
func (bh *BlockHeader) BitsStr() string {
return hex.EncodeToString(bh.Bits)
}
// String returns the Block Header encoded as hex string.
func (bh *BlockHeader) String() string {
return hex.EncodeToString(bh.Bytes())
}
// Bytes will decode a bitcoin block header struct
// into a byte slice.
//
// See https://en.bitcoin.it/wiki/Block_hashing_algorithm
func (bh *BlockHeader) Bytes() []byte {
bytes := []byte{}
bytes = append(bytes, UInt32ToBytes(bh.Version)...)
bytes = append(bytes, bt.ReverseBytes(bh.HashPrevBlock)...)
bytes = append(bytes, bt.ReverseBytes(bh.HashMerkleRoot)...)
bytes = append(bytes, UInt32ToBytes(bh.Time)...)
bytes = append(bytes, bt.ReverseBytes(bh.Bits)...)
bytes = append(bytes, UInt32ToBytes(bh.Nonce)...)
return bytes
}
// Valid checks whether a blockheader satisfies the proof-of-work claimed
// in Bits. Wwe check whether its Hash256 read as a little endian number
// is less than the Bits written in expanded form.
func (bh *BlockHeader) Valid() bool {
target, err := ExpandTargetFromAsInt(hex.EncodeToString(bh.Bits))
if err != nil {
return false
}
digest := bt.ReverseBytes(crypto.Sha256d(bh.Bytes()))
var bn = big.NewInt(0)
bn.SetBytes(digest)
return bn.Cmp(target) < 0
}
// NewBlockHeaderFromStr will encode a block header hash
// into the bitcoin block header structure.
//
// See https://en.bitcoin.it/wiki/Block_hashing_algorithm
func NewBlockHeaderFromStr(headerStr string) (*BlockHeader, error) {
if len(headerStr) != 160 {
return nil, errors.New("block header should be 80 bytes long")
}
headerBytes, err := hex.DecodeString(headerStr)
if err != nil {
return nil, err
}
return NewBlockHeaderFromBytes(headerBytes)
}
// NewBlockHeaderFromBytes will encode a block header byte slice
// into the bitcoin block header structure.
//
// See https://en.bitcoin.it/wiki/Block_hashing_algorithm
func NewBlockHeaderFromBytes(headerBytes []byte) (*BlockHeader, error) {
if len(headerBytes) != 80 {
return nil, errors.New("block header should be 80 bytes long")
}
return &BlockHeader{
Version: binary.LittleEndian.Uint32(headerBytes[:4]),
HashPrevBlock: bt.ReverseBytes(headerBytes[4:36]),
HashMerkleRoot: bt.ReverseBytes(headerBytes[36:68]),
Time: binary.LittleEndian.Uint32(headerBytes[68:72]),
Bits: bt.ReverseBytes(headerBytes[72:76]),
Nonce: binary.LittleEndian.Uint32(headerBytes[76:]),
}, nil
}
// ExtractMerkleRootFromBlockHeader will take an 80 byte Bitcoin block
// header hex string and return the Merkle Root from it.
func ExtractMerkleRootFromBlockHeader(header string) (string, error) {
bh, err := NewBlockHeaderFromStr(header)
if err != nil {
return "", err
}
return hex.EncodeToString(bh.HashMerkleRoot), nil
}
// MarshalJSON marshals the receiving bc.BlockHeader into a JSON []byte.
func (bh *BlockHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(bhJSON{
Version: bh.Version,
Time: bh.Time,
Nonce: bh.Nonce,
Bits: bh.BitsStr(),
HashMerkleRoot: bh.HashMerkleRootStr(),
HashPrevBlock: bh.HashPrevBlockStr(),
})
}
// UnmarshalJSON unmarshals a JSON []byte into the receiving bc.BlockHeader.
func (bh *BlockHeader) UnmarshalJSON(b []byte) error {
var bhj bhJSON
if err := json.Unmarshal(b, &bhj); err != nil {
return err
}
bh.Version = bhj.Version
bh.Nonce = bhj.Nonce
bh.Time = bhj.Time
bits, err := hex.DecodeString(bhj.Bits)
if err != nil {
return err
}
bh.Bits = bits
hashPrevBlock, err := hex.DecodeString(bhj.HashPrevBlock)
if err != nil {
return err
}
bh.HashPrevBlock = hashPrevBlock
hashMerkleRoot, err := hex.DecodeString(bhj.HashMerkleRoot)
if err != nil {
return err
}
bh.HashMerkleRoot = hashMerkleRoot
return nil
}