github.com/0xsequence/ethkit@v1.25.0/go-ethereum/accounts/abi/bind/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  	"io"
    24  	"math/big"
    25  
    26  	"github.com/0xsequence/ethkit/go-ethereum/accounts"
    27  	"github.com/0xsequence/ethkit/go-ethereum/accounts/keystore"
    28  	"github.com/0xsequence/ethkit/go-ethereum/common"
    29  	"github.com/0xsequence/ethkit/go-ethereum/core/types"
    30  	"github.com/0xsequence/ethkit/go-ethereum/crypto"
    31  	"github.com/0xsequence/ethkit/go-ethereum/log"
    32  )
    33  
    34  // ErrNoChainID is returned whenever the user failed to specify a chain id.
    35  var ErrNoChainID = errors.New("no chain id specified")
    36  
    37  // ErrNotAuthorized is returned when an account is not properly unlocked.
    38  var ErrNotAuthorized = errors.New("not authorized to sign this account")
    39  
    40  // NewTransactor is a utility method to easily create a transaction signer from
    41  // an encrypted json key stream and the associated passphrase.
    42  //
    43  // Deprecated: Use NewTransactorWithChainID instead.
    44  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    45  	log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
    46  	json, err := io.ReadAll(keyin)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	key, err := keystore.DecryptKey(json, passphrase)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return NewKeyedTransactor(key.PrivateKey), nil
    55  }
    56  
    57  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    58  // an decrypted key from a keystore.
    59  //
    60  // Deprecated: Use NewKeyStoreTransactorWithChainID instead.
    61  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    62  	log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
    63  	signer := types.HomesteadSigner{}
    64  	return &TransactOpts{
    65  		From: account.Address,
    66  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    67  			if address != account.Address {
    68  				return nil, ErrNotAuthorized
    69  			}
    70  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    71  			if err != nil {
    72  				return nil, err
    73  			}
    74  			return tx.WithSignature(signer, signature)
    75  		},
    76  		Context: context.Background(),
    77  	}, nil
    78  }
    79  
    80  // NewKeyedTransactor is a utility method to easily create a transaction signer
    81  // from a single private key.
    82  //
    83  // Deprecated: Use NewKeyedTransactorWithChainID instead.
    84  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    85  	log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
    86  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    87  	signer := types.HomesteadSigner{}
    88  	return &TransactOpts{
    89  		From: keyAddr,
    90  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    91  			if address != keyAddr {
    92  				return nil, ErrNotAuthorized
    93  			}
    94  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    95  			if err != nil {
    96  				return nil, err
    97  			}
    98  			return tx.WithSignature(signer, signature)
    99  		},
   100  		Context: context.Background(),
   101  	}
   102  }
   103  
   104  // NewTransactorWithChainID is a utility method to easily create a transaction signer from
   105  // an encrypted json key stream and the associated passphrase.
   106  func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
   107  	json, err := io.ReadAll(keyin)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	key, err := keystore.DecryptKey(json, passphrase)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
   116  }
   117  
   118  // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
   119  // an decrypted key from a keystore.
   120  func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
   121  	if chainID == nil {
   122  		return nil, ErrNoChainID
   123  	}
   124  	signer := types.LatestSignerForChainID(chainID)
   125  	return &TransactOpts{
   126  		From: account.Address,
   127  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   128  			if address != account.Address {
   129  				return nil, ErrNotAuthorized
   130  			}
   131  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
   132  			if err != nil {
   133  				return nil, err
   134  			}
   135  			return tx.WithSignature(signer, signature)
   136  		},
   137  		Context: context.Background(),
   138  	}, nil
   139  }
   140  
   141  // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
   142  // from a single private key.
   143  func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
   144  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
   145  	if chainID == nil {
   146  		return nil, ErrNoChainID
   147  	}
   148  	signer := types.LatestSignerForChainID(chainID)
   149  	return &TransactOpts{
   150  		From: keyAddr,
   151  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   152  			if address != keyAddr {
   153  				return nil, ErrNotAuthorized
   154  			}
   155  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
   156  			if err != nil {
   157  				return nil, err
   158  			}
   159  			return tx.WithSignature(signer, signature)
   160  		},
   161  		Context: context.Background(),
   162  	}, nil
   163  }