gitee.com/liu-zhao234568/cntest@v1.0.0/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 "gitee.com/liu-zhao234568/cntest/accounts" 27 "gitee.com/liu-zhao234568/cntest/accounts/external" 28 "gitee.com/liu-zhao234568/cntest/accounts/keystore" 29 "gitee.com/liu-zhao234568/cntest/common" 30 "gitee.com/liu-zhao234568/cntest/core/types" 31 "gitee.com/liu-zhao234568/cntest/crypto" 32 "gitee.com/liu-zhao234568/cntest/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 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 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 72 if err != nil { 73 return nil, err 74 } 75 return tx.WithSignature(signer, signature) 76 }, 77 }, nil 78 } 79 80 // NewKeyedTransactor is a utility method to easily create a transaction signer 81 // from a single private key. 82 // 83 // Deprecated: Use NewKeyedTransactorWithChainID instead. 84 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 85 log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") 86 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 87 signer := types.HomesteadSigner{} 88 return &TransactOpts{ 89 From: keyAddr, 90 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 91 if address != keyAddr { 92 return nil, ErrNotAuthorized 93 } 94 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 95 if err != nil { 96 return nil, err 97 } 98 return tx.WithSignature(signer, signature) 99 }, 100 } 101 } 102 103 // NewTransactorWithChainID is a utility method to easily create a transaction signer from 104 // an encrypted json key stream and the associated passphrase. 105 func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { 106 json, err := ioutil.ReadAll(keyin) 107 if err != nil { 108 return nil, err 109 } 110 key, err := keystore.DecryptKey(json, passphrase) 111 if err != nil { 112 return nil, err 113 } 114 return NewKeyedTransactorWithChainID(key.PrivateKey, chainID) 115 } 116 117 // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from 118 // an decrypted key from a keystore. 119 func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { 120 if chainID == nil { 121 return nil, ErrNoChainID 122 } 123 signer := types.LatestSignerForChainID(chainID) 124 return &TransactOpts{ 125 From: account.Address, 126 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 127 if address != account.Address { 128 return nil, ErrNotAuthorized 129 } 130 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 131 if err != nil { 132 return nil, err 133 } 134 return tx.WithSignature(signer, signature) 135 }, 136 }, nil 137 } 138 139 // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer 140 // from a single private key. 141 func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) { 142 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 143 if chainID == nil { 144 return nil, ErrNoChainID 145 } 146 signer := types.LatestSignerForChainID(chainID) 147 return &TransactOpts{ 148 From: keyAddr, 149 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 150 if address != keyAddr { 151 return nil, ErrNotAuthorized 152 } 153 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 154 if err != nil { 155 return nil, err 156 } 157 return tx.WithSignature(signer, signature) 158 }, 159 }, nil 160 } 161 162 // NewClefTransactor is a utility method to easily create a transaction signer 163 // with a clef backend. 164 func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { 165 return &TransactOpts{ 166 From: account.Address, 167 Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { 168 if address != account.Address { 169 return nil, ErrNotAuthorized 170 } 171 return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id 172 }, 173 } 174 }