/
relayToEthereum.go
81 lines (66 loc) · 3.22 KB
/
relayToEthereum.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
package ethtxs
import (
"crypto/ecdsa"
"github.com/33cn/plugincgo/plugin/crypto/secp256k1hsm/adapter"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/lianbaotong/ebrelayer/utils"
"github.com/lianbaotong/ebrelayer/relayer/ethereum/ethinterface"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/33cn/chain33/common/log/log15"
"github.com/33cn/plugin/plugin/dapp/cross2eth/contracts/contracts4eth/generated"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
txslog = log15.New("ethereum relayer", "ethtxs")
)
//const ...
const (
// GasLimit : the gas limit in Gwei used for transactions sent with TransactOpts
GasLimit = uint64(100 * 10000)
GasLimit4Deploy = uint64(0) //此处需要设置为0,让交易自行估计,否则将会导致部署失败,TODO:其他解决途径后续调研解决
)
// RelayOracleClaimToEthereum : relays the provided burn or lock to Chain33Bridge contract on the Ethereum network
func RelayOracleClaimToEthereum(oracleInstance *generated.Oracle, client ethinterface.EthClientSpec, sender, tokenOnEth common.Address, claim ProphecyClaim, privateKey *ecdsa.PrivateKey, signViaHsm bool, secp256k1Index int) (txhash string, err error) {
txslog.Info("RelayProphecyClaimToEthereum", "sender", sender.String(), "chain33Sender", hexutil.Encode(claim.Chain33Sender), "ethereumReceiver", claim.EthereumReceiver.String(),
"TokenAddress", claim.TokenContractAddress.String(), "symbol", claim.Symbol, "Amount", claim.Amount.String(), "claimType", claim.ClaimType, "tokenOnEth", tokenOnEth.String())
var auth *bind.TransactOpts
if signViaHsm {
auth, err = PrepareAuthHsm(client, secp256k1Index, sender)
if nil != err {
txslog.Error("RelayProphecyClaimToEthereum", "PrepareAuthHsm err", err.Error())
return "", err
}
} else {
auth, err = PrepareAuth(client, privateKey, sender)
if nil != err {
txslog.Error("RelayProphecyClaimToEthereum", "PrepareAuth err", err.Error())
return "", err
}
}
auth.GasLimit = GasLimit
claimID := crypto.Keccak256Hash(claim.chain33TxHash, claim.Chain33Sender, claim.EthereumReceiver.Bytes(), []byte(claim.Symbol), claim.Amount.Bytes())
// Sign the hash using the active validator's private key
var signature []byte
if signViaHsm {
R, S, err := adapter.SignSecp256k1Workaround(utils.SoliditySHA3WithPrefix(claimID[:]), secp256k1Index, sender.String(), adapter.AddrVerifyTypeEthereum)
if nil != err {
panic("Failed to Sign Secp256k1 via HSM due to" + err.Error())
}
signature = adapter.MakeRSVsignature(R, S)
} else {
signature, err = utils.SignClaim4Evm(claimID, privateKey)
if nil != err {
return "", err
}
}
txslog.Info("RelayProphecyClaimToEthereum", "sender", sender.String(), "nonce", auth.Nonce, "claim.chain33TxHash", common.Bytes2Hex(claim.chain33TxHash))
tx, err := oracleInstance.NewOracleClaim(auth, uint8(claim.ClaimType), claim.Chain33Sender, claim.EthereumReceiver, tokenOnEth, claim.Symbol, claim.Amount, claimID, signature)
if nil != err {
txslog.Error("RelayProphecyClaimToEthereum", "NewOracleClaim failed due to:", err.Error())
return "", err
}
txhash = tx.Hash().Hex()
txslog.Info("RelayProphecyClaimToEthereum", "NewOracleClaim tx hash:", txhash)
return txhash, nil
}