github.com/poctek/mev-geth@v1.9.7/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" 26 "github.com/ethereum/go-ethereum/accounts/external" 27 "github.com/ethereum/go-ethereum/accounts/keystore" 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/core/types" 30 "github.com/ethereum/go-ethereum/crypto" 31 ) 32 33 // NewTransactor is a utility method to easily create a transaction signer from 34 // an encrypted json key stream and the associated passphrase. 35 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 36 json, err := ioutil.ReadAll(keyin) 37 if err != nil { 38 return nil, err 39 } 40 key, err := keystore.DecryptKey(json, passphrase) 41 if err != nil { 42 return nil, err 43 } 44 return NewKeyedTransactor(key.PrivateKey), nil 45 } 46 47 // NewKeyStoreTransactor is a utility method to easily create a transaction signer from 48 // an decrypted key from a keystore 49 func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { 50 return &TransactOpts{ 51 From: account.Address, 52 Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { 53 if address != account.Address { 54 return nil, errors.New("not authorized to sign this account") 55 } 56 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 57 if err != nil { 58 return nil, err 59 } 60 return tx.WithSignature(signer, signature) 61 }, 62 }, nil 63 } 64 65 // NewKeyedTransactor is a utility method to easily create a transaction signer 66 // from a single private key. 67 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 68 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 69 return &TransactOpts{ 70 From: keyAddr, 71 Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) { 72 if address != keyAddr { 73 return nil, errors.New("not authorized to sign this account") 74 } 75 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 76 if err != nil { 77 return nil, err 78 } 79 return tx.WithSignature(signer, signature) 80 }, 81 } 82 } 83 84 // NewClefTransactor is a utility method to easily create a transaction signer 85 // with a clef backend. 86 func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { 87 return &TransactOpts{ 88 From: account.Address, 89 Signer: func(signer types.Signer, address common.Address, transaction *types.Transaction) (*types.Transaction, error) { 90 if address != account.Address { 91 return nil, errors.New("not authorized to sign this account") 92 } 93 return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id 94 }, 95 } 96 }