-
Notifications
You must be signed in to change notification settings - Fork 6
/
signtx.go
311 lines (278 loc) · 8.4 KB
/
signtx.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
package btc
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/fsn-dev/crossChain-Bridge/common"
"github.com/fsn-dev/crossChain-Bridge/dcrm"
"github.com/fsn-dev/crossChain-Bridge/log"
"github.com/fsn-dev/crossChain-Bridge/tokens"
"github.com/fsn-dev/crossChain-Bridge/tools/crypto"
)
var (
retryCount = 15
retryInterval = 10 * time.Second
waitInterval = 10 * time.Second
hashType = txscript.SigHashAll
)
// DcrmSignTransaction dcrm sign raw tx
func (b *Bridge) DcrmSignTransaction(rawTx interface{}, args *tokens.BuildTxArgs) (signedTx interface{}, txHash string, err error) {
authoredTx, ok := rawTx.(*txauthor.AuthoredTx)
if !ok {
return nil, "", tokens.ErrWrongRawTx
}
var (
msgHashes []string
rsvs []string
sigScripts [][]byte
hasP2shInput bool
sigHash []byte
)
for i, preScript := range authoredTx.PrevScripts {
sigScript := preScript
if txscript.IsPayToScriptHash(preScript) {
sigScript, err = b.getRedeemScriptByOutputScrpit(preScript)
if err != nil {
return nil, "", err
}
hasP2shInput = true
}
sigHash, err = txscript.CalcSignatureHash(sigScript, hashType, authoredTx.Tx, i)
if err != nil {
return nil, "", err
}
msgHash := hex.EncodeToString(sigHash)
msgHashes = append(msgHashes, msgHash)
sigScripts = append(sigScripts, sigScript)
}
if !hasP2shInput {
sigScripts = nil
}
rsvs, err = b.DcrmSignMsgHash(msgHashes, args)
if err != nil {
return nil, "", err
}
return b.MakeSignedTransaction(authoredTx, msgHashes, rsvs, sigScripts, args)
}
func (b *Bridge) verifyPublickeyData(pkData []byte, swapType tokens.SwapType) error {
switch swapType {
case tokens.SwapinType:
return tokens.ErrSwapTypeNotSupported
case tokens.SwapoutType, tokens.SwapRecallType:
dcrmAddress := b.TokenConfig.DcrmAddress
address, _ := btcutil.NewAddressPubKeyHash(btcutil.Hash160(pkData), b.GetChainConfig())
if address.EncodeAddress() != b.TokenConfig.DcrmAddress {
return fmt.Errorf("sign public key %v is not the configed dcrm address %v", address, dcrmAddress)
}
}
return nil
}
// MakeSignedTransaction make signed tx
func (b *Bridge) MakeSignedTransaction(authoredTx *txauthor.AuthoredTx, msgHash, rsv []string, sigScripts [][]byte, args *tokens.BuildTxArgs) (signedTx interface{}, txHash string, err error) {
txIn := authoredTx.Tx.TxIn
if len(txIn) != len(msgHash) {
return nil, "", errors.New("mismatch number of msghashes and tx inputs")
}
if len(txIn) != len(rsv) {
return nil, "", errors.New("mismatch number of signatures and tx inputs")
}
if sigScripts != nil && len(sigScripts) != len(txIn) {
return nil, "", errors.New("mismatch number of signatures scripts and tx inputs")
}
log.Info(b.TokenConfig.BlockChain+" Bridge MakeSignedTransaction", "msghash", msgHash, "count", len(msgHash))
cPkData, err := b.getPkDataFronConfig(args)
if err != nil {
return nil, "", err
}
var sigScript []byte
for i, txin := range txIn {
signData, ok := getSigDataFromRSV(rsv[i])
if !ok {
return nil, "", errors.New("wrong RSV data")
}
if len(cPkData) == 0 {
cPkData, err = b.getPkDataFronSig(rsv[i], msgHash[i], args.SwapType)
if err != nil {
return nil, "", err
}
}
prevScript := authoredTx.PrevScripts[i]
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 {
sigScript, err = txscript.NewScriptBuilder().AddData(signData).AddData(cPkData).AddData(sigScripts[i]).Script()
}
default:
err = fmt.Errorf("unsupport to spend '%v' output", scriptClass.String())
}
if err != nil {
return nil, "", err
}
txin.SignatureScript = sigScript
}
txHash = authoredTx.Tx.TxHash().String()
log.Info(b.TokenConfig.BlockChain+" MakeSignedTransaction success", "txhash", txHash)
return authoredTx, txHash, nil
}
func 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
}
sign := &btcec.Signature{
R: rr,
S: ss,
}
signData := append(sign.Serialize(), byte(hashType))
return signData, true
}
func (b *Bridge) getPkDataFronConfig(args *tokens.BuildTxArgs) (cPkData []byte, err error) {
fromPublicKey := tokens.BtcFromPublicKey
if args != nil && args.Extra != nil {
extra := args.Extra.BtcExtra
if extra != nil && extra.FromPublicKey != nil {
fromPublicKey = *extra.FromPublicKey
}
}
if fromPublicKey == "" {
return nil, nil
}
cPkData = common.FromHex(fromPublicKey)
err = b.verifyPublickeyData(cPkData, args.SwapType)
if err != nil {
return nil, err
}
return cPkData, nil
}
func (b *Bridge) getPkDataFronSig(rsv, msgHash string, swapType tokens.SwapType) (cPkData []byte, err error) {
rsvData := common.FromHex(rsv)
hashData := common.FromHex(msgHash)
pkData, err := crypto.Ecrecover(hashData, rsvData)
if err != nil {
return nil, err
}
pk, err := btcec.ParsePubKey(pkData, btcec.S256())
if err != nil {
return nil, err
}
cPkData = pk.SerializeCompressed()
err = b.verifyPublickeyData(cPkData, swapType)
if err != nil {
return nil, err
}
return cPkData, nil
}
// DcrmSignMsgHash dcrm sign msg hash
func (b *Bridge) DcrmSignMsgHash(msgHash []string, args *tokens.BuildTxArgs) (rsv []string, err error) {
extra := args.Extra.BtcExtra
if extra == nil {
return nil, tokens.ErrWrongExtraArgs
}
jsondata, _ := json.Marshal(args)
msgContext := []string{string(jsondata)}
keyID, err := dcrm.DoSign(msgHash, msgContext)
if err != nil {
return nil, err
}
log.Info(b.TokenConfig.BlockChain+" DcrmSignTransaction start", "keyID", keyID, "msghash", msgHash, "txid", args.SwapID)
time.Sleep(waitInterval)
i := 0
for ; i < retryCount; i++ {
signStatus, err := dcrm.GetSignStatus(keyID)
if err == nil {
if len(signStatus.Rsv) != len(msgHash) {
return nil, fmt.Errorf("get sign status require %v rsv but have %v (keyID = %v)", len(msgHash), len(signStatus.Rsv), keyID)
}
rsv = signStatus.Rsv
break
}
switch err {
case dcrm.ErrGetSignStatusFailed, dcrm.ErrGetSignStatusTimeout:
return nil, err
}
log.Warn("retry get sign status as error", "err", err, "txid", args.SwapID)
time.Sleep(retryInterval)
}
if i == retryCount || len(rsv) == 0 {
return nil, errors.New("get sign status failed")
}
log.Trace(b.TokenConfig.BlockChain+" DcrmSignTransaction get rsv success", "keyID", keyID, "rsv", rsv)
return rsv, nil
}
// SignTransaction sign tx with wif
func (b *Bridge) SignTransaction(rawTx interface{}, wif string) (signedTx interface{}, txHash string, err error) {
authoredTx, ok := rawTx.(*txauthor.AuthoredTx)
if !ok {
return nil, "", tokens.ErrWrongRawTx
}
pkwif, err := btcutil.DecodeWIF(wif)
if err != nil {
return nil, "", err
}
privateKey := pkwif.PrivKey
var (
msgHashes []string
rsvs []string
sigScripts [][]byte
hasP2shInput bool
)
for i, preScript := range authoredTx.PrevScripts {
sigScript := preScript
if txscript.IsPayToScriptHash(preScript) {
sigScript, err = b.getRedeemScriptByOutputScrpit(preScript)
if err != nil {
return nil, "", err
}
hasP2shInput = true
}
sigHash, err := txscript.CalcSignatureHash(sigScript, hashType, authoredTx.Tx, i)
if err != nil {
return nil, "", err
}
msgHash := hex.EncodeToString(sigHash)
msgHashes = append(msgHashes, msgHash)
sigScripts = append(sigScripts, sigScript)
}
if !hasP2shInput {
sigScripts = nil
}
for _, msgHash := range msgHashes {
signature, err := privateKey.Sign(common.FromHex(msgHash))
if err != nil {
return nil, "", err
}
rr := fmt.Sprintf("%064X", signature.R)
ss := fmt.Sprintf("%064X", signature.S)
rsv := fmt.Sprintf("%s%s00", rr, ss)
rsvs = append(rsvs, rsv)
}
pk := (*btcec.PublicKey)(&privateKey.PublicKey).SerializeCompressed()
pubKey := hex.EncodeToString(pk)
args := &tokens.BuildTxArgs{
Extra: &tokens.AllExtras{
BtcExtra: &tokens.BtcExtraArgs{
FromPublicKey: &pubKey,
},
},
}
return b.MakeSignedTransaction(authoredTx, msgHashes, rsvs, sigScripts, args)
}