-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
124 lines (104 loc) · 3.59 KB
/
client.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
package libbtc
import (
"context"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/renproject/libbtc-go/clients"
"github.com/renproject/libbtc-go/errors"
)
type Client interface {
clients.ClientCore
// Balance of the given address on Bitcoin blockchain.
Balance(ctx context.Context, address string, confirmations int64) (int64, error)
// FormatTransactionView formats the message and txhash into a user friendly
// message.
FormatTransactionView(msg, txhash string) string
// SerializePublicKey serializes the given public key.
SerializePublicKey(pubKey *btcec.PublicKey) ([]byte, error)
// PublicKeyToAddress converts the public key to a bitcoin address.
PublicKeyToAddress(pubKeyBytes []byte) (btcutil.Address, error)
// SlaveAddress creates an a deterministic unique address that can be spent
// by the private key correspndong to the given master public key hash
SlaveAddress(mpkh, nonce []byte) (btcutil.Address, error)
// SlaveScript creates a deterministic unique script that can be spent by
// the private key correspndong to the given master public key hash
SlaveScript(mpkh, nonce []byte) ([]byte, error)
// UTXOCount returns the number of utxos that can be spent.
UTXOCount(ctx context.Context, address string, confirmations int64) (int, error)
// Validate returns whether an address is valid or not
Validate(address string) error
}
type client struct {
clients.ClientCore
}
func (client *client) Balance(ctx context.Context, address string, confirmations int64) (int64, error) {
utxos, err := client.GetUTXOs(ctx, address, 999999, confirmations)
if err != nil {
return 0, err
}
var balance int64
for _, utxo := range utxos {
balance = balance + utxo.Amount
}
return balance, nil
}
func (client *client) UTXOCount(ctx context.Context, address string, confirmations int64) (int, error) {
utxos, err := client.GetUTXOs(ctx, address, 999999, confirmations)
if err != nil {
return 0, err
}
return len(utxos), nil
}
func (client *client) FormatTransactionView(msg, txhash string) string {
switch client.NetworkParams().Name {
case "mainnet":
return fmt.Sprintf("%s, transaction can be viewed at https://live.blockcypher.com/btc/tx/%s", msg, txhash)
case "testnet3":
return fmt.Sprintf("%s, transaction can be viewed at https://live.blockcypher.com/btc-testnet/tx/%s", msg, txhash)
default:
return ""
}
}
func (client *client) SerializePublicKey(pubKey *btcec.PublicKey) ([]byte, error) {
net := client.NetworkParams()
switch net {
case &chaincfg.MainNetParams:
return pubKey.SerializeCompressed(), nil
case &chaincfg.TestNet3Params:
return pubKey.SerializeUncompressed(), nil
default:
return nil, errors.NewErrUnsupportedNetwork(net.Name)
}
}
func (client *client) PublicKeyToAddress(pubKeyBytes []byte) (btcutil.Address, error) {
net := client.NetworkParams()
pubKey, err := btcutil.NewAddressPubKey(pubKeyBytes, net)
if err != nil {
return nil, err
}
addrString := pubKey.EncodeAddress()
return btcutil.DecodeAddress(addrString, net)
}
func NewBlockchainInfoClient(network string) (Client, error) {
core, err := clients.NewBlockchainInfoClientCore(network)
if err != nil {
return nil, err
}
return &client{core}, nil
}
func NewBitcoinFNClient(host, user, password string) (Client, error) {
core, err := clients.NewBitcoinFNClientCore(host, user, password)
if err != nil {
return nil, err
}
return &client{core}, nil
}
func NewMercuryClient(network string) (Client, error) {
core, err := clients.NewMercuryClientCore(network)
if err != nil {
return nil, err
}
return &client{core}, nil
}