github.com/klaytn/klaytn@v1.10.2/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  	"io/ioutil"
    28  	"math/big"
    29  
    30  	"github.com/klaytn/klaytn/accounts"
    31  	"github.com/klaytn/klaytn/accounts/keystore"
    32  	"github.com/klaytn/klaytn/blockchain/types"
    33  	"github.com/klaytn/klaytn/common"
    34  	"github.com/klaytn/klaytn/crypto"
    35  )
    36  
    37  // NewTransactor is a utility method to easily create a transaction signer from
    38  // an encrypted json key stream and the associated passphrase.
    39  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    40  	json, err := ioutil.ReadAll(keyin)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	key, err := keystore.DecryptKey(json, passphrase)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return NewKeyedTransactor(key.GetPrivateKey()), nil
    49  }
    50  
    51  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    52  // an decrypted key from a keystore
    53  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    54  	return &TransactOpts{
    55  		From: account.Address,
    56  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    57  			if address != account.Address {
    58  				return nil, errors.New("not authorized to sign this account")
    59  			}
    60  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    61  			if err != nil {
    62  				return nil, err
    63  			}
    64  			return tx.WithSignature(signer, signature)
    65  		},
    66  	}, nil
    67  }
    68  
    69  // NewKeyedTransactor is a utility method to easily create a transaction signer
    70  // from a single private key.
    71  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    72  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    73  	return &TransactOpts{
    74  		From: keyAddr,
    75  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    76  			if address != keyAddr {
    77  				return nil, errors.New("not authorized to sign this account")
    78  			}
    79  			return types.SignTx(tx, signer, key)
    80  		},
    81  	}
    82  }
    83  
    84  // TODO-klaytn: clef related code
    85  /*
    86  // NewClefTransactor is a utility method to easily create a transaction signer
    87  // with a clef backend.
    88  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
    89  	return &TransactOpts{
    90  		From: account.Address,
    91  		Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
    92  			if address != account.Address {
    93  				return nil, errors.New("not authorized to sign this account")
    94  			}
    95  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
    96  		},
    97  	}
    98  }
    99  */
   100  
   101  // NewKeyedTransactorWithKeystore is a utility method to easily create a transaction signer
   102  // from a keystore wallet.
   103  func NewKeyedTransactorWithKeystore(address common.Address, ks *keystore.KeyStore, chainID *big.Int) *TransactOpts {
   104  	keyAddr := address
   105  	return &TransactOpts{
   106  		From: keyAddr,
   107  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   108  			if address != keyAddr {
   109  				return nil, errors.New("not authorized to sign this account")
   110  			}
   111  			account := accounts.Account{Address: address}
   112  			return ks.SignTx(account, tx, chainID)
   113  		},
   114  	}
   115  }
   116  
   117  // MakeTransactOpts creates a transaction signer with nonce, gasLimit, and gasPrice from a single private key.
   118  func MakeTransactOpts(accountKey *ecdsa.PrivateKey, nonce *big.Int, gasLimit uint64, gasPrice *big.Int) *TransactOpts {
   119  	if accountKey == nil {
   120  		return nil
   121  	}
   122  	auth := NewKeyedTransactor(accountKey)
   123  	auth.GasLimit = gasLimit
   124  	auth.GasPrice = gasPrice
   125  	auth.Nonce = nonce
   126  	return auth
   127  }
   128  
   129  // MakeTransactOptsWithKeystore creates a transaction signer with nonce, gasLimit, and gasPrice from a keystore wallet.
   130  func MakeTransactOptsWithKeystore(ks *keystore.KeyStore, from common.Address, nonce *big.Int, chainID *big.Int, gasLimit uint64, gasPrice *big.Int) *TransactOpts {
   131  	if ks == nil {
   132  		return nil
   133  	}
   134  
   135  	auth := NewKeyedTransactorWithKeystore(from, ks, chainID)
   136  	auth.GasLimit = gasLimit
   137  	auth.GasPrice = gasPrice
   138  	auth.Nonce = nonce
   139  	return auth
   140  }