github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/accounts/abi/bind/auth.go (about) 1 // Copyright 2022 Commercium 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 18 package bind 19 20 import ( 21 "crypto/ecdsa" 22 "errors" 23 "io" 24 "io/ioutil" 25 "math/big" 26 27 "github.com/CommerciumBlockchain/go-commercium/accounts" 28 "github.com/CommerciumBlockchain/go-commercium/accounts/external" 29 "github.com/CommerciumBlockchain/go-commercium/accounts/keystore" 30 "github.com/CommerciumBlockchain/go-commercium/common" 31 "github.com/CommerciumBlockchain/go-commercium/core/types" 32 "github.com/CommerciumBlockchain/go-commercium/crypto" 33 "github.com/CommerciumBlockchain/go-commercium/log" 34 ) 35 36 // ErrNoChainID is returned whenever the user failed to specify a chain id. 37 var ErrNoChainID = errors.New("no chain id specified") 38 39 // ErrNotAuthorized is returned when an account is not properly unlocked. 40 var ErrNotAuthorized = errors.New("not authorized to sign this account") 41 42 // NewTransactor is a utility method to easily create a transaction signer from 43 // an encrypted json key stream and the associated passphrase. 44 // 45 // Deprecated: Use NewTransactorWithChainID instead. 46 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 47 log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") 48 json, err := ioutil.ReadAll(keyin) 49 if err != nil { 50 return nil, err 51 } 52 key, err := keystore.DecryptKey(json, passphrase) 53 if err != nil { 54 return nil, err 55 } 56 return NewKeyedTransactor(key.PrivateKey), nil 57 } 58 59 // NewKeyStoreTransactor is a utility method to easily create a transaction signer from 60 // an decrypted key from a keystore. 61 // 62 // Deprecated: Use NewKeyStoreTransactorWithChainID instead. 63 func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { 64 log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID") 65 signer := types.HomesteadSigner{} 66 return &TransactOpts{ 67 From: account.Address, 68 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 69 if address != account.Address { 70 return nil, ErrNotAuthorized 71 } 72 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 73 if err != nil { 74 return nil, err 75 } 76 return tx.WithSignature(signer, signature) 77 }, 78 }, nil 79 } 80 81 // NewKeyedTransactor is a utility method to easily create a transaction signer 82 // from a single private key. 83 // 84 // Deprecated: Use NewKeyedTransactorWithChainID instead. 85 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 86 log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") 87 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 88 signer := types.HomesteadSigner{} 89 return &TransactOpts{ 90 From: keyAddr, 91 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 92 if address != keyAddr { 93 return nil, ErrNotAuthorized 94 } 95 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 96 if err != nil { 97 return nil, err 98 } 99 return tx.WithSignature(signer, signature) 100 }, 101 } 102 } 103 104 // NewTransactorWithChainID is a utility method to easily create a transaction signer from 105 // an encrypted json key stream and the associated passphrase. 106 func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { 107 json, err := ioutil.ReadAll(keyin) 108 if err != nil { 109 return nil, err 110 } 111 key, err := keystore.DecryptKey(json, passphrase) 112 if err != nil { 113 return nil, err 114 } 115 return NewKeyedTransactorWithChainID(key.PrivateKey, chainID) 116 } 117 118 // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from 119 // an decrypted key from a keystore. 120 func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { 121 if chainID == nil { 122 return nil, ErrNoChainID 123 } 124 signer := types.NewEIP155Signer(chainID) 125 return &TransactOpts{ 126 From: account.Address, 127 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 128 if address != account.Address { 129 return nil, ErrNotAuthorized 130 } 131 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 132 if err != nil { 133 return nil, err 134 } 135 return tx.WithSignature(signer, signature) 136 }, 137 }, nil 138 } 139 140 // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer 141 // from a single private key. 142 func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) { 143 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 144 if chainID == nil { 145 return nil, ErrNoChainID 146 } 147 signer := types.NewEIP155Signer(chainID) 148 return &TransactOpts{ 149 From: keyAddr, 150 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 151 if address != keyAddr { 152 return nil, ErrNotAuthorized 153 } 154 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 155 if err != nil { 156 return nil, err 157 } 158 return tx.WithSignature(signer, signature) 159 }, 160 }, nil 161 } 162 163 // NewClefTransactor is a utility method to easily create a transaction signer 164 // with a clef backend. 165 func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { 166 return &TransactOpts{ 167 From: account.Address, 168 Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { 169 if address != account.Address { 170 return nil, ErrNotAuthorized 171 } 172 return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id 173 }, 174 } 175 }