github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/bind/auth.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package bind
    28  
    29  import (
    30  	"context"
    31  	"crypto/ecdsa"
    32  	"errors"
    33  	"io"
    34  	"math/big"
    35  
    36  	"github.com/ava-labs/subnet-evm/accounts"
    37  	"github.com/ava-labs/subnet-evm/accounts/external"
    38  	"github.com/ava-labs/subnet-evm/accounts/keystore"
    39  	"github.com/ava-labs/subnet-evm/core/types"
    40  	"github.com/ethereum/go-ethereum/common"
    41  	"github.com/ethereum/go-ethereum/crypto"
    42  	"github.com/ethereum/go-ethereum/log"
    43  )
    44  
    45  // ErrNoChainID is returned whenever the user failed to specify a chain id.
    46  var ErrNoChainID = errors.New("no chain id specified")
    47  
    48  // ErrNotAuthorized is returned when an account is not properly unlocked.
    49  var ErrNotAuthorized = errors.New("not authorized to sign this account")
    50  
    51  // NewTransactor is a utility method to easily create a transaction signer from
    52  // an encrypted json key stream and the associated passphrase.
    53  //
    54  // Deprecated: Use NewTransactorWithChainID instead.
    55  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    56  	log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
    57  	json, err := io.ReadAll(keyin)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	key, err := keystore.DecryptKey(json, passphrase)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return NewKeyedTransactor(key.PrivateKey), nil
    66  }
    67  
    68  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    69  // an decrypted key from a keystore.
    70  //
    71  // Deprecated: Use NewKeyStoreTransactorWithChainID instead.
    72  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    73  	log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
    74  	signer := types.HomesteadSigner{}
    75  	return &TransactOpts{
    76  		From: account.Address,
    77  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    78  			if address != account.Address {
    79  				return nil, ErrNotAuthorized
    80  			}
    81  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    82  			if err != nil {
    83  				return nil, err
    84  			}
    85  			return tx.WithSignature(signer, signature)
    86  		},
    87  		Context: context.Background(),
    88  	}, nil
    89  }
    90  
    91  // NewKeyedTransactor is a utility method to easily create a transaction signer
    92  // from a single private key.
    93  //
    94  // Deprecated: Use NewKeyedTransactorWithChainID instead.
    95  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    96  	log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
    97  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    98  	signer := types.HomesteadSigner{}
    99  	return &TransactOpts{
   100  		From: keyAddr,
   101  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   102  			if address != keyAddr {
   103  				return nil, ErrNotAuthorized
   104  			}
   105  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
   106  			if err != nil {
   107  				return nil, err
   108  			}
   109  			return tx.WithSignature(signer, signature)
   110  		},
   111  		Context: context.Background(),
   112  	}
   113  }
   114  
   115  // NewTransactorWithChainID is a utility method to easily create a transaction signer from
   116  // an encrypted json key stream and the associated passphrase.
   117  func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
   118  	json, err := io.ReadAll(keyin)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  	key, err := keystore.DecryptKey(json, passphrase)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
   127  }
   128  
   129  // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
   130  // an decrypted key from a keystore.
   131  func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
   132  	if chainID == nil {
   133  		return nil, ErrNoChainID
   134  	}
   135  	signer := types.LatestSignerForChainID(chainID)
   136  	return &TransactOpts{
   137  		From: account.Address,
   138  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   139  			if address != account.Address {
   140  				return nil, ErrNotAuthorized
   141  			}
   142  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
   143  			if err != nil {
   144  				return nil, err
   145  			}
   146  			return tx.WithSignature(signer, signature)
   147  		},
   148  		Context: context.Background(),
   149  	}, nil
   150  }
   151  
   152  // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
   153  // from a single private key.
   154  func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
   155  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
   156  	if chainID == nil {
   157  		return nil, ErrNoChainID
   158  	}
   159  	signer := types.LatestSignerForChainID(chainID)
   160  	return &TransactOpts{
   161  		From: keyAddr,
   162  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   163  			if address != keyAddr {
   164  				return nil, ErrNotAuthorized
   165  			}
   166  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
   167  			if err != nil {
   168  				return nil, err
   169  			}
   170  			return tx.WithSignature(signer, signature)
   171  		},
   172  		Context: context.Background(),
   173  	}, nil
   174  }
   175  
   176  // NewClefTransactor is a utility method to easily create a transaction signer
   177  // with a clef backend.
   178  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
   179  	return &TransactOpts{
   180  		From: account.Address,
   181  		Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
   182  			if address != account.Address {
   183  				return nil, ErrNotAuthorized
   184  			}
   185  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
   186  		},
   187  		Context: context.Background(),
   188  	}
   189  }