github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/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/quickchainproject/quickchain/accounts/keystore" 10 "github.com/quickchainproject/quickchain/common" 11 "github.com/quickchainproject/quickchain/core/types" 12 "github.com/quickchainproject/quickchain/crypto" 13 ) 14 15 // NewTransactor is a utility method to easily create a transaction signer from 16 // an encrypted json key stream and the associated passphrase. 17 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 18 json, err := ioutil.ReadAll(keyin) 19 if err != nil { 20 return nil, err 21 } 22 key, err := keystore.DecryptKey(json, passphrase) 23 if err != nil { 24 return nil, err 25 } 26 return NewKeyedTransactor(key.PrivateKey), nil 27 } 28 29 // NewKeyedTransactor is a utility method to easily create a transaction signer 30 // from a single private key. 31 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 32 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 33 return &TransactOpts{ 34 From: keyAddr, 35 Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { 36 if address != keyAddr { 37 return nil, errors.New("not authorized to sign this account") 38 } 39 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 40 if err != nil { 41 return nil, err 42 } 43 return tx.WithSignature(signer, signature) 44 }, 45 } 46 }