-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
39 lines (34 loc) · 880 Bytes
/
decode.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
package main
import (
"bytes"
"encoding/hex"
"github.com/btcsuite/btcd/wire"
)
func DecodeBlockHeaderResp(resp *BlockHeaderResp) (*wire.BlockHeader, error) {
raw, err := hex.DecodeString(resp.Result)
if err != nil {
return nil, err
}
buff := bytes.NewBuffer(raw)
blockHeader := wire.BlockHeader{}
if err := blockHeader.Deserialize(buff); err != nil {
return nil, err
}
return &blockHeader, nil
}
func DecodeBlockHeadersResp(resp *BlockHeadersResp) ([]*wire.BlockHeader, error) {
raw, err := hex.DecodeString(resp.Result.Hex)
if err != nil {
return nil, err
}
buff := bytes.NewBuffer(raw)
blockHeaders := make([]*wire.BlockHeader, 0)
for buff.Len() != 0 {
blockHeader := wire.BlockHeader{}
if err := blockHeader.Deserialize(buff); err != nil {
return nil, err
}
blockHeaders = append(blockHeaders, &blockHeader)
}
return blockHeaders, nil
}