Skip to content

Commit 035b7fa

Browse files
zhiqiangxuzouxyanrenlulurenlulujoeqian10
authored
add tools for polygon,neo3,ok,bsc,zil,kai etc (#13)
* optimized and add bsc * refactor (#4) * mod text (#5) * add config.json to gitignore (#6) * fix (#8) * add SendBnbCrossEth (#9) * add SendBnbCrossEth * fix * fix testcase (#10) * fix test * fix test * fix test * fix test * log * log * add GetBlockHeaderByHash * mod .gitignore * mod bsc blkToWait * Revert "mod bsc blkToWait" This reverts commit 259f098. * add testcase * add testcase * add neo support to bsc * testcase for eth->bsc * add NeoToBscCircle * feat: register and sync for zilliqa devnet * chore: remove testcode * fix: register ccmc address * chore: update config file * chore: recovery some default config * fix: one node's ipaddr * chore: update zilliqa sdk version * fix(synczil): add 0x for raw header * chore(zil): ds node ip * fix ide warning * add msc * feat(zil) contract deploy * fix: rename * add ok * add ok support * mod cosmos * add o3 deployer * mod for o3 * o3too3 * mod * use clique for o3 * sync genesis for okex * add heco deployer * mod o3 deployer * fix(zilliqa): get rid of hard code for syncing genesis info * further resolve conflicts * fix bracket localtion * fix(zilliqa): register chain * fix(zil): sync header * refactor(zil): change veriable name, add some annotation * add tools for neo3 * fix a typo * rm dup field * update neo-gogogo version * fix * update .gitignore * mod msg * add ok * add ok * mod * update neo3 ccmc format * fix * add neo3 state validator related commands * update dependency * use new poly-go-sdk * update flag name to neo3_state_validators * rm useless * fix * add update_ok * mod tidy * add extra info (Neo3Magic) when registering or updating neo3 chain * fix param order * add test kai chain * add sync kai genesis header command * merge * update kardiachain config * fix kai signing * update kaichain id * update kai endpoint * mod for kai * fix kai client * update kaiclient * Fix DeploySmartContract logic in kai_invoker * upgrade kai relayer version * update kai epoch * update kai testcases * UPDATE KAI TEST * FIX SendKaiToEthChain * update * fix register kai * update kai testcases * UPDATE KAI TEST * FIX SendKaiToEthChain * update * fix register kai * uncomment ontInvoker, neoInvoker. revert max confirmation time in eth_tools.go * fix * update neo3-gogogo to v0.3.5 * update neo3 router number to 14 * revert go-ethereum version * fix * update poly dependency * fix wrong block hash * add polygon * add polygon_deployer * add SetupMatic to polygon deployer * add eip155 support for bor * add update_bor * add MaticToBor * mod for bor * update * fix * update Co-authored-by: zouxyan <mailzouxueyan@163.com> Co-authored-by: xiaohuo <xiaohuo200@gmail.com> Co-authored-by: renlulu <lulu@zilliqa.com> Co-authored-by: zhuoqian <zhuoqian10@gmail.com> Co-authored-by: joeqian <qianzhuo@ngd.neo.org> Co-authored-by: dogecoindev <dogecoindev@gmail.com> Co-authored-by: trinhdn97 <trinhdn@protonmail.com>
1 parent 48aeafe commit 035b7fa

20 files changed

Lines changed: 1986 additions & 115 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ report/*
88
test.go
99
fisco_sdk.toml
1010
*.yaml
11+
.DS_Store

chains/eth/eth_eip155_signer.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package eth
2+
3+
import (
4+
"crypto/ecdsa"
5+
"errors"
6+
"fmt"
7+
"math/big"
8+
"sync"
9+
10+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
11+
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/core/types"
13+
"github.com/ethereum/go-ethereum/crypto"
14+
"github.com/ethereum/go-ethereum/rlp"
15+
"golang.org/x/crypto/sha3"
16+
)
17+
18+
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*bind.TransactOpts, error) {
19+
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
20+
if chainID == nil {
21+
return nil, fmt.Errorf("ErrNoChainID")
22+
}
23+
signer := NewEIP155Signer(chainID)
24+
return &bind.TransactOpts{
25+
From: keyAddr,
26+
Signer: func(_ types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
27+
if address != keyAddr {
28+
return nil, fmt.Errorf("ErrNotAuthorized")
29+
}
30+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return tx.WithSignature(signer, signature)
35+
},
36+
}, nil
37+
}
38+
39+
// EIP155Signer implements Signer using the EIP-155 rules. This accepts transactions which
40+
// are replay-protected as well as unprotected homestead transactions.
41+
type EIP155Signer struct {
42+
chainId, chainIdMul *big.Int
43+
}
44+
45+
func NewEIP155Signer(chainId *big.Int) EIP155Signer {
46+
if chainId == nil {
47+
chainId = new(big.Int)
48+
}
49+
return EIP155Signer{
50+
chainId: chainId,
51+
chainIdMul: new(big.Int).Mul(chainId, big.NewInt(2)),
52+
}
53+
}
54+
55+
func (s EIP155Signer) ChainID() *big.Int {
56+
return s.chainId
57+
}
58+
59+
func (s EIP155Signer) Equal(s2 types.Signer) bool {
60+
eip155, ok := s2.(EIP155Signer)
61+
return ok && eip155.chainId.Cmp(s.chainId) == 0
62+
}
63+
64+
var big8 = big.NewInt(8)
65+
66+
func (s EIP155Signer) Sender(tx *types.Transaction) (common.Address, error) {
67+
if tx.ChainId().Cmp(s.chainId) != 0 {
68+
return common.Address{}, fmt.Errorf("ErrInvalidChainId")
69+
}
70+
V, R, S := tx.RawSignatureValues()
71+
V = new(big.Int).Sub(V, s.chainIdMul)
72+
V.Sub(V, big8)
73+
return recoverPlain(s.Hash(tx), R, S, V, true)
74+
}
75+
76+
func recoverPlain(sighash common.Hash, R, S, Vb *big.Int, homestead bool) (common.Address, error) {
77+
if Vb.BitLen() > 8 {
78+
return common.Address{}, errors.New("ErrInvalidSig")
79+
}
80+
V := byte(Vb.Uint64() - 27)
81+
if !crypto.ValidateSignatureValues(V, R, S, homestead) {
82+
return common.Address{}, errors.New("ErrInvalidSig")
83+
}
84+
// encode the signature in uncompressed format
85+
r, s := R.Bytes(), S.Bytes()
86+
sig := make([]byte, crypto.SignatureLength)
87+
copy(sig[32-len(r):32], r)
88+
copy(sig[64-len(s):64], s)
89+
sig[64] = V
90+
// recover the public key from the signature
91+
pub, err := crypto.Ecrecover(sighash[:], sig)
92+
if err != nil {
93+
return common.Address{}, err
94+
}
95+
if len(pub) == 0 || pub[0] != 4 {
96+
return common.Address{}, errors.New("invalid public key")
97+
}
98+
var addr common.Address
99+
copy(addr[:], crypto.Keccak256(pub[1:])[12:])
100+
return addr, nil
101+
}
102+
103+
func decodeSignature(sig []byte) (r, s, v *big.Int) {
104+
if len(sig) != crypto.SignatureLength {
105+
panic(fmt.Sprintf("wrong size for signature: got %d, want %d", len(sig), crypto.SignatureLength))
106+
}
107+
r = new(big.Int).SetBytes(sig[:32])
108+
s = new(big.Int).SetBytes(sig[32:64])
109+
v = new(big.Int).SetBytes([]byte{sig[64] + 27})
110+
return r, s, v
111+
}
112+
113+
// SignatureValues returns signature values. This signature
114+
// needs to be in the [R || S || V] format where V is 0 or 1.
115+
func (s EIP155Signer) SignatureValues(tx *types.Transaction, sig []byte) (R, S, V *big.Int, err error) {
116+
R, S, V = decodeSignature(sig)
117+
if s.chainId.Sign() != 0 {
118+
V = big.NewInt(int64(sig[64] + 35))
119+
V.Add(V, s.chainIdMul)
120+
}
121+
return R, S, V, nil
122+
}
123+
124+
// hasherPool holds LegacyKeccak256 hashers for rlpHash.
125+
var hasherPool = sync.Pool{
126+
New: func() interface{} { return sha3.NewLegacyKeccak256() },
127+
}
128+
129+
// rlpHash encodes x and hashes the encoded bytes.
130+
func rlpHash(x interface{}) (h common.Hash) {
131+
sha := hasherPool.Get().(crypto.KeccakState)
132+
defer hasherPool.Put(sha)
133+
sha.Reset()
134+
rlp.Encode(sha, x)
135+
sha.Read(h[:])
136+
return h
137+
}
138+
139+
// Hash returns the hash to be signed by the sender.
140+
// It does not uniquely identify the transaction.
141+
func (s EIP155Signer) Hash(tx *types.Transaction) common.Hash {
142+
return rlpHash([]interface{}{
143+
tx.Nonce(),
144+
tx.GasPrice(),
145+
tx.Gas(),
146+
tx.To(),
147+
tx.Value(),
148+
tx.Data(),
149+
s.chainId, uint(0), uint(0),
150+
})
151+
}

chains/eth/eth_invoker.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ func (ethInvoker *EInvoker) url() string {
8383
return ethInvoker.TConfiguration.O3URL
8484
case ethInvoker.TConfiguration.OkChainID:
8585
return ethInvoker.TConfiguration.OKURL
86+
case ethInvoker.TConfiguration.KaiChainID:
87+
return ethInvoker.TConfiguration.KaiUrl
88+
case ethInvoker.TConfiguration.PolygonBorChainID:
89+
return ethInvoker.TConfiguration.BorURL
8690
default:
87-
panic(fmt.Sprintf("unknown chain id:%d", ethInvoker.ChainID))
91+
panic(fmt.Sprintf("url:unknown chain id:%d", ethInvoker.ChainID))
8892
}
8993
}
9094

@@ -104,8 +108,12 @@ func (ethInvoker *EInvoker) privateKey() string {
104108
return ethInvoker.TConfiguration.O3PrivateKey
105109
case ethInvoker.TConfiguration.OkChainID:
106110
return ethInvoker.TConfiguration.OKPrivateKey
111+
case ethInvoker.TConfiguration.KaiChainID:
112+
return ethInvoker.TConfiguration.KaiPrivateKey
113+
case ethInvoker.TConfiguration.PolygonBorChainID:
114+
return ethInvoker.TConfiguration.BorPrivateKey
107115
default:
108-
panic(fmt.Sprintf("unknown chain id:%d", ethInvoker.ChainID))
116+
panic(fmt.Sprintf("privateKey:unknown chain id:%d", ethInvoker.ChainID))
109117
}
110118
}
111119

@@ -124,7 +132,16 @@ func (ethInvoker *EInvoker) MakeSmartContractAuth() (*bind.TransactOpts, error)
124132
if err != nil {
125133
return nil, fmt.Errorf("MakeSmartContractAuth, %v", err)
126134
}
127-
auth := bind.NewKeyedTransactor(ethInvoker.PrivateKey)
135+
var auth *bind.TransactOpts
136+
if ethInvoker.ChainID == ethInvoker.TConfiguration.PolygonBorChainID {
137+
auth, err = NewKeyedTransactorWithChainID(ethInvoker.PrivateKey, big.NewInt(int64(ethInvoker.TConfiguration.PolygonBorSignerChainID)))
138+
if err != nil {
139+
return nil, fmt.Errorf("NewKeyedTransactorWithChainID fail, %v", err)
140+
}
141+
} else {
142+
auth = bind.NewKeyedTransactor(ethInvoker.PrivateKey)
143+
}
144+
128145
auth.Nonce = big.NewInt(int64(nonce))
129146
auth.Value = big.NewInt(int64(0)) // in wei
130147
auth.GasLimit = uint64(DefaultGasLimit) // in units
@@ -238,6 +255,10 @@ func (ethInvoker *EInvoker) BindAssetHash(lockProxyAddr, fromAssetHash, toAssetH
238255
toAddr = ethComm.HexToAddress(toAssetHash).Bytes()
239256
} else if uint64(toChainId) == config.DefConfig.O3ChainID {
240257
toAddr = ethComm.HexToAddress(toAssetHash).Bytes()
258+
} else if uint64(toChainId) == config.DefConfig.KaiChainID {
259+
toAddr = ethComm.HexToAddress(toAssetHash).Bytes()
260+
} else if uint64(toChainId) == config.DefConfig.PolygonBorChainID {
261+
toAddr = ethComm.HexToAddress(toAssetHash).Bytes()
241262
} else if uint64(toChainId) == config.DefConfig.NeoChainID {
242263
other, err := helper.UInt160FromString(toAssetHash)
243264
if err != nil {

cmd/cctest/main.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ func main() {
6262
}
6363

6464
var (
65-
ethInvoker *eth.EInvoker
66-
bscInvoker *eth.EInvoker
67-
mscInvoker *eth.EInvoker
68-
o3Invoker *eth.EInvoker
69-
cmInvoker *cosmos.CosmosInvoker
65+
ethInvoker *eth.EInvoker
66+
bscInvoker *eth.EInvoker
67+
mscInvoker *eth.EInvoker
68+
o3Invoker *eth.EInvoker
69+
cmInvoker *cosmos.CosmosInvoker
70+
kaiInvoker, borInvoker *eth.EInvoker
7071
)
7172
if config.DefConfig.EthChainID > 0 {
7273
ethInvoker = eth.NewEInvoker(config.DefConfig.EthChainID)
@@ -84,6 +85,14 @@ func main() {
8485
o3Invoker = eth.NewEInvoker(config.DefConfig.O3ChainID)
8586
}
8687

88+
if config.DefConfig.KaiChainID > 0 {
89+
kaiInvoker = eth.NewEInvoker(config.DefConfig.KaiChainID)
90+
}
91+
92+
if config.DefConfig.PolygonBorChainID > 0 {
93+
borInvoker = eth.NewEInvoker(config.DefConfig.PolygonBorChainID)
94+
}
95+
8796
//btcInvoker, err := btc.NewBtcInvoker(config.DefConfig.RchainJsonRpcAddress, config.DefConfig.RCWallet,
8897
// config.DefConfig.RCWalletPwd, config.DefConfig.BtcRestAddr, config.DefConfig.BtcRestUser,
8998
// config.DefConfig.BtcRestPwd, config.DefConfig.BtcSignerPrivateKey)
@@ -124,6 +133,14 @@ func main() {
124133
if o3Invoker != nil {
125134
testframework.TFramework.SetO3Invoker(o3Invoker)
126135
}
136+
137+
if kaiInvoker != nil {
138+
testframework.TFramework.SeKaiInvoker(kaiInvoker)
139+
}
140+
if borInvoker != nil {
141+
testframework.TFramework.SeBorInvoker(borInvoker)
142+
}
143+
127144
//testframework.TFramework.SetBtcInvoker(btcInvoker)
128145
testframework.TFramework.SetOntInvoker(ontInvoker)
129146
testframework.TFramework.SetCosmosInvoker(cmInvoker)

0 commit comments

Comments
 (0)