github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/accounts/abi/bind/auth.go (about)

     1  package bind
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  	"io"
     7  	"io/ioutil"
     8  
     9  	"github.com/neatio-net/neatio/chain/accounts"
    10  	"github.com/neatio-net/neatio/chain/accounts/keystore"
    11  	"github.com/neatio-net/neatio/chain/core/types"
    12  	"github.com/neatio-net/neatio/utilities/common"
    13  	"github.com/neatio-net/neatio/utilities/crypto"
    14  )
    15  
    16  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    17  	json, err := ioutil.ReadAll(keyin)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	key, err := keystore.DecryptKey(json, passphrase)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return NewKeyedTransactor(key.PrivateKey), nil
    26  }
    27  
    28  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    29  	return &TransactOpts{
    30  		From: account.Address,
    31  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    32  			if address != account.Address {
    33  				return nil, errors.New("not authorized to sign this account")
    34  			}
    35  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    36  			if err != nil {
    37  				return nil, err
    38  			}
    39  			return tx.WithSignature(signer, signature)
    40  		},
    41  	}, nil
    42  }
    43  
    44  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    45  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    46  	return &TransactOpts{
    47  		From: keyAddr,
    48  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    49  			if address != keyAddr {
    50  				return nil, errors.New("not authorized to sign this account")
    51  			}
    52  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    53  			if err != nil {
    54  				return nil, err
    55  			}
    56  			return tx.WithSignature(signer, signature)
    57  		},
    58  	}
    59  }