github.com/core-coin/go-core/v2@v2.1.9/accounts/abi/bind/auth.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bind
    18  
    19  import (
    20  	"errors"
    21  	"io"
    22  	"io/ioutil"
    23  	"math/big"
    24  
    25  	"github.com/core-coin/go-core/v2/accounts"
    26  	"github.com/core-coin/go-core/v2/accounts/external"
    27  	"github.com/core-coin/go-core/v2/accounts/keystore"
    28  	"github.com/core-coin/go-core/v2/common"
    29  	"github.com/core-coin/go-core/v2/core/types"
    30  	"github.com/core-coin/go-core/v2/crypto"
    31  )
    32  
    33  // ErrNoNetworkID is returned whenever the user failed to specify a network id.
    34  var ErrNoNetworkID = errors.New("no network id specified")
    35  
    36  // ErrNotAuthorized is returned when an account is not properly unlocked.
    37  var ErrNotAuthorized = errors.New("not authorized to sign this account")
    38  
    39  // NewTransactorWithNetworkID is a utility method to easily create a transaction signer from
    40  // an encrypted json key stream and the associated passphrase.
    41  func NewTransactorWithNetworkID(keyin io.Reader, passphrase string, networkID *big.Int) (*TransactOpts, error) {
    42  	json, err := ioutil.ReadAll(keyin)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	key, err := keystore.DecryptKey(json, passphrase)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return NewKeyedTransactorWithNetworkID(key.PrivateKey, networkID)
    51  }
    52  
    53  // NewKeyStoreTransactorWithNetworkID is a utility method to easily create a transaction signer from
    54  // an decrypted key from a keystore.
    55  func NewKeyStoreTransactorWithNetworkID(keystore *keystore.KeyStore, account accounts.Account, networkID *big.Int) (*TransactOpts, error) {
    56  	if networkID == nil {
    57  		return nil, ErrNoNetworkID
    58  	}
    59  	signer := types.NewNucleusSigner(networkID)
    60  	return &TransactOpts{
    61  		From: account.Address,
    62  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    63  			if address != account.Address {
    64  				return nil, ErrNotAuthorized
    65  			}
    66  			tx.SetNetworkID(uint(signer.NetworkID()))
    67  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  			return tx.WithSignature(signer, signature)
    72  		},
    73  	}, nil
    74  }
    75  
    76  // NewKeyedTransactorWithNetworkID is a utility method to easily create a transaction signer
    77  // from a single private key.
    78  func NewKeyedTransactorWithNetworkID(key *crypto.PrivateKey, networkID *big.Int) (*TransactOpts, error) {
    79  	if networkID == nil {
    80  		return nil, ErrNoNetworkID
    81  	}
    82  	signer := types.NewNucleusSigner(networkID)
    83  	return &TransactOpts{
    84  		From: key.Address(),
    85  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    86  			if address != key.Address() {
    87  				return nil, ErrNotAuthorized
    88  			}
    89  			tx.SetNetworkID(uint(signer.NetworkID()))
    90  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    91  			if err != nil {
    92  				return nil, err
    93  			}
    94  			return tx.WithSignature(signer, signature)
    95  		},
    96  	}, nil
    97  }
    98  
    99  // NewClefTransactor is a utility method to easily create a transaction signer
   100  // with a clef backend.
   101  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
   102  	return &TransactOpts{
   103  		From: account.Address,
   104  		Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
   105  			if address != account.Address {
   106  				return nil, ErrNotAuthorized
   107  			}
   108  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
   109  		},
   110  	}
   111  }