forked from 33cn/plugin
/
btcweb.go
147 lines (131 loc) · 3.19 KB
/
btcweb.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package relayd
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/common/merkle"
ty "github.com/33cn/plugin/plugin/dapp/relay/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/valyala/fasthttp"
)
type btcWeb struct {
urlRoot string
httpClient *fasthttp.Client
}
func newBtcWeb() (BtcClient, error) {
b := &btcWeb{
urlRoot: "https://blockchain.info",
httpClient: &fasthttp.Client{TLSConfig: &tls.Config{InsecureSkipVerify: true}},
}
return b, nil
}
func (b *btcWeb) Start() error {
return nil
}
func (b *btcWeb) Stop() error {
return nil
}
func (b *btcWeb) GetBlockHeader(height uint64) (*ty.BtcHeader, error) {
block, err := b.getBlock(height)
if err != nil {
return nil, err
}
return block.BtcHeader(), nil
}
func (b *btcWeb) getBlock(height uint64) (*block, error) {
if height < 0 {
return nil, errors.New("height < 0")
}
url := fmt.Sprintf("%s/block-height/%d?format=json", b.urlRoot, height)
data, err := b.requestURL(url)
if err != nil {
return nil, err
}
var blocks = blocks{}
err = json.Unmarshal(data, &blocks)
if err != nil {
return nil, err
}
block := blocks.Blocks[0]
return &block, nil
}
func (b *btcWeb) GetLatestBlock() (*chainhash.Hash, uint64, error) {
url := b.urlRoot + "/latestblock"
data, err := b.requestURL(url)
if err != nil {
return nil, 0, err
}
var blocks = latestBlock{}
err = json.Unmarshal(data, &blocks)
if err != nil {
return nil, 0, err
}
hash, err := chainhash.NewHashFromStr(blocks.Hash)
if err != nil {
return nil, 0, err
}
return hash, blocks.Height, nil
}
func (b *btcWeb) Ping() {
hash, height, err := b.GetLatestBlock()
if err != nil {
log.Error("btcWeb ping", "error", err)
}
log.Info("btcWeb ping", "latest Hash: ", hash.String(), "latest height", height)
}
func (b *btcWeb) GetTransaction(hash string) (*ty.BtcTransaction, error) {
url := b.urlRoot + "/rawtx/" + hash
data, err := b.requestURL(url)
if err != nil {
return nil, err
}
var tx = transactionResult{}
err = json.Unmarshal(data, &tx)
if err != nil {
return nil, err
}
return tx.BtcTransaction(), nil
}
func (b *btcWeb) GetSPV(height uint64, txHash string) (*ty.BtcSpv, error) {
block, err := b.getBlock(height)
if err != nil {
return nil, err
}
var txIndex uint32
txs := make([][]byte, 0, len(block.Tx))
for index, tx := range block.Tx {
if txHash == tx.Hash {
txIndex = uint32(index)
}
hash, err := merkle.NewHashFromStr(tx.Hash)
if err != nil {
return nil, err
}
txs = append(txs, hash.CloneBytes())
}
proof := merkle.GetMerkleBranch(txs, txIndex)
spv := &ty.BtcSpv{
Hash: txHash,
Time: block.Time,
Height: block.Height,
BlockHash: block.Hash,
TxIndex: txIndex,
BranchProof: proof,
}
return spv, nil
}
func (b *btcWeb) requestURL(url string) ([]byte, error) {
status, body, err := b.httpClient.Get(nil, url)
if err != nil {
return nil, err
}
if status != fasthttp.StatusOK {
return nil, fmt.Errorf("%d", status)
}
return body, nil
}