-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_test.go
169 lines (136 loc) · 4.75 KB
/
btc_test.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
package zetaclient
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"testing"
"github.com/btcsuite/btcd/btcec"
"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/stretchr/testify/suite"
"github.com/zeta-chain/zetacore/common"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type BTCSignTestSuite struct {
suite.Suite
testSigner *TestSigner
db *gorm.DB
}
const (
prevOut = "07a84f4bd45a633e93871be5c98d958afd13a37f3cf5010f40eec0840d19f5fa"
// tb1q7r6lnqjhvdjuw9uf4ehx7fs0euc6cxnqz7jj50
pk = "cQkjdfeMU8vHvE6jErnFVqZYYZnGGYy64jH6zovbSXdfTjte6QgY"
utxoCount = 5
)
func (suite *BTCSignTestSuite) SetupTest() {
wif, _ := btcutil.DecodeWIF(pk)
privateKey := wif.PrivKey
//skHex := "7b8507ba117e069f4a3f456f505276084f8c92aee86ac78ae37b4d1801d35fa8"
//privateKey, err := crypto.HexToECDSA(skHex)
//pkBytes := crypto.FromECDSAPub(&privateKey.PublicKey)
//suite.T().Logf("pubkey: %d", len(pkBytes))
//suite.Require().NoError(err)
suite.testSigner = &TestSigner{ // fake TSS
PrivKey: privateKey.ToECDSA(),
}
addr := suite.testSigner.BTCAddressWitnessPubkeyHash()
suite.T().Logf("segwit addr: %s", addr)
db, err := gorm.Open(sqlite.Open(TempSQLiteDbPath), &gorm.Config{})
suite.NoError(err)
suite.db = db
}
func (suite *BTCSignTestSuite) TearDownSuite() {
}
func (suite *BTCSignTestSuite) TestSign() {
// build a tx used for both signatures
tx, txSigHashes, idx, amt, subscript, privKey, compress, err := buildTX()
suite.Require().NoError(err)
// sign tx using wallet signature
walletSignedTX, err := getWalletTX(tx, txSigHashes, idx, amt, subscript, txscript.SigHashAll, privKey, compress)
suite.Require().NoError(err)
suite.T().Logf("wallet signed tx : %v\n", walletSignedTX)
// sign tx using tss signature
tssSignedTX, err := getTSSTX(suite.testSigner, tx, txSigHashes, idx, amt, subscript, txscript.SigHashAll)
suite.Require().NoError(err)
suite.T().Logf("tss signed tx : %v\n", tssSignedTX)
}
func (suite *BTCSignTestSuite) TestSubmittedTx() {
}
func TestBTCSign(t *testing.T) {
suite.Run(t, new(BTCSignTestSuite))
}
func buildTX() (*wire.MsgTx, *txscript.TxSigHashes, int, int64, []byte, *btcec.PrivateKey, bool, error) {
wif, err := btcutil.DecodeWIF(pk)
if err != nil {
return nil, nil, 0, 0, nil, nil, false, err
}
fmt.Printf("is wif.netid testnet3 %v\n", wif.IsForNet(&chaincfg.TestNet3Params))
fmt.Printf("is wif pubkey compressed %v\n", wif.CompressPubKey)
addr, err := btcutil.NewAddressWitnessPubKeyHash(btcutil.Hash160(wif.SerializePubKey()), &chaincfg.TestNet3Params)
if err != nil {
return nil, nil, 0, 0, nil, nil, false, err
}
fmt.Printf("addr %v\n", addr.EncodeAddress())
hash, err := chainhash.NewHashFromStr(prevOut)
if err != nil {
return nil, nil, 0, 0, nil, nil, false, err
}
outpoint := wire.NewOutPoint(hash, 0)
// build tx
tx := wire.NewMsgTx(wire.TxVersion)
txIn := wire.NewTxIn(outpoint, nil, nil)
tx.AddTxIn(txIn)
pkScript, err := payToWitnessPubKeyHashScript(addr.WitnessProgram())
if err != nil {
return nil, nil, 0, 0, nil, nil, false, err
}
txOut := wire.NewTxOut(47000, pkScript)
tx.AddTxOut(txOut)
txSigHashes := txscript.NewTxSigHashes(tx)
privKey := btcec.PrivateKey(*wif.PrivKey.ToECDSA())
return tx, txSigHashes, int(0), int64(65236), pkScript, &privKey, wif.CompressPubKey, nil
}
func getWalletTX(tx *wire.MsgTx, sigHashes *txscript.TxSigHashes, idx int, amt int64, subscript []byte, hashType txscript.SigHashType, privKey *btcec.PrivateKey, compress bool) (string, error) {
txWitness, err := txscript.WitnessSignature(tx, sigHashes, idx, amt, subscript, hashType, privKey, compress)
if err != nil {
return "", err
}
tx.TxIn[0].Witness = txWitness
buf := new(bytes.Buffer)
if err := tx.Serialize(buf); err != nil {
return "", err
}
walletTx := hex.EncodeToString(buf.Bytes())
return walletTx, nil
}
func getTSSTX(tss *TestSigner, tx *wire.MsgTx, sigHashes *txscript.TxSigHashes, idx int, amt int64, subscript []byte, hashType txscript.SigHashType) (string, error) {
witnessHash, err := txscript.CalcWitnessSigHash(subscript, sigHashes, txscript.SigHashAll, tx, idx, amt)
if err != nil {
return "", err
}
sig65B, err := tss.Sign(witnessHash, 10, 10, &common.Chain{}, "")
R := big.NewInt(0).SetBytes(sig65B[:32])
S := big.NewInt(0).SetBytes(sig65B[32:64])
sig := btcec.Signature{
R: R,
S: S,
}
if err != nil {
return "", err
}
pkCompressed := tss.PubKeyCompressedBytes()
txWitness := wire.TxWitness{append(sig.Serialize(), byte(hashType)), pkCompressed}
tx.TxIn[0].Witness = txWitness
buf := new(bytes.Buffer)
err = tx.Serialize(buf)
if err != nil {
return "", err
}
tssTX := hex.EncodeToString(buf.Bytes())
return tssTX, nil
}