github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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  	"math/big"
    25  
    26  	"github.com/vntchain/go-vnt/accounts/keystore"
    27  	"github.com/vntchain/go-vnt/common"
    28  	"github.com/vntchain/go-vnt/core/types"
    29  	"github.com/vntchain/go-vnt/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, chainID *big.Int) (*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, chainID), nil
    44  }
    45  
    46  // NewKeyedTransactor is a utility method to easily create a transaction signer
    47  // from a single private key.
    48  func NewKeyedTransactor(key *ecdsa.PrivateKey, chainID *big.Int) *TransactOpts {
    49  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
    50  	return &TransactOpts{
    51  		From:    keyAddr,
    52  		ChainID: chainID,
    53  		Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    54  			if address != keyAddr {
    55  				return nil, errors.New("not authorized to sign this account")
    56  			}
    57  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
    58  			if err != nil {
    59  				return nil, err
    60  			}
    61  			return tx.WithSignature(signer, signature)
    62  		},
    63  	}
    64  }