From 313018496f6b5cb442749d10bc071dc15f6bc550 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:09 +0800 Subject: [PATCH] accounts: added transactorFromKeyStore (#19685) --- accounts/abi/bind/auth.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/accounts/abi/bind/auth.go b/accounts/abi/bind/auth.go index a53aeef366..b661a29f4d 100644 --- a/accounts/abi/bind/auth.go +++ b/accounts/abi/bind/auth.go @@ -53,6 +53,25 @@ func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { return NewKeyedTransactor(key.PrivateKey), nil } +// NewTransactor is a utility method to easily create a transaction signer from +// an decrypted key from a keystore +func NewTransactorFromKeyStore(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { + signer := types.HomesteadSigner{} + return &TransactOpts{ + From: account.Address, + 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") + } + signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) + if err != nil { + return nil, err + } + return tx.WithSignature(signer, signature) + }, + }, nil +} + // NewKeyedTransactor is a utility method to easily create a transaction signer // from a single private key. //