Skip to content

Commit

Permalink
update sign with adjust v value
Browse files Browse the repository at this point in the history
  • Loading branch information
jowenshaw committed May 14, 2021
1 parent 9e4c0ba commit b6be7b7
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions tokens/eth/signtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (b *Bridge) DcrmSignTransaction(rawTx interface{}, args *tokens.BuildTxArgs
}
gasPrice, err := b.getGasPrice(args)
if err == nil && args.Extra.EthExtra.GasPrice.Cmp(gasPrice) < 0 {
log.Info(b.ChainConfig.BlockChain+" DcrmSignTransaction update gas price", "txid", args.SwapID, "oldGasPrice", args.Extra.EthExtra.GasPrice, "newGasPrice", gasPrice)
args.Extra.EthExtra.GasPrice = gasPrice
tx.SetGasPrice(gasPrice)
}
Expand Down Expand Up @@ -72,25 +73,38 @@ func (b *Bridge) DcrmSignTransaction(rawTx interface{}, args *tokens.BuildTxArgs
return nil, "", errors.New("wrong signature of keyID " + keyID)
}

signedTx, err := tx.WithSignature(signer, signature)
token := b.GetTokenConfig(args.PairID)
signedTx, err := b.signTxWithSignature(tx, signature, common.HexToAddress(token.DcrmAddress))
if err != nil {
return nil, "", err
}
txHash = signedTx.Hash().String()
log.Info(b.ChainConfig.BlockChain+" DcrmSignTransaction success", "keyID", keyID, "txid", args.SwapID, "txhash", txHash, "nonce", signedTx.Nonce())
return signedTx, txHash, nil
}

sender, err := types.Sender(signer, signedTx)
if err != nil {
return nil, "", err
}
func (b *Bridge) signTxWithSignature(tx *types.Transaction, signature []byte, signerAddr common.Address) (*types.Transaction, error) {
signer := b.Signer
vPos := crypto.SignatureLength - 1
for i := 0; i < 2; i++ {
signedTx, err := tx.WithSignature(signer, signature)
if err != nil {
return nil, err
}

pairID := args.PairID
token := b.GetTokenConfig(pairID)
if sender != common.HexToAddress(token.DcrmAddress) {
log.Error("DcrmSignTransaction verify sender failed", "have", sender.String(), "want", token.DcrmAddress)
return nil, "", errors.New("wrong sender address")
sender, err := types.Sender(signer, signedTx)
if err != nil {
return nil, err
}

if sender == signerAddr {
return signedTx, nil
}

signature[vPos] ^= 0x1 // v can only be 0 or 1
}
txHash = signedTx.Hash().String()
log.Info(b.ChainConfig.BlockChain+" DcrmSignTransaction success", "keyID", keyID, "txid", args.SwapID, "txhash", txHash, "nonce", signedTx.Nonce())
return signedTx, txHash, err

return nil, errors.New("wrong sender address")
}

// SignTransaction sign tx with pairID
Expand Down

0 comments on commit b6be7b7

Please sign in to comment.