accounts/abi/bind: allow specifying signer on transactOpts (#21356)

This commit enables users to specify which signer they want to use while creating their transactOpts.
Previously all contract interactions used the homestead signer. Now a user can specify whether they
want to sign with homestead or EIP155 and specify the chainID which adds another layer of security.

Closes #16484
This commit is contained in:
Marius van der Wijden
2020-12-08 14:44:56 +01:00
committed by GitHub
parent 6a4e730003
commit ed0670cb17
9 changed files with 127 additions and 40 deletions

View File

@ -40,7 +40,7 @@ type MobileSigner struct {
}
func (s *MobileSigner) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
sig, err := s.sign(types.EIP155Signer{}, addr.address, unsignedTx.tx)
sig, err := s.sign(addr.address, unsignedTx.tx)
if err != nil {
return nil, err
}
@ -82,12 +82,16 @@ func NewTransactOpts() *TransactOpts {
// NewKeyedTransactOpts is a utility method to easily create a transaction signer
// from a single private key.
func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
func NewKeyedTransactOpts(keyJson []byte, passphrase string, chainID *big.Int) (*TransactOpts, error) {
key, err := keystore.DecryptKey(keyJson, passphrase)
if err != nil {
return nil, err
}
return &TransactOpts{*bind.NewKeyedTransactor(key.PrivateKey)}, nil
auth, err := bind.NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
if err != nil {
return nil, err
}
return &TransactOpts{*auth}, nil
}
func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
@ -106,7 +110,7 @@ func (opts *TransactOpts) GetGasLimit() int64 { return int64(opts.opts.GasLimi
func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address }
func (opts *TransactOpts) SetNonce(nonce int64) { opts.opts.Nonce = big.NewInt(nonce) }
func (opts *TransactOpts) SetSigner(s Signer) {
opts.opts.Signer = func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
opts.opts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
sig, err := s.Sign(&Address{addr}, &Transaction{tx})
if err != nil {
return nil, err