Skip to content

Commit fc6389e

Browse files
authored
Merge pull request #71 from keep-network/eip-155-patch
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 34905d2 + ad67a69 commit fc6389e

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

tools/generators/ethereum/command.go.tmpl

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

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

240248
return contract.New{{.Class}}(
241249
address,
250+
chainID,
242251
key,
243252
client,
244253
ethutil.NewNonceManager(key.Address, client),

tools/generators/ethereum/command_template_content.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ func initialize{{.Class}}(c *cli.Context) (*contract.{{.Class}}, error) {
207207
return nil, fmt.Errorf("error connecting to Ethereum node: [%v]", err)
208208
}
209209
210+
chainID, err := client.ChainID(context.Background())
211+
if err != nil {
212+
return nil, fmt.Errorf(
213+
"failed to resolve Ethereum chain id: [%v]",
214+
err,
215+
)
216+
}
217+
210218
key, err := ethutil.DecryptKeyFile(
211219
config.Account.KeyFile,
212220
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
ethutil.NewNonceManager(key.Address, client),

tools/generators/ethereum/contract.go.tmpl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ethereum/go-ethereum/accounts/keystore"
1414
"github.com/ethereum/go-ethereum/common"
1515
"github.com/ethereum/go-ethereum/core/types"
16+
"github.com/ethereum/go-ethereum/crypto"
1617

1718
"github.com/ipfs/go-log"
1819

@@ -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 *ethutil.NonceManager,
@@ -55,9 +57,25 @@ 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+
transactorOptions := &bind.TransactOpts{
65+
From: keyAddress,
66+
Signer: func(_ types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
67+
signer := types.NewEIP155Signer(chainId)
68+
69+
if address != keyAddress {
70+
return nil, fmt.Errorf("not authorized to sign this account")
71+
}
72+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
73+
if err != nil {
74+
return nil, err
75+
}
76+
return tx.WithSignature(signer, signature)
77+
},
78+
}
6179

6280
randomBeaconContract, err := abi.New{{.AbiClass}}(
6381
contractAddress,

tools/generators/ethereum/contract_template_content.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/ethereum/go-ethereum/accounts/keystore"
1717
"github.com/ethereum/go-ethereum/common"
1818
"github.com/ethereum/go-ethereum/core/types"
19+
"github.com/ethereum/go-ethereum/crypto"
1920
2021
"github.com/ipfs/go-log"
2122
@@ -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 *ethutil.NonceManager,
@@ -58,9 +60,25 @@ 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+
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+
}
6482
6583
randomBeaconContract, err := abi.New{{.AbiClass}}(
6684
contractAddress,

0 commit comments

Comments
 (0)