github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/bind/auth.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bind
    19  
    20  import (
    21  	"crypto/ecdsa"
    22  	"errors"
    23  	"io"
    24  	"io/ioutil"
    25  
    26  	"github.com/AigarNetwork/aigar/accounts"
    27  	"github.com/AigarNetwork/aigar/accounts/external"
    28  	"github.com/AigarNetwork/aigar/accounts/keystore"
    29  	"github.com/AigarNetwork/aigar/common"
    30  	"github.com/AigarNetwork/aigar/core/types"
    31  	"github.com/AigarNetwork/aigar/crypto"
    32  )
    33  
    34  // NewTransactor is a utility method to easily create a transaction signer from
    35  // an encrypted json key stream and the associated passphrase.
    36  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    37  	json, err := ioutil.ReadAll(keyin)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	key, err := keystore.DecryptKey(json, passphrase)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return NewKeyedTransactor(key.PrivateKey), nil
    46  }
    47  
    48  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    49  // an decrypted key from a keystore
    50  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    51  	return &TransactOpts{
    52  		From: account.Address,
    53  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    54  			if address != account.Address {
    55  				return nil, errors.New("not authorized to sign this account")
    56  			}
    57  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  			return tx.WithSignature(signer, signature)
    62  		},
    63  	}, nil
    64  }
    65  
    66  // NewKeyedTransactor is a utility method to easily create a transaction signer
    67  // from a single private key.
    68  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
    69  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    70  	return &TransactOpts{
    71  		From: keyAddr,
    72  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    73  			if address != keyAddr {
    74  				return nil, errors.New("not authorized to sign this account")
    75  			}
    76  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    77  			if err != nil {
    78  				return nil, err
    79  			}
    80  			return tx.WithSignature(signer, signature)
    81  		},
    82  	}
    83  }
    84  
    85  // NewClefTransactor is a utility method to easily create a transaction signer
    86  // with a clef backend.
    87  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
    88  	return &TransactOpts{
    89  		From: account.Address,
    90  		Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
    91  			if address != account.Address {
    92  				return nil, errors.New("not authorized to sign this account")
    93  			}
    94  			return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
    95  		},
    96  	}
    97  }