github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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 25 "github.com/ethereumproject/go-ethereum/accounts" 26 "github.com/ethereumproject/go-ethereum/common" 27 "github.com/ethereumproject/go-ethereum/core/types" 28 "github.com/ethereumproject/go-ethereum/crypto" 29 ) 30 31 // NewTransactor is a utility method to easily create a transaction signer from 32 // an encrypted json key stream and the associated passphrase. 33 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 34 json, err := ioutil.ReadAll(keyin) 35 if err != nil { 36 return nil, err 37 } 38 39 key, err := accounts.Web3PrivateKey(json, passphrase) 40 if err != nil { 41 return nil, err 42 } 43 44 return NewKeyedTransactor(key), nil 45 } 46 47 // NewKeyedTransactor is a utility method to easily create a transaction signer 48 // from a single private key. 49 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 50 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 51 return &TransactOpts{ 52 From: keyAddr, 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.WithSigner(signer).WithSignature(signature) 62 }, 63 } 64 }