github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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/kisexp/xdchain/accounts" 27 "github.com/kisexp/xdchain/accounts/external" 28 "github.com/kisexp/xdchain/accounts/keystore" 29 "github.com/kisexp/xdchain/common" 30 "github.com/kisexp/xdchain/core/types" 31 "github.com/kisexp/xdchain/crypto" 32 "github.com/kisexp/xdchain/log" 33 ) 34 35 // ErrNoChainID is returned whenever the user failed to specify a chain id. 36 var ErrNoChainID = errors.New("no chain id specified") 37 38 // ErrNotAuthorized is returned when an account is not properly unlocked. 39 var ErrNotAuthorized = errors.New("not authorized to sign this account") 40 41 // NewTransactor is a utility method to easily create a transaction signer from 42 // an encrypted json key stream and the associated passphrase. 43 // 44 // Deprecated: Use NewTransactorWithChainID instead. 45 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 46 log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") 47 json, err := ioutil.ReadAll(keyin) 48 if err != nil { 49 return nil, err 50 } 51 key, err := keystore.DecryptKey(json, passphrase) 52 if err != nil { 53 return nil, err 54 } 55 return NewKeyedTransactor(key.PrivateKey), nil 56 } 57 58 // NewKeyStoreTransactor is a utility method to easily create a transaction signer from 59 // an decrypted key from a keystore. 60 // 61 // Deprecated: Use NewKeyStoreTransactorWithChainID instead. 62 func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { 63 log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID") 64 var homesteadSigner types.Signer = types.HomesteadSigner{} 65 return &TransactOpts{ 66 From: account.Address, 67 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 68 if address != account.Address { 69 return nil, ErrNotAuthorized 70 } 71 // Quorum 72 signer := homesteadSigner 73 if tx.IsPrivate() { 74 signer = types.QuorumPrivateTxSigner{} 75 } 76 // / Quorum 77 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 78 if err != nil { 79 return nil, err 80 } 81 return tx.WithSignature(signer, signature) 82 }, 83 }, nil 84 } 85 86 // NewKeyedTransactor is a utility method to easily create a transaction signer 87 // from a single private key. 88 // 89 // Deprecated: Use NewKeyedTransactorWithChainID instead. 90 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 91 log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") 92 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 93 var homesteadSigner types.Signer = types.HomesteadSigner{} 94 return &TransactOpts{ 95 From: keyAddr, 96 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 97 if address != keyAddr { 98 return nil, ErrNotAuthorized 99 } 100 // Quorum 101 signer := homesteadSigner 102 if tx.IsPrivate() { 103 signer = types.QuorumPrivateTxSigner{} 104 } 105 // / Quorum 106 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 107 if err != nil { 108 return nil, err 109 } 110 return tx.WithSignature(signer, signature) 111 }, 112 } 113 } 114 115 // NewTransactorWithChainID is a utility method to easily create a transaction signer from 116 // an encrypted json key stream and the associated passphrase. 117 func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { 118 json, err := ioutil.ReadAll(keyin) 119 if err != nil { 120 return nil, err 121 } 122 key, err := keystore.DecryptKey(json, passphrase) 123 if err != nil { 124 return nil, err 125 } 126 return NewKeyedTransactorWithChainID(key.PrivateKey, chainID) 127 } 128 129 // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from 130 // an decrypted key from a keystore. 131 func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { 132 if chainID == nil { 133 return nil, ErrNoChainID 134 } 135 var eipo155Signer types.Signer = types.NewEIP155Signer(chainID) 136 return &TransactOpts{ 137 From: account.Address, 138 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 139 if address != account.Address { 140 return nil, ErrNotAuthorized 141 } 142 // Quorum 143 signer := eipo155Signer 144 if tx.IsPrivate() { 145 signer = types.QuorumPrivateTxSigner{} 146 } 147 // / Quorum 148 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 149 if err != nil { 150 return nil, err 151 } 152 return tx.WithSignature(signer, signature) 153 }, 154 }, nil 155 } 156 157 // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer 158 // from a single private key. 159 func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) { 160 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 161 if chainID == nil { 162 return nil, ErrNoChainID 163 } 164 var eipo155Signer types.Signer = types.NewEIP155Signer(chainID) 165 return &TransactOpts{ 166 From: keyAddr, 167 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 168 if address != keyAddr { 169 return nil, ErrNotAuthorized 170 } 171 // Quorum 172 signer := eipo155Signer 173 if tx.IsPrivate() { 174 signer = types.QuorumPrivateTxSigner{} 175 } 176 // / Quorum 177 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 178 if err != nil { 179 return nil, err 180 } 181 return tx.WithSignature(signer, signature) 182 }, 183 }, nil 184 } 185 186 // NewClefTransactor is a utility method to easily create a transaction signer 187 // with a clef backend. 188 func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { 189 return &TransactOpts{ 190 From: account.Address, 191 Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { 192 if address != account.Address { 193 return nil, ErrNotAuthorized 194 } 195 return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id 196 }, 197 } 198 } 199 200 // Quorum 201 // 202 // NewWalletTransactor is a utility method to easily create a transaction signer 203 // from a wallet account 204 func NewWalletTransactor(w accounts.Wallet, account accounts.Account) *TransactOpts { 205 return &TransactOpts{ 206 From: account.Address, 207 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 208 if address != account.Address { 209 return nil, errors.New("not authorized to sign this account") 210 } 211 return w.SignTx(account, tx, nil) // homestead signer without chainID is backward compatible 212 }, 213 } 214 }