/
utils.go
327 lines (289 loc) · 9.36 KB
/
utils.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package btc
import (
"bytes"
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/deltaswapio/swaprouter/v3/common"
"github.com/deltaswapio/swaprouter/v3/tools/crypto"
)
// GetPayToAddrScript get pay to address script
func (b *Bridge) GetPayToAddrScript(address string) ([]byte, error) {
toAddr, err := b.DecodeAddress(address)
if err != nil {
return nil, fmt.Errorf("decode btc address '%v' failed. %w", address, err)
}
return txscript.PayToAddrScript(toAddr)
}
// DecodeAddress decode address
func (b *Bridge) DecodeAddress(addr string) (address btcutil.Address, err error) {
chainConfig := b.GetChainParams(b.ChainConfig.GetChainID())
address, err = btcutil.DecodeAddress(addr, chainConfig)
if err != nil {
return
}
if !address.IsForNet(chainConfig) {
err = fmt.Errorf("invalid address for net")
return
}
return
}
// NullDataScript encap
func (b *Bridge) NullDataScript(memo string) ([]byte, error) {
return txscript.NullDataScript([]byte(memo))
}
// NewTxOut new txout
func (b *Bridge) NewTxOut(amount int64, pkScript []byte) *wire.TxOut {
return wire.NewTxOut(amount, pkScript)
}
func (b *Bridge) FindUtxos(addr string) (result []*ElectUtxo, err error) {
urls := b.GatewayConfig.AllGatewayURLs
for _, url := range urls {
result, err = FindUtxos(url, addr)
if err == nil {
break
}
}
if err != nil {
return nil, err
}
return result, nil
}
// EstimateFeePerKb impl
func (b *Bridge) EstimateFeePerKb(blocks int) (result int64, err error) {
urls := b.GatewayConfig.AllGatewayURLs
for _, url := range urls {
result, err = EstimateFeePerKb(url, blocks)
if err == nil {
break
}
}
if err != nil {
return 0, err
}
return result, nil
}
// NewTxIn new txin
func (b *Bridge) NewTxIn(txid string, vout uint32, pkScript []byte) (*wire.TxIn, error) {
txHash, err := chainhash.NewHashFromStr(txid)
if err != nil {
return nil, err
}
prevOutPoint := wire.NewOutPoint(txHash, vout)
return wire.NewTxIn(prevOutPoint, pkScript, nil), nil
}
func isValidValue(value btcAmountType) bool {
return value > 0 && value <= btcutil.MaxSatoshi
}
// IsPayToScriptHash is p2sh
func (b *Bridge) IsPayToScriptHash(sigScript []byte) bool {
return txscript.IsPayToScriptHash(sigScript)
}
func (b *Bridge) getRedeemScriptByOutputScrpit(preScript []byte) ([]byte, error) {
pkScript, err := b.ParsePkScript(preScript)
if err != nil {
return nil, err
}
chainParams := b.GetChainParams(b.ChainConfig.GetChainID())
p2shAddress, err := pkScript.Address(chainParams)
if err != nil {
return nil, err
}
p2shAddr := p2shAddress.String()
bindAddr := GetP2shBindAddress(p2shAddr)
if bindAddr == "" {
return nil, fmt.Errorf("p2sh address %v is not registered", p2shAddr)
}
var address string
address, redeemScript, _ := b.GetP2shAddress(bindAddr)
if address != p2shAddr {
return nil, fmt.Errorf("p2sh address mismatch for bind address %v, have %v want %v", bindAddr, p2shAddr, address)
}
return redeemScript, nil
}
// ParsePkScript parse pkScript
func (b *Bridge) ParsePkScript(pkScript []byte) (txscript.PkScript, error) {
return txscript.ParsePkScript(pkScript)
}
// GetP2shBindAddress get p2sh bind address
func GetP2shBindAddress(p2shAddress string) (bindAddress string) {
return ""
}
func (b *Bridge) GetP2shAddress(bindAddr string) (p2shAddress string, redeemScript []byte, err error) {
if b.IsValidAddress(bindAddr) {
return "", nil, fmt.Errorf("invalid bind address %v", bindAddr)
}
memo := common.FromHex(bindAddr)
dcrmAddress := b.GetChainConfig().RouterContract // in btc routerMPC is routerContract
address, err := b.DecodeAddress(dcrmAddress)
if err != nil {
return "", nil, fmt.Errorf("invalid dcrm address %v, %w", dcrmAddress, err)
}
pubKeyHash := address.ScriptAddress()
return b.getP2shAddressWithMemo(memo, pubKeyHash)
}
func (b *Bridge) getP2shAddressWithMemo(memo, pubKeyHash []byte) (p2shAddress string, redeemScript []byte, err error) {
redeemScript, err = b.GetP2shRedeemScript(memo, pubKeyHash)
if err != nil {
return
}
addressScriptHash, err := b.NewAddressScriptHash(redeemScript)
if err != nil {
return
}
p2shAddress = addressScriptHash.EncodeAddress()
return
}
func (b *Bridge) GetP2shRedeemScript(memo, pubKeyHash []byte) (redeemScript []byte, err error) {
return txscript.NewScriptBuilder().
AddData(memo).AddOp(txscript.OP_DROP).
AddOp(txscript.OP_DUP).AddOp(txscript.OP_HASH160).AddData(pubKeyHash).
AddOp(txscript.OP_EQUALVERIFY).AddOp(txscript.OP_CHECKSIG).
Script()
}
// NewAddressScriptHash encap
func (b *Bridge) NewAddressScriptHash(redeemScript []byte) (*btcutil.AddressScriptHash, error) {
chainParams := b.GetChainParams(b.ChainConfig.GetChainID())
return btcutil.NewAddressScriptHash(redeemScript, chainParams)
}
// CalcSignatureHash calc sig hash
func (b *Bridge) CalcSignatureHash(sigScript []byte, tx *wire.MsgTx, i int) (sigHash []byte, err error) {
return txscript.CalcSignatureHash(sigScript, txscript.SigHashAll, tx, i)
}
func (b *Bridge) getSigDataFromRSV(rsv string) ([]byte, bool) {
rs := rsv[0 : len(rsv)-2]
r := rs[:64]
s := rs[64:]
rr, ok := new(big.Int).SetString(r, 16)
if !ok {
return nil, false
}
ss, ok := new(big.Int).SetString(s, 16)
if !ok {
return nil, false
}
return b.SerializeSignature(rr, ss), true
}
func (b *Bridge) SerializeSignature(r, s *big.Int) []byte {
sign := &btcec.Signature{R: r, S: s}
return append(sign.Serialize(), byte(txscript.SigHashAll))
}
// GetSigScript get script
func (b *Bridge) GetSigScript(sigScripts [][]byte, prevScript, signData, cPkData []byte, i int) (sigScript []byte, err error) {
scriptClass := txscript.GetScriptClass(prevScript)
switch scriptClass {
case txscript.PubKeyHashTy:
sigScript, err = txscript.NewScriptBuilder().AddData(signData).AddData(cPkData).Script()
case txscript.ScriptHashTy:
if sigScripts == nil {
err = fmt.Errorf("call MakeSignedTransaction spend p2sh without redeem scripts")
} else {
redeemScript := sigScripts[i]
err = b.VerifyRedeemScript(prevScript, redeemScript)
if err == nil {
sigScript, err = txscript.NewScriptBuilder().AddData(signData).AddData(cPkData).AddData(redeemScript).Script()
}
}
default:
err = fmt.Errorf("unsupport to spend '%v' output", scriptClass.String())
}
return sigScript, err
}
// VerifyRedeemScript verify redeem script
func (b *Bridge) VerifyRedeemScript(prevScript, redeemScript []byte) error {
p2shScript, err := b.GetP2shSigScript(redeemScript)
if err != nil {
return err
}
if !bytes.Equal(p2shScript, prevScript) {
return fmt.Errorf("redeem script %x mismatch", redeemScript)
}
return nil
}
// GetP2shSigScript get p2sh signature script
func (b *Bridge) GetP2shSigScript(redeemScript []byte) ([]byte, error) {
p2shAddr, err := b.GetP2shAddressByRedeemScript(redeemScript)
if err != nil {
return nil, err
}
return b.GetPayToAddrScript(p2shAddr)
}
// GetP2shAddressByRedeemScript get p2sh address by redeem script
func (b *Bridge) GetP2shAddressByRedeemScript(redeemScript []byte) (string, error) {
addressScriptHash, err := b.NewAddressScriptHash(redeemScript)
if err != nil {
return "", err
}
return addressScriptHash.EncodeAddress(), nil
}
// GetCompressedPublicKey get compressed public key
func (b *Bridge) GetCompressedPublicKey(fromPublicKey string, needVerify bool) (cPkData []byte, err error) {
if fromPublicKey == "" {
return nil, nil
}
pkData := common.FromHex(fromPublicKey)
cPkData, err = b.ToCompressedPublicKey(pkData)
if err != nil {
return nil, err
}
if needVerify {
err = b.verifyPublickeyData(cPkData)
if err != nil {
return nil, err
}
}
return cPkData, nil
}
// ToCompressedPublicKey convert to compressed public key if not
func (b *Bridge) ToCompressedPublicKey(pkData []byte) ([]byte, error) {
pubKey, err := btcec.ParsePubKey(pkData, btcec.S256())
if err != nil {
return nil, err
}
return pubKey.SerializeCompressed(), nil
}
func (b *Bridge) verifyPublickeyData(pkData []byte) error {
dcrmAddress := b.GetChainConfig().RouterContract // in btc routerMPC is routerContract
if dcrmAddress == "" {
return nil
}
address, err := b.NewAddressPubKeyHash(pkData)
if err != nil {
return err
}
if address.EncodeAddress() != dcrmAddress {
return fmt.Errorf("public key address %v is not the configed dcrm address %v", address, dcrmAddress)
}
return nil
}
// NewAddressPubKeyHash encap
func (b *Bridge) NewAddressPubKeyHash(pkData []byte) (*btcutil.AddressPubKeyHash, error) {
chainParams := b.GetChainParams(b.ChainConfig.GetChainID())
return btcutil.NewAddressPubKeyHash(btcutil.Hash160(pkData), chainParams)
}
// the rsv must have correct v (recovery id), otherwise will get wrong public key data.
func (b *Bridge) getPkDataFromSig(rsv, msgHash string, compressed bool) (pkData []byte, err error) {
rsvData := common.FromHex(rsv)
hashData := common.FromHex(msgHash)
ecPub, err := crypto.SigToPub(hashData, rsvData)
if err != nil {
return nil, err
}
return b.SerializePublicKey(ecPub, compressed), nil
}
// SerializePublicKey serialize ecdsa public key
func (b *Bridge) SerializePublicKey(ecPub *ecdsa.PublicKey, compressed bool) []byte {
if compressed {
return (*btcec.PublicKey)(ecPub).SerializeCompressed()
}
return (*btcec.PublicKey)(ecPub).SerializeUncompressed()
}
// DecodeWIF decode wif
func DecodeWIF(wif string) (*btcutil.WIF, error) {
return btcutil.DecodeWIF(wif)
}