-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathweb.go
200 lines (167 loc) · 4.49 KB
/
web.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package btc
import (
"encoding/hex"
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/shopspring/decimal"
)
type Web struct {
client *Client
defaultNet *chaincfg.Params
}
func NewWeb(host, user, pass string, network string) (*Web, error) {
client, err := NewClient(host, user, pass)
if err != nil {
return nil, err
}
defaultNet := getNetwork(network)
return &Web{
client: client,
defaultNet: defaultNet,
}, nil
}
// CreateWallet 创建地址
func (w *Web) CreateWallet() (*Wallet, error) {
return CreateWallet(w.defaultNet)
}
// GetBlockCount 获取最高区块数量
func (w *Web) GetBlockCount() (int64, error) {
return w.client.GetBlockCount()
}
// GetWalletByPrivateKey 根据私钥获取地址
func (w *Web) GetWalletByPrivateKey(privateKeyStr string) (string, error) {
btcW, err := GetWalletByPrivateKey(w.defaultNet, privateKeyStr)
if err != nil {
return "", err
}
return btcW.NestedSegWitAddress, nil
}
// GetBalance 获取余额
func (w *Web) GetBalance(address string) (int64, error) {
fromAddress, err := btcutil.DecodeAddress(address, w.defaultNet)
if err != nil {
return 0, err
}
balance := decimal.NewFromInt(0)
listUnSpent, err := w.client.ListUnspent(fromAddress)
if err != nil {
fmt.Println("报错")
return 0, err
}
powDecimal := decimal.NewFromInt(1e8)
for _, result := range listUnSpent {
if result.Amount == 0 {
continue
}
resultAmount := decimal.NewFromFloat(result.Amount)
balance.Add(resultAmount.Mul(powDecimal))
}
return balance.IntPart(), nil
}
// Transfer 转账
func (w *Web) Transfer(from, to, privateKey string, amount, fee int64) (string, error) {
fromAddress, err := btcutil.DecodeAddress(from, w.defaultNet)
if err != nil {
return "", err
}
toAddress, err := btcutil.DecodeAddress(to, w.defaultNet)
if err != nil {
return "", err
}
// 记录累加金额
outAmount := decimal.NewFromInt(0)
listUnSpent, err := w.client.ListUnspent(fromAddress)
if err != nil {
return "", err
}
amountDecimal := decimal.NewFromInt(amount)
feeDecimal := decimal.NewFromInt(fee)
powDecimal := decimal.NewFromInt(1e8)
// 构造输出
var outputs []*wire.TxOut
// 构造输入
var inputs []*wire.TxIn
var pkScripts [][]byte // txin 签名用script
for _, result := range listUnSpent {
if result.Amount == 0 {
fmt.Println("sssssss")
continue
}
if outAmount.Cmp(amountDecimal) >= 0 {
fmt.Println("sssssss")
// 已经累加到需要转账的金额
break
}
resultAmount := decimal.NewFromFloat(result.Amount)
outAmount.Add(resultAmount.Mul(powDecimal))
fmt.Println("余额:", outAmount)
// 构造输入
hash, _ := chainhash.NewHashFromStr(result.TxID) // tx hash
outPoint := wire.NewOutPoint(hash, result.Vout) // 第几个输出
txIn := wire.NewTxIn(outPoint, nil, nil)
inputs = append(inputs, txIn)
//设置签名用script
txInPkScript, err := hex.DecodeString(result.ScriptPubKey)
if err != nil {
return "", err
}
pkScripts = append(pkScripts, txInPkScript)
}
fmt.Println(outAmount, amountDecimal)
// 余额不足
if outAmount.Cmp(amountDecimal) == -1 {
return "", errors.New("余额不足")
}
// 输出给转账者自己
leftToMe := outAmount.Sub(amountDecimal).Sub(feeDecimal) // 累加值-转账值-交易费就是剩下再给我的
pkScript, err := txscript.PayToAddrScript(fromAddress)
if err != nil {
return "", err
}
outputs = append(outputs, wire.NewTxOut(leftToMe.IntPart(), pkScript))
// 输出给接收者
pkScript, err = txscript.PayToAddrScript(toAddress)
if err != nil {
return "", err
}
outputs = append(outputs, wire.NewTxOut(amount, pkScript))
tx := &wire.MsgTx{
Version: wire.TxVersion,
TxIn: inputs,
TxOut: outputs,
LockTime: 0,
}
// 签名
err = sign(tx, privateKey, pkScripts)
if err != nil {
return "", err
}
// 广播裸交易
hash, err := w.client.SendRawTransaction(tx)
if err != nil {
return "", err
}
return hash.String(), nil
}
// 签名
func sign(tx *wire.MsgTx, privKeyStr string, prevPkScripts [][]byte) error {
inputs := tx.TxIn
wif, err := btcutil.DecodeWIF(privKeyStr)
if err != nil {
return err
}
privateKey := wif.PrivKey
for i := range inputs {
pkScript := prevPkScripts[i]
var script []byte
script, err = txscript.SignatureScript(tx, i, pkScript, txscript.SigHashAll,
privateKey, false)
inputs[i].SignatureScript = script
}
return nil
}