-
Notifications
You must be signed in to change notification settings - Fork 20.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
accounts/abi/bind: allow to specify signer on transactOpts #21356
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a68ffd
internal/ethapi: add optional parameter blockNrOrHash to estimateGas …
MariusVanDerWijden 7726cde
accounts/abi/bind: allow to specify signer on transactOpts
MariusVanDerWijden 90744e1
accounts/abi/bind: change api of transactor helpers
MariusVanDerWijden 5b5241a
accounts/abi/bind/: restore and deprecate previous functions
MariusVanDerWijden 7b29c7d
accounts/abi/bind: Use log.Warn instead of println
MariusVanDerWijden fc7fbb4
accounts/abi/bind: add custom errors, don't sign for mainnet
MariusVanDerWijden e61c432
accounts/abi/bind: better error handling
MariusVanDerWijden f68040b
accounts/abi/bind: better error names
MariusVanDerWijden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,18 +21,29 @@ import ( | |
"errors" | ||
"io" | ||
"io/ioutil" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/accounts/external" | ||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
// NoChainID is returned whenever the user failed to specify a chain id. | ||
var NoChainID = errors.New("no chain id specified") | ||
|
||
// NotAuthorized is returned when an account is not properly unlocked. | ||
var NotAuthorized = errors.New("not authorized to sign this account") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NotAuthorized -> ErrNotAuthorized |
||
|
||
// NewTransactor is a utility method to easily create a transaction signer from | ||
// an encrypted json key stream and the associated passphrase. | ||
// | ||
// Deprecated: Use NewTransactorWithChainID instead. | ||
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { | ||
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") | ||
json, err := ioutil.ReadAll(keyin) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -45,13 +56,17 @@ func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { | |
} | ||
|
||
// NewKeyStoreTransactor is a utility method to easily create a transaction signer from | ||
// a decrypted key from a keystore. | ||
// an decrypted key from a keystore. | ||
// | ||
// Deprecated: Use NewKeyStoreTransactorWithChainID instead. | ||
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { | ||
log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID") | ||
signer := types.HomesteadSigner{} | ||
return &TransactOpts{ | ||
From: account.Address, | ||
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
if address != account.Address { | ||
return nil, errors.New("not authorized to sign this account") | ||
return nil, NotAuthorized | ||
} | ||
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) | ||
if err != nil { | ||
|
@@ -64,13 +79,17 @@ func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account | |
|
||
// NewKeyedTransactor is a utility method to easily create a transaction signer | ||
// from a single private key. | ||
// | ||
// Deprecated: Use NewKeyedTransactorWithChainID instead. | ||
func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { | ||
log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") | ||
keyAddr := crypto.PubkeyToAddress(key.PublicKey) | ||
signer := types.HomesteadSigner{} | ||
return &TransactOpts{ | ||
From: keyAddr, | ||
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
if address != keyAddr { | ||
return nil, errors.New("not authorized to sign this account") | ||
return nil, NotAuthorized | ||
} | ||
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) | ||
if err != nil { | ||
|
@@ -81,14 +100,73 @@ func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { | |
} | ||
} | ||
|
||
// NewTransactorWithChainID is a utility method to easily create a transaction signer from | ||
// an encrypted json key stream and the associated passphrase. | ||
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { | ||
json, err := ioutil.ReadAll(keyin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
key, err := keystore.DecryptKey(json, passphrase) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewKeyedTransactorWithChainID(key.PrivateKey, chainID) | ||
} | ||
|
||
// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from | ||
// an decrypted key from a keystore. | ||
func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { | ||
if chainID == nil { | ||
return nil, NoChainID | ||
} | ||
signer := types.NewEIP155Signer(chainID) | ||
return &TransactOpts{ | ||
From: account.Address, | ||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
if address != account.Address { | ||
return nil, NotAuthorized | ||
} | ||
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tx.WithSignature(signer, signature) | ||
}, | ||
}, nil | ||
} | ||
|
||
// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer | ||
// from a single private key. | ||
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) { | ||
keyAddr := crypto.PubkeyToAddress(key.PublicKey) | ||
if chainID == nil { | ||
return nil, NoChainID | ||
} | ||
signer := types.NewEIP155Signer(chainID) | ||
return &TransactOpts{ | ||
From: keyAddr, | ||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
if address != keyAddr { | ||
return nil, NotAuthorized | ||
} | ||
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tx.WithSignature(signer, signature) | ||
}, | ||
}, nil | ||
} | ||
|
||
// NewClefTransactor is a utility method to easily create a transaction signer | ||
// with a clef backend. | ||
func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { | ||
return &TransactOpts{ | ||
From: account.Address, | ||
Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) { | ||
Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { | ||
if address != account.Address { | ||
return nil, errors.New("not authorized to sign this account") | ||
return nil, NotAuthorized | ||
} | ||
return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ErrNoChainID