/
tx_decode.go
591 lines (479 loc) · 17.1 KB
/
tx_decode.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
* Copyright 2018 The OpenWallet Authors
* This file is part of the OpenWallet library.
*
* The OpenWallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The OpenWallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package whitecoin
import (
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/blocktree/openwallet/v2/log"
"github.com/blocktree/whitecoin-adapter/libs/operations"
"github.com/blocktree/whitecoin-adapter/libs/config"
"github.com/blocktree/whitecoin-adapter/libs/crypto"
bt "github.com/blocktree/whitecoin-adapter/libs/types"
"github.com/blocktree/whitecoin-adapter/types"
owcrypt "github.com/blocktree/go-owcrypt"
"github.com/blocktree/openwallet/v2/openwallet"
"github.com/shopspring/decimal"
)
// TransactionDecoder 交易单解析器
type TransactionDecoder struct {
openwallet.TransactionDecoderBase
wm *WalletManager //钱包管理者
}
//NewTransactionDecoder 交易单解析器
func NewTransactionDecoder(wm *WalletManager) *TransactionDecoder {
decoder := TransactionDecoder{}
decoder.wm = wm
return &decoder
}
//CreateRawTransaction 创建交易单
func (decoder *TransactionDecoder) CreateRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
var (
accountID = rawTx.Account.AccountID
amountStr string
to string
assetID types.ObjectID
precise uint64
)
if !rawTx.Coin.IsContract {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "only support contract")
}
assetID = types.MustParseObjectID(rawTx.Coin.Contract.Address)
precise = rawTx.Coin.Contract.Decimals
//获取wallet
addresses, err := wrapper.GetAddressList(0, -1, "AccountID", accountID) //wrapper.GetWallet().GetAddressesByAccount(rawTx.Account.AccountID)
if err != nil {
return err
}
if len(addresses) == 0 {
return openwallet.Errorf(openwallet.ErrAccountNotAddress, "[%s] have not addresses", accountID)
}
for k, v := range rawTx.To {
amountStr = v
to = k
break
}
for _, addr := range addresses {
balance, err := decoder.wm.Api.GetAddrBalance(addr.Address, assetID)
if err != nil {
return err
}
accountBalanceDec, _ := decimal.NewFromString(balance.Amount)
amountDec, _ := decimal.NewFromString(amountStr)
amountDec = amountDec.Shift(int32(precise))
//memo := rawTx.GetExtParam().Get("memo").String()
asset := bt.AssetIDFromObject(bt.NewAssetID(assetID.String()))
amount := bt.AssetAmount{
Asset: asset,
Amount: bt.Int64(amountDec.IntPart()),
}
fromAddr, _ := bt.NewAddressFromString(addr.Address)
toAddr, _ := bt.NewAddressFromString(to)
op := operations.TransferOperation{
Amount: amount,
Extensions: bt.Extensions{},
From: bt.AccountIDFromObject(bt.NewAccountID("1.2.0")),
To: bt.AccountIDFromObject(bt.NewAccountID("1.2.0")),
FromAddr: *fromAddr,
ToAddr: *toAddr,
}
ops := bt.Operations{&op}
fees, err := decoder.wm.GetRequiredFee(ops, assetID.String())
if err != nil {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "can't get fees")
}
feesDec := decimal.Zero
for _, fee := range fees {
feesDec = feesDec.Add(decimal.New(int64(fee.Amount), 0))
}
if accountBalanceDec.LessThan(amountDec.Add(feesDec)) {
continue
}
if err := ops.ApplyFees(fees); err != nil {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "ApplyFees")
}
//fromPublicKey, _ := bt.NewPublicKeyFromString(fromAccount.Options.MemoKey)
//toPublicKey, _ := bt.NewPublicKeyFromString(toAccount.Options.MemoKey)
//if memo != "" {
// m := bt.Memo{
// From: *fromPublicKey,
// To: *toPublicKey,
// Nonce: bt.UInt64(rand.Uint64()),
// }
// keyBag := crypto.NewKeyBag()
// keyBag.Add(decoder.wm.Config.MemoPrivateKey)
//
// if err := keyBag.EncryptMemo(&m, memo); err != nil {
// return fmt.Errorf("EncryptMemo: %v", err)
// }
//
// op.Memo = &m
//}
createTxErr := decoder.createRawTransaction(
wrapper,
rawTx,
&accountBalanceDec,
addr,
ops,
"")
if createTxErr != nil {
return createTxErr
}
return nil
}
return openwallet.Errorf(openwallet.ErrInsufficientBalanceOfAccount, "the balance is not enough")
}
//SignRawTransaction 签名交易单
func (decoder *TransactionDecoder) SignRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
if rawTx.Signatures == nil || len(rawTx.Signatures) == 0 {
//this.wm.Log.Std.Error("len of signatures error. ")
return fmt.Errorf("transaction signature is empty")
}
key, err := wrapper.HDKey()
if err != nil {
return err
}
keySignatures := rawTx.Signatures[rawTx.Account.AccountID]
if keySignatures != nil {
for _, keySignature := range keySignatures {
childKey, err := key.DerivedKeyWithPath(keySignature.Address.HDPath, keySignature.EccType)
keyBytes, err := childKey.GetPrivateKeyBytes()
if err != nil {
return err
}
//decoder.wm.Log.Debug("privateKey:", hex.EncodeToString(keyBytes))
hash, err := hex.DecodeString(keySignature.Message)
if err != nil {
return fmt.Errorf("decoder transaction hash failed, unexpected err: %v", err)
}
//decoder.wm.Log.Debug("hash:", hash)
signature, v, sigErr := owcrypt.Signature(keyBytes, nil, hash, decoder.wm.CurveType())
if sigErr == owcrypt.FAILURE {
return fmt.Errorf("sign transaction hash failed")
}
signature = append(signature, v)
keySignature.Signature = hex.EncodeToString(signature)
}
}
decoder.wm.Log.Info("transaction hash sign success")
rawTx.Signatures[rawTx.Account.AccountID] = keySignatures
return nil
}
//VerifyRawTransaction 验证交易单,验证交易单并返回加入签名后的交易单
func (decoder *TransactionDecoder) VerifyRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) error {
if rawTx.Signatures == nil || len(rawTx.Signatures) == 0 {
//this.wm.Log.Std.Error("len of signatures error. ")
return fmt.Errorf("transaction signature is empty")
}
var tx bt.SignedTransaction
txHex, err := hex.DecodeString(rawTx.RawHex)
if err != nil {
return fmt.Errorf("transaction DecodeString failed, unexpected error: %v", err)
}
err = tx.UnmarshalJSON(txHex)
if err != nil {
return fmt.Errorf("transaction UnmarshalJSON failed, unexpected error: %v", err)
}
//支持多重签名
for accountID, keySignatures := range rawTx.Signatures {
decoder.wm.Log.Debug("accountID Signatures:", accountID)
for _, keySignature := range keySignatures {
messsage, _ := hex.DecodeString(keySignature.Message)
signature, _ := hex.DecodeString(keySignature.Signature)
//publicKey, _ := hex.DecodeString(keySignature.Address.PublicKey)
//decoder.wm.Log.Debug("publicKey:", keySignature.Address.PublicKey)
//验证签名,解压公钥,解压后首字节04要去掉
//uncompessedPublicKey := owcrypt.PointDecompress(publicKey, decoder.wm.CurveType())
_, valid := owcrypt.RecoverPubkey(signature, messsage, decoder.wm.CurveType())
//valid, compactSig, err := eos_txsigner.Default.VerifyAndCombineSignature(messsage, uncompessedPublicKey[1:], signature)
if valid == owcrypt.FAILURE {
return fmt.Errorf("transaction verify failed: %v", err)
}
v := signature[len(signature)-1] //签名最后一字节是v
//验签通过后处理V值,符合节点验签
compactSig := signature[:len(signature)-1]
compactSig = append([]byte{v + 27 + 4}, compactSig...)
tx.Signatures = append(
tx.Signatures,
compactSig,
)
}
}
rawTx.IsCompleted = true
jsonTx, _ := tx.MarshalJSON()
rawTx.RawHex = hex.EncodeToString(jsonTx)
return nil
}
// SubmitRawTransaction 广播交易单
func (decoder *TransactionDecoder) SubmitRawTransaction(wrapper openwallet.WalletDAI, rawTx *openwallet.RawTransaction) (*openwallet.Transaction, error) {
var stx bt.SignedTransaction
txHex, err := hex.DecodeString(rawTx.RawHex)
if err != nil {
return nil, fmt.Errorf("transaction decode hex failed, unexpected error: %v", err)
}
err = stx.UnmarshalJSON(txHex)
if err != nil {
return nil, fmt.Errorf("transaction decode json failed, unexpected error: %v", err)
}
txid, err := decoder.wm.Api.BroadcastTransaction(&stx)
if err != nil {
log.Errorf("json: %s", string(txHex))
return nil, fmt.Errorf("push transaction: %s", err)
}
decoder.wm.Log.Info("Transaction [%s] submitted to the network successfully.", txid)
rawTx.TxID = txid
rawTx.IsSubmit = true
decimals := int32(rawTx.Coin.Contract.Decimals)
//记录一个交易单
tx := &openwallet.Transaction{
From: rawTx.TxFrom,
To: rawTx.TxTo,
Amount: rawTx.TxAmount,
Coin: rawTx.Coin,
TxID: rawTx.TxID,
Decimal: decimals,
AccountID: rawTx.Account.AccountID,
Fees: rawTx.Fees,
SubmitTime: time.Now().Unix(),
ExtParam: rawTx.ExtParam,
}
tx.WxID = openwallet.GenTransactionWxID(tx)
return tx, nil
}
//GetRawTransactionFeeRate 获取交易单的费率
func (decoder *TransactionDecoder) GetRawTransactionFeeRate() (feeRate string, unit string, err error) {
xwcFees := decimal.New(decoder.wm.Config.FixFees, 0)
xwcFees = xwcFees.Shift(-decoder.wm.Decimal())
return xwcFees.String(), "TX", nil
}
//CreateSummaryRawTransaction 创建汇总交易
func (decoder *TransactionDecoder) CreateSummaryRawTransaction(wrapper openwallet.WalletDAI, sumRawTx *openwallet.SummaryRawTransaction) ([]*openwallet.RawTransaction, error) {
var (
rawTxWithErrArray []*openwallet.RawTransactionWithError
rawTxArray = make([]*openwallet.RawTransaction, 0)
err error
)
rawTxWithErrArray, err = decoder.CreateSummaryRawTransactionWithError(wrapper, sumRawTx)
if err != nil {
return nil, err
}
for _, rawTxWithErr := range rawTxWithErrArray {
if rawTxWithErr.Error != nil {
continue
}
rawTxArray = append(rawTxArray, rawTxWithErr.RawTx)
}
return rawTxArray, nil
}
//CreateSummaryRawTransactionWithError 创建汇总交易
func (decoder *TransactionDecoder) CreateSummaryRawTransactionWithError(wrapper openwallet.WalletDAI, sumRawTx *openwallet.SummaryRawTransaction) ([]*openwallet.RawTransactionWithError, error) {
var (
rawTxArray = make([]*openwallet.RawTransactionWithError, 0)
accountID = sumRawTx.Account.AccountID
assetID types.ObjectID
precise uint64
)
if !sumRawTx.Coin.IsContract {
return nil, openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "only support contract")
}
assetID = types.MustParseObjectID(sumRawTx.Coin.Contract.Address)
precise = sumRawTx.Coin.Contract.Decimals
minTransfer, _ := decimal.NewFromString(sumRawTx.MinTransfer)
retainedBalance, _ := decimal.NewFromString(sumRawTx.RetainedBalance)
if minTransfer.LessThan(retainedBalance) {
return nil, fmt.Errorf("mini transfer amount must be greater than address retained balance")
}
//获取wallet
addresses, err := wrapper.GetAddressList(sumRawTx.AddressStartIndex, sumRawTx.AddressLimit, "AccountID", accountID) //wrapper.GetWallet().GetAddressesByAccount(rawTx.Account.AccountID)
if err != nil {
return nil, err
}
if len(addresses) == 0 {
return nil, openwallet.Errorf(openwallet.ErrAccountNotAddress, "[%s] have not addresses", accountID)
}
for _, addr := range addresses {
balance, err := decoder.wm.Api.GetAddrBalance(addr.Address, assetID)
if err != nil {
return nil, err
}
accountBalanceDec, _ := decimal.NewFromString(balance.Amount)
minTransfer = minTransfer.Shift(int32(precise))
retainedBalance = retainedBalance.Shift(int32(precise))
if accountBalanceDec.LessThan(minTransfer) || accountBalanceDec.LessThanOrEqual(decimal.Zero) {
continue
}
fees, err := decoder.wm.GetRequiredFee(nil, assetID.String())
if err != nil {
return nil, openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "can't get fees")
}
feesDec := decimal.Zero
for _, fee := range fees {
feesDec = feesDec.Add(decimal.New(int64(fee.Amount), 0))
}
//计算汇总数量 = 余额 - 保留余额 - 手续费
sumAmount := accountBalanceDec.Sub(retainedBalance).Sub(feesDec)
if sumAmount.LessThanOrEqual(decimal.Zero) {
continue
}
amountInt64 := sumAmount.IntPart()
//memo := sumRawTx.GetExtParam().Get("memo").String()
asset := bt.AssetIDFromObject(bt.NewAssetID(assetID.String()))
amount := bt.AssetAmount{
Asset: asset,
Amount: bt.Int64(amountInt64),
}
fromAddr, _ := bt.NewAddressFromString(addr.Address)
toAddr, _ := bt.NewAddressFromString(sumRawTx.SummaryAddress)
op := operations.TransferOperation{
Amount: amount,
Extensions: bt.Extensions{},
From: bt.AccountIDFromObject(bt.NewAccountID("1.2.0")),
To: bt.AccountIDFromObject(bt.NewAccountID("1.2.0")),
FromAddr: *fromAddr,
ToAddr: *toAddr,
}
ops := bt.Operations{&op}
if err := ops.ApplyFees(fees); err != nil {
return nil, openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "ApplyFees")
}
decoder.wm.Log.Debugf("balance: %s", accountBalanceDec.String())
decoder.wm.Log.Debugf("fees: %s", feesDec.String())
decoder.wm.Log.Debugf("sumAmount: %s", sumAmount.String())
//创建一笔交易单
rawTx := &openwallet.RawTransaction{
Coin: sumRawTx.Coin,
Account: sumRawTx.Account,
To: map[string]string{
sumRawTx.SummaryAddress: sumAmount.String(),
},
Required: 1,
}
//fromPublicKey, _ := bt.NewPublicKeyFromString(fromAccount.Options.MemoKey)
//toPublicKey, _ := bt.NewPublicKeyFromString(toAccount.Options.MemoKey)
//if memo != "" {
// m := bt.Memo{
// From: *fromPublicKey,
// To: *toPublicKey,
// Nonce: bt.UInt64(rand.Uint64()),
// }
// keyBag := crypto.NewKeyBag()
// keyBag.Add(decoder.wm.Config.MemoPrivateKey)
//
// if err := keyBag.EncryptMemo(&m, memo); err != nil {
// return nil, fmt.Errorf("EncryptMemo: %v", err)
// }
//
// op.Memo = &m
//}
createTxErr := decoder.createRawTransaction(
wrapper,
rawTx,
&accountBalanceDec,
addr,
ops,
"")
rawTxWithErr := &openwallet.RawTransactionWithError{
RawTx: rawTx,
Error: createTxErr,
}
//创建成功,添加到队列
rawTxArray = append(rawTxArray, rawTxWithErr)
}
return rawTxArray, nil
}
//createRawTransaction
func (decoder *TransactionDecoder) createRawTransaction(
wrapper openwallet.WalletDAI,
rawTx *openwallet.RawTransaction,
balanceDec *decimal.Decimal,
from *openwallet.Address,
ops bt.Operations,
memo string) *openwallet.Error {
var (
to string
accountTotalSent = decimal.Zero
txFrom = make([]string, 0)
txTo = make([]string, 0)
keySignList = make([]*openwallet.KeySignature, 0)
amountDec = decimal.Zero
curveType = decoder.wm.Config.CurveType
//assetID = bt.NewAssetID(rawTx.Coin.Contract.Address)
//precise = rawTx.Coin.Contract.Decimals
operations = ops
)
for k, v := range rawTx.To {
to = k
amountDec, _ = decimal.NewFromString(v)
break
}
info, err := decoder.wm.Api.GetBlockchainInfo()
if err != nil {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "GetBlockchainInfo")
}
j, _ := json.Marshal(info.HeadBlockID)
headBlockID := bt.String{}
headBlockID.UnmarshalJSON(j)
t := bt.Time{}
t.FromTime(info.Timestamp)
props := &bt.DynamicGlobalProperties{
HeadBlockID: headBlockID,
HeadBlockNumber: bt.UInt32(info.HeadBlockNum),
LastIrreversibleBlockNum: bt.UInt32(info.LastIrreversibleBlockNum),
Time: t,
}
tx, err := bt.NewSignedTransactionWithBlockData(props)
if err != nil {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "NewTransaction")
}
tx.Operations = operations
signer := crypto.NewTransactionSigner(tx)
//交易哈希
digest, err := signer.Digest(config.Current())
if err != nil {
return openwallet.Errorf(openwallet.ErrCreateRawTransactionFailed, "Calculate digest error: %v", err)
}
log.Debugf("digest: %s", hex.EncodeToString(digest))
signature := openwallet.KeySignature{
EccType: curveType,
Nonce: "",
Address: from,
Message: hex.EncodeToString(digest),
RSV: true,
}
keySignList = append(keySignList, &signature)
//计算账户的实际转账amount
if from.Address != to {
accountTotalSent = accountTotalSent.Add(amountDec)
}
accountTotalSent = decimal.Zero.Sub(accountTotalSent)
txFrom = []string{fmt.Sprintf("%s:%s", from.Address, amountDec.String())}
txTo = []string{fmt.Sprintf("%s:%s", to, amountDec.String())}
if rawTx.Signatures == nil {
rawTx.Signatures = make(map[string][]*openwallet.KeySignature)
}
feesDec := decimal.New(decoder.wm.Config.FixFees, 0)
feesDec = feesDec.Shift(-decoder.wm.Decimal())
jsonTx, _ := tx.MarshalJSON()
rawTx.RawHex = hex.EncodeToString(jsonTx)
rawTx.Signatures[rawTx.Account.AccountID] = keySignList
rawTx.FeeRate = feesDec.String()
rawTx.Fees = feesDec.String()
rawTx.IsBuilt = true
rawTx.TxAmount = accountTotalSent.String()
rawTx.TxFrom = txFrom
rawTx.TxTo = txTo
return nil
}