github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/intfoundation/intchain/accounts"
    26  	"github.com/intfoundation/intchain/accounts/keystore"
    27  	"github.com/intfoundation/intchain/common"
    28  	"github.com/intfoundation/intchain/core/types"
    29  	"github.com/intfoundation/intchain/crypto"
    30  )
    31  
    32  // NewTransactor is a utility method to easily create a transaction signer from
    33  // an encrypted json key stream and the associated passphrase.
    34  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    35  	json, err := ioutil.ReadAll(keyin)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	key, err := keystore.DecryptKey(json, passphrase)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return NewKeyedTransactor(key.PrivateKey), nil
    44  }
    45  
    46  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    47  // an decrypted key from a keystore
    48  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    49  	return &TransactOpts{
    50  		From: account.Address,
    51  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    52  			if address != account.Address {
    53  				return nil, errors.New("not authorized to sign this account")
    54  			}
    55  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    56  			if err != nil {
    57  				return nil, err
    58  			}
    59  			return tx.WithSignature(signer, signature)
    60  		},
    61  	}, nil
    62  }
    63  
    64  // NewKeyedTransactor is a utility method to easily create a transaction signer
    65  // from a single private key.
    66  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    67  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    68  	return &TransactOpts{
    69  		From: keyAddr,
    70  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    71  			if address != keyAddr {
    72  				return nil, errors.New("not authorized to sign this account")
    73  			}
    74  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    75  			if err != nil {
    76  				return nil, err
    77  			}
    78  			return tx.WithSignature(signer, signature)
    79  		},
    80  	}
    81  }