github.com/codingfuture/orig-energi3@v0.8.4/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/ethereum/go-ethereum/accounts/keystore" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/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 key, err := keystore.DecryptKey(json, passphrase) 39 if err != nil { 40 return nil, err 41 } 42 return NewKeyedTransactor(key.PrivateKey), nil 43 } 44 45 // NewKeyedTransactor is a utility method to easily create a transaction signer 46 // from a single private key. 47 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 48 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 49 return &TransactOpts{ 50 From: keyAddr, 51 Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { 52 if address != keyAddr { 53 return nil, errors.New("not authorized to sign this account") 54 } 55 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 56 if err != nil { 57 return nil, err 58 } 59 return tx.WithSignature(signer, signature) 60 }, 61 } 62 }