github.com/theQRL/go-zond@v0.1.1/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 "context" 21 "errors" 22 "io" 23 "math/big" 24 25 "github.com/theQRL/go-qrllib/dilithium" 26 "github.com/theQRL/go-zond/accounts" 27 "github.com/theQRL/go-zond/accounts/external" 28 "github.com/theQRL/go-zond/accounts/keystore" 29 "github.com/theQRL/go-zond/common" 30 "github.com/theQRL/go-zond/core/types" 31 "github.com/theQRL/go-zond/log" 32 "github.com/theQRL/go-zond/pqcrypto" 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 := io.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.Dilithium), 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 pk, err := keystore.GetPublicKey(account) 76 if err != nil { 77 return nil, err 78 } 79 return tx.WithSignatureAndPublicKey(signer, signature, pk) 80 }, 81 Context: context.Background(), 82 }, nil 83 } 84 85 // NewKeyedTransactor is a utility method to easily create a transaction signer 86 // from a single private key. 87 // 88 // Deprecated: Use NewKeyedTransactorWithChainID instead. 89 func NewKeyedTransactor(d *dilithium.Dilithium) *TransactOpts { 90 log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") 91 keyAddr := d.GetAddress() 92 signer := types.HomesteadSigner{} 93 return &TransactOpts{ 94 From: keyAddr, 95 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 96 if address != keyAddr { 97 return nil, ErrNotAuthorized 98 } 99 signature, err := pqcrypto.Sign(signer.Hash(tx).Bytes(), d) 100 if err != nil { 101 return nil, err 102 } 103 pk := d.GetPK() 104 return tx.WithSignatureAndPublicKey(signer, signature, pk[:]) 105 }, 106 Context: context.Background(), 107 } 108 } 109 110 // NewTransactorWithChainID is a utility method to easily create a transaction signer from 111 // an encrypted json key stream and the associated passphrase. 112 func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { 113 json, err := io.ReadAll(keyin) 114 if err != nil { 115 return nil, err 116 } 117 key, err := keystore.DecryptKey(json, passphrase) 118 if err != nil { 119 return nil, err 120 } 121 return NewKeyedTransactorWithChainID(key.Dilithium, chainID) 122 } 123 124 // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from 125 // an decrypted key from a keystore. 126 func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { 127 if chainID == nil { 128 return nil, ErrNoChainID 129 } 130 signer := types.LatestSignerForChainID(chainID) 131 return &TransactOpts{ 132 From: account.Address, 133 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 134 if address != account.Address { 135 return nil, ErrNotAuthorized 136 } 137 signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) 138 if err != nil { 139 return nil, err 140 } 141 pk, err := keystore.GetPublicKey(account) 142 if err != nil { 143 return nil, err 144 } 145 return tx.WithSignatureAndPublicKey(signer, signature, pk) 146 }, 147 Context: context.Background(), 148 }, nil 149 } 150 151 // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer 152 // from a single private key. 153 func NewKeyedTransactorWithChainID(d *dilithium.Dilithium, chainID *big.Int) (*TransactOpts, error) { 154 keyAddr := d.GetAddress() 155 if chainID == nil { 156 return nil, ErrNoChainID 157 } 158 signer := types.LatestSignerForChainID(chainID) 159 return &TransactOpts{ 160 From: keyAddr, 161 Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { 162 if address != keyAddr { 163 return nil, ErrNotAuthorized 164 } 165 signature, err := pqcrypto.Sign(signer.Hash(tx).Bytes(), d) 166 if err != nil { 167 return nil, err 168 } 169 pk := d.GetPK() 170 return tx.WithSignatureAndPublicKey(signer, signature, pk[:]) 171 }, 172 Context: context.Background(), 173 }, nil 174 } 175 176 // NewClefTransactor is a utility method to easily create a transaction signer 177 // with a clef backend. 178 func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { 179 return &TransactOpts{ 180 From: account.Address, 181 Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { 182 if address != account.Address { 183 return nil, ErrNotAuthorized 184 } 185 return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id 186 }, 187 Context: context.Background(), 188 } 189 }