github.com/theQRL/go-zond@v0.2.1/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  	"context"
    21  	"errors"
    22  	"io"
    23  	"math/big"
    24  
    25  	"github.com/theQRL/go-qrllib/dilithium"
    26  	"github.com/theQRL/go-zond/accounts"
    27  	"github.com/theQRL/go-zond/accounts/external"
    28  	"github.com/theQRL/go-zond/accounts/keystore"
    29  	"github.com/theQRL/go-zond/common"
    30  	"github.com/theQRL/go-zond/core/types"
    31  	"github.com/theQRL/go-zond/crypto/pqcrypto"
    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  // NewTransactorWithChainID is a utility method to easily create a transaction signer from
    41  // an encrypted json key stream and the associated passphrase.
    42  func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
    43  	json, err := io.ReadAll(keyin)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	key, err := keystore.DecryptKey(json, passphrase)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return NewKeyedTransactorWithChainID(key.Dilithium, chainID)
    52  }
    53  
    54  // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
    55  // an decrypted key from a keystore.
    56  func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
    57  	if chainID == nil {
    58  		return nil, ErrNoChainID
    59  	}
    60  	signer := types.LatestSignerForChainID(chainID)
    61  	return &TransactOpts{
    62  		From: account.Address,
    63  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    64  			if address != account.Address {
    65  				return nil, ErrNotAuthorized
    66  			}
    67  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  			pk, err := keystore.GetPublicKey(account)
    72  			if err != nil {
    73  				return nil, err
    74  			}
    75  			return tx.WithSignatureAndPublicKey(signer, signature, pk)
    76  		},
    77  		Context: context.Background(),
    78  	}, nil
    79  }
    80  
    81  // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
    82  // from a single private key.
    83  func NewKeyedTransactorWithChainID(d *dilithium.Dilithium, chainID *big.Int) (*TransactOpts, error) {
    84  	keyAddr := d.GetAddress()
    85  	if chainID == nil {
    86  		return nil, ErrNoChainID
    87  	}
    88  	signer := types.LatestSignerForChainID(chainID)
    89  	return &TransactOpts{
    90  		From: keyAddr,
    91  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    92  			if address != keyAddr {
    93  				return nil, ErrNotAuthorized
    94  			}
    95  			signature, err := pqcrypto.Sign(signer.Hash(tx).Bytes(), d)
    96  			if err != nil {
    97  				return nil, err
    98  			}
    99  			pk := d.GetPK()
   100  			return tx.WithSignatureAndPublicKey(signer, signature, pk[:])
   101  		},
   102  		Context: context.Background(),
   103  	}, nil
   104  }
   105  
   106  // NewClefTransactor is a utility method to easily create a transaction signer
   107  // with a clef backend.
   108  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
   109  	return &TransactOpts{
   110  		From: account.Address,
   111  		Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
   112  			if address != account.Address {
   113  				return nil, ErrNotAuthorized
   114  			}
   115  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
   116  		},
   117  		Context: context.Background(),
   118  	}
   119  }