github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/bind/v2/auth.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bind
    18  
    19  import (
    20  	"context"
    21  	"crypto/ecdsa"
    22  	"errors"
    23  	"math/big"
    24  
    25  	"github.com/ethereum/go-ethereum/accounts"
    26  	"github.com/ethereum/go-ethereum/accounts/external"
    27  	"github.com/ethereum/go-ethereum/accounts/keystore"
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/crypto"
    31  )
    32  
    33  // ErrNotAuthorized is returned when an account is not properly unlocked.
    34  var ErrNotAuthorized = errors.New("not authorized to sign this account")
    35  
    36  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    37  // a decrypted key from a keystore.
    38  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) *TransactOpts {
    39  	if chainID == nil {
    40  		panic("nil chainID")
    41  	}
    42  	signer := types.LatestSignerForChainID(chainID)
    43  	return &TransactOpts{
    44  		From: account.Address,
    45  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    46  			if address != account.Address {
    47  				return nil, ErrNotAuthorized
    48  			}
    49  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    50  			if err != nil {
    51  				return nil, err
    52  			}
    53  			return tx.WithSignature(signer, signature)
    54  		},
    55  		Context: context.Background(),
    56  	}
    57  }
    58  
    59  // NewKeyedTransactor is a utility method to easily create a transaction signer
    60  // from a single private key.
    61  func NewKeyedTransactor(key *ecdsa.PrivateKey, chainID *big.Int) *TransactOpts {
    62  	if chainID == nil {
    63  		panic("nil chainID")
    64  	}
    65  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    66  	signer := types.LatestSignerForChainID(chainID)
    67  	return &TransactOpts{
    68  		From: keyAddr,
    69  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    70  			if address != keyAddr {
    71  				return nil, ErrNotAuthorized
    72  			}
    73  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    74  			if err != nil {
    75  				return nil, err
    76  			}
    77  			return tx.WithSignature(signer, signature)
    78  		},
    79  		Context: context.Background(),
    80  	}
    81  }
    82  
    83  // NewClefTransactor is a utility method to easily create a transaction signer
    84  // with a clef backend.
    85  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
    86  	return &TransactOpts{
    87  		From: account.Address,
    88  		Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
    89  			if address != account.Address {
    90  				return nil, ErrNotAuthorized
    91  			}
    92  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
    93  		},
    94  		Context: context.Background(),
    95  	}
    96  }