github.com/bcskill/bcschain/v3@v3.4.9-beta2/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  	"crypto/ecdsa"
    21  	"errors"
    22  	"io"
    23  	"io/ioutil"
    24  
    25  	"github.com/bcskill/bcschain/v3/accounts"
    26  	"github.com/bcskill/bcschain/v3/accounts/external"
    27  	"github.com/bcskill/bcschain/v3/accounts/keystore"
    28  	"github.com/bcskill/bcschain/v3/common"
    29  	"github.com/bcskill/bcschain/v3/core/types"
    30  	"github.com/bcskill/bcschain/v3/crypto"
    31  	"github.com/bcskill/bcschain/v3/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 := ioutil.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  	}, nil
    77  }
    78  
    79  // NewKeyedTransactor is a utility method to easily create a transaction signer
    80  // from a single private key.
    81  //
    82  // Deprecated: Use NewKeyedTransactorWithChainID instead.
    83  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    84  	log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
    85  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    86  	signer := types.HomesteadSigner{}
    87  	return &TransactOpts{
    88  		From: keyAddr,
    89  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    90  			if address != keyAddr {
    91  				return nil, ErrNotAuthorized
    92  			}
    93  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    94  			if err != nil {
    95  				return nil, err
    96  			}
    97  			return tx.WithSignature(signer, signature)
    98  		},
    99  	}
   100  }
   101  
   102  // NewClefTransactor is a utility method to easily create a transaction signer
   103  // with a clef backend.
   104  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
   105  	return &TransactOpts{
   106  		From: account.Address,
   107  		Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
   108  			if address != account.Address {
   109  				return nil, ErrNotAuthorized
   110  			}
   111  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
   112  		},
   113  	}
   114  }