github.com/klaytn/klaytn@v1.12.1/accounts/abi/bind/auth.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from accounts/abi/bind/auth.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package bind
    22  
    23  import (
    24  	"crypto/ecdsa"
    25  	"errors"
    26  	"io"
    27  	"math/big"
    28  
    29  	"github.com/klaytn/klaytn/accounts"
    30  	"github.com/klaytn/klaytn/accounts/keystore"
    31  	"github.com/klaytn/klaytn/blockchain/types"
    32  	"github.com/klaytn/klaytn/common"
    33  	"github.com/klaytn/klaytn/crypto"
    34  )
    35  
    36  // NewTransactor is a utility method to easily create a transaction signer from
    37  // an encrypted json key stream and the associated passphrase.
    38  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    39  	json, err := io.ReadAll(keyin)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	key, err := keystore.DecryptKey(json, passphrase)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return NewKeyedTransactor(key.GetPrivateKey()), nil
    48  }
    49  
    50  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    51  // an decrypted key from a keystore
    52  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    53  	return &TransactOpts{
    54  		From: account.Address,
    55  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    56  			if address != account.Address {
    57  				return nil, errors.New("not authorized to sign this account")
    58  			}
    59  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    60  			if err != nil {
    61  				return nil, err
    62  			}
    63  			return tx.WithSignature(signer, signature)
    64  		},
    65  	}, nil
    66  }
    67  
    68  // NewKeyedTransactor is a utility method to easily create a transaction signer
    69  // from a single private key.
    70  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    71  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    72  	return &TransactOpts{
    73  		From: keyAddr,
    74  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    75  			if address != keyAddr {
    76  				return nil, errors.New("not authorized to sign this account")
    77  			}
    78  			return types.SignTx(tx, signer, key)
    79  		},
    80  	}
    81  }
    82  
    83  // TODO-klaytn: clef related code
    84  /*
    85  // NewClefTransactor is a utility method to easily create a transaction signer
    86  // with a clef backend.
    87  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
    88  	return &TransactOpts{
    89  		From: account.Address,
    90  		Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
    91  			if address != account.Address {
    92  				return nil, errors.New("not authorized to sign this account")
    93  			}
    94  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
    95  		},
    96  	}
    97  }
    98  */
    99  
   100  // NewKeyedTransactorWithKeystore is a utility method to easily create a transaction signer
   101  // from a keystore wallet.
   102  func NewKeyedTransactorWithKeystore(address common.Address, ks *keystore.KeyStore, chainID *big.Int) *TransactOpts {
   103  	keyAddr := address
   104  	return &TransactOpts{
   105  		From: keyAddr,
   106  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   107  			if address != keyAddr {
   108  				return nil, errors.New("not authorized to sign this account")
   109  			}
   110  			account := accounts.Account{Address: address}
   111  			return ks.SignTx(account, tx, chainID)
   112  		},
   113  	}
   114  }
   115  
   116  // MakeTransactOpts creates a transaction signer with nonce, gasLimit, and gasPrice from a single private key.
   117  func MakeTransactOpts(accountKey *ecdsa.PrivateKey, nonce *big.Int, gasLimit uint64, gasPrice *big.Int) *TransactOpts {
   118  	if accountKey == nil {
   119  		return nil
   120  	}
   121  	auth := NewKeyedTransactor(accountKey)
   122  	auth.GasLimit = gasLimit
   123  	auth.GasPrice = gasPrice
   124  	auth.Nonce = nonce
   125  	return auth
   126  }
   127  
   128  // MakeTransactOptsWithKeystore creates a transaction signer with nonce, gasLimit, and gasPrice from a keystore wallet.
   129  func MakeTransactOptsWithKeystore(ks *keystore.KeyStore, from common.Address, nonce *big.Int, chainID *big.Int, gasLimit uint64, gasPrice *big.Int) *TransactOpts {
   130  	if ks == nil {
   131  		return nil
   132  	}
   133  
   134  	auth := NewKeyedTransactorWithKeystore(from, ks, chainID)
   135  	auth.GasLimit = gasLimit
   136  	auth.GasPrice = gasPrice
   137  	auth.Nonce = nonce
   138  	return auth
   139  }