Skip to content

Commit 3203332

Browse files
authored
Merge pull request #72 from keep-network/eip-155-patch-backport
Backport: Adjust Ethereum contracts to use EIP155 signer go-ethereum 1.10.0 has switched to denying non-EIP155 signatures by default. Although our code uses go-ethereum's own ABI binding generation, the version currently in our dependencies never added support for EIP155 signers, and instead always uses Homestead signers for the transactors it provides helpers for. In the short term, this commit manually creates the transactor options for the account key, forcing an EIP155 signer when doing signing. When the go-ethereum dependency gets bumped past 1.9.25, we will be able to move back to `NewKeyedTransactorWithChainID`, which uses an EIP155 signer under the hood.
2 parents cbbc8f4 + 94d2520 commit 3203332

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

tools/generators/ethlike/command.go.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ func initialize{{.Class}}(c *cli.Context) (*contract.{{.Class}}, error) {
203203
return nil, fmt.Errorf("error connecting to host chain node: [%v]", err)
204204
}
205205

206+
chainID, err := client.ChainID(context.Background())
207+
if err != nil {
208+
return nil, fmt.Errorf(
209+
"failed to resolve host chain id: [%v]",
210+
err,
211+
)
212+
}
213+
206214
key, err := chainutil.DecryptKeyFile(
207215
config.Account.KeyFile,
208216
config.Account.KeyFilePassword,
@@ -242,6 +250,7 @@ func initialize{{.Class}}(c *cli.Context) (*contract.{{.Class}}, error) {
242250

243251
return contract.New{{.Class}}(
244252
address,
253+
chainID,
245254
key,
246255
client,
247256
chainutil.NewNonceManager(client, key.Address),

tools/generators/ethlike/command_template_content.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ func initialize{{.Class}}(c *cli.Context) (*contract.{{.Class}}, error) {
206206
return nil, fmt.Errorf("error connecting to host chain node: [%v]", err)
207207
}
208208
209+
chainID, err := client.ChainID(context.Background())
210+
if err != nil {
211+
return nil, fmt.Errorf(
212+
"failed to resolve host chain id: [%v]",
213+
err,
214+
)
215+
}
216+
209217
key, err := chainutil.DecryptKeyFile(
210218
config.Account.KeyFile,
211219
config.Account.KeyFilePassword,
@@ -245,6 +253,7 @@ func initialize{{.Class}}(c *cli.Context) (*contract.{{.Class}}, error) {
245253
246254
return contract.New{{.Class}}(
247255
address,
256+
chainID,
248257
key,
249258
client,
250259
chainutil.NewNonceManager(client, key.Address),

tools/generators/ethlike/contract.go.tmpl

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"{{.HostChainModule}}/accounts/keystore"
1313
"{{.HostChainModule}}/common"
1414
"{{.HostChainModule}}/core/types"
15+
"{{.HostChainModule}}/crypto"
1516
"{{.HostChainModule}}/event"
1617

1718
"github.com/ipfs/go-log"
@@ -44,6 +45,7 @@ type {{.Class}} struct {
4445

4546
func New{{.Class}}(
4647
contractAddress common.Address,
48+
chainId *big.Int,
4749
accountKey *keystore.Key,
4850
backend bind.ContractBackend,
4951
nonceManager *ethlike.NonceManager,
@@ -55,9 +57,28 @@ func New{{.Class}}(
5557
From: accountKey.Address,
5658
}
5759

58-
transactorOptions := bind.NewKeyedTransactor(
59-
accountKey.PrivateKey,
60-
)
60+
// FIXME Switch to bind.NewKeyedTransactorWithChainID when go-ethereum dep
61+
// FIXME bumps beyond 1.9.25.
62+
key := accountKey.PrivateKey
63+
keyAddress := crypto.PubkeyToAddress(key.PublicKey)
64+
if chainId == nil {
65+
return nil, fmt.Errorf("no chain id specified")
66+
}
67+
transactorOptions := &bind.TransactOpts{
68+
From: keyAddress,
69+
Signer: func(_ types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
70+
signer := types.NewEIP155Signer(chainId)
71+
72+
if address != keyAddress {
73+
return nil, fmt.Errorf("not authorized to sign this account")
74+
}
75+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
76+
if err != nil {
77+
return nil, err
78+
}
79+
return tx.WithSignature(signer, signature)
80+
},
81+
}
6182

6283
contract, err := abi.New{{.AbiClass}}(
6384
contractAddress,

tools/generators/ethlike/contract_template_content.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"{{.HostChainModule}}/accounts/keystore"
1616
"{{.HostChainModule}}/common"
1717
"{{.HostChainModule}}/core/types"
18+
"{{.HostChainModule}}/crypto"
1819
"{{.HostChainModule}}/event"
1920
2021
"github.com/ipfs/go-log"
@@ -47,6 +48,7 @@ type {{.Class}} struct {
4748
4849
func New{{.Class}}(
4950
contractAddress common.Address,
51+
chainId *big.Int,
5052
accountKey *keystore.Key,
5153
backend bind.ContractBackend,
5254
nonceManager *ethlike.NonceManager,
@@ -58,9 +60,28 @@ func New{{.Class}}(
5860
From: accountKey.Address,
5961
}
6062
61-
transactorOptions := bind.NewKeyedTransactor(
62-
accountKey.PrivateKey,
63-
)
63+
// FIXME Switch to bind.NewKeyedTransactorWithChainID when go-ethereum dep
64+
// FIXME bumps beyond 1.9.25.
65+
key := accountKey.PrivateKey
66+
keyAddress := crypto.PubkeyToAddress(key.PublicKey)
67+
if chainId == nil {
68+
return nil, fmt.Errorf("no chain id specified")
69+
}
70+
transactorOptions := &bind.TransactOpts{
71+
From: keyAddress,
72+
Signer: func(_ types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
73+
signer := types.NewEIP155Signer(chainId)
74+
75+
if address != keyAddress {
76+
return nil, fmt.Errorf("not authorized to sign this account")
77+
}
78+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
79+
if err != nil {
80+
return nil, err
81+
}
82+
return tx.WithSignature(signer, signature)
83+
},
84+
}
6485
6586
contract, err := abi.New{{.AbiClass}}(
6687
contractAddress,

0 commit comments

Comments
 (0)