github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/accounts/abi/bind/auth.go (about)

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