github.com/amazechain/amc@v0.1.3/accounts/abi/bind/auth.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package bind 18 19 import ( 20 "context" 21 "crypto/ecdsa" 22 "errors" 23 "github.com/amazechain/amc/accounts" 24 "github.com/amazechain/amc/accounts/keystore" 25 "github.com/amazechain/amc/common/crypto" 26 "github.com/amazechain/amc/common/transaction" 27 "github.com/amazechain/amc/common/types" 28 "github.com/amazechain/amc/log" 29 "io" 30 "math/big" 31 ) 32 33 // ErrNoChainID is returned whenever the user failed to specify a chain id. 34 var ErrNoChainID = errors.New("no chain id specified") 35 36 // ErrNotAuthorized is returned when an account is not properly unlocked. 37 var ErrNotAuthorized = errors.New("not authorized to sign this account") 38 39 // NewTransactor is a utility method to easily create a transaction signer from 40 // an encrypted json key stream and the associated passphrase. 41 // 42 // Deprecated: Use NewTransactorWithChainID instead. 43 func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { 44 log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") 45 json, err := io.ReadAll(keyin) 46 if err != nil { 47 return nil, err 48 } 49 key, err := keystore.DecryptKey(json, passphrase) 50 if err != nil { 51 return nil, err 52 } 53 return NewKeyedTransactor(key.PrivateKey), nil 54 } 55 56 // NewKeyStoreTransactor is a utility method to easily create a transaction signer from 57 // an decrypted key from a keystore. 58 // 59 // Deprecated: Use NewKeyStoreTransactorWithChainID instead. 60 func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { 61 log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID") 62 signer := transaction.HomesteadSigner{} 63 return &TransactOpts{ 64 From: account.Address, 65 Signer: func(address types.Address, tx *transaction.Transaction) (*transaction.Transaction, error) { 66 if address != account.Address { 67 return nil, ErrNotAuthorized 68 } 69 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 70 if err != nil { 71 return nil, err 72 } 73 return tx.WithSignature(signer, signature) 74 }, 75 Context: context.Background(), 76 }, nil 77 } 78 79 // NewKeyedTransactor is a utility method to easily create a transaction signer 80 // from a single private key. 81 // 82 // Deprecated: Use NewKeyedTransactorWithChainID instead. 83 func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { 84 log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") 85 keyAddr := crypto.PubkeyToAddress(key.PublicKey) 86 signer := transaction.HomesteadSigner{} 87 return &TransactOpts{ 88 From: keyAddr, 89 Signer: func(address types.Address, tx *transaction.Transaction) (*transaction.Transaction, error) { 90 if address != keyAddr { 91 return nil, ErrNotAuthorized 92 } 93 signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) 94 if err != nil { 95 return nil, err 96 } 97 return tx.WithSignature(signer, signature) 98 }, 99 Context: context.Background(), 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 := io.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 := transaction.LatestSignerForChainID(chainID) 124 return &TransactOpts{ 125 From: account.Address, 126 Signer: func(address types.Address, tx *transaction.Transaction) (*transaction.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 Context: context.Background(), 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 := transaction.LatestSignerForChainID(chainID) 148 return &TransactOpts{ 149 From: keyAddr, 150 Signer: func(address types.Address, tx *transaction.Transaction) (*transaction.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 Context: context.Background(), 161 }, nil 162 }