auth.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. package bind
  17. import (
  18. "crypto/ecdsa"
  19. "errors"
  20. "io"
  21. "io/ioutil"
  22. "math/big"
  23. "reflect"
  24. "github.com/ethereum/go-ethereum/accounts"
  25. "github.com/ethereum/go-ethereum/accounts/external"
  26. "github.com/ethereum/go-ethereum/accounts/keystore"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/log"
  31. )
  32. // ErrNoChainID is returned whenever the user failed to specify a chain id.
  33. var ErrNoChainID = errors.New("no chain id specified")
  34. // ErrNotAuthorized is returned when an account is not properly unlocked.
  35. var ErrNotAuthorized = errors.New("not authorized to sign this account")
  36. // NewTransactor is a utility method to easily create a transaction signer from
  37. // an encrypted json key stream and the associated passphrase.
  38. //
  39. // Deprecated: Use NewTransactorWithChainID instead.
  40. func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
  41. log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
  42. json, err := ioutil.ReadAll(keyin)
  43. if err != nil {
  44. return nil, err
  45. }
  46. key, err := keystore.DecryptKey(json, passphrase)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return NewKeyedTransactor(key.PrivateKey), nil
  51. }
  52. // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
  53. // an decrypted key from a keystore.
  54. //
  55. // Deprecated: Use NewKeyStoreTransactorWithChainID instead.
  56. func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
  57. log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
  58. var homesteadSigner types.Signer = types.HomesteadSigner{}
  59. return &TransactOpts{
  60. From: account.Address,
  61. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  62. if address != account.Address {
  63. return nil, ErrNotAuthorized
  64. }
  65. // Quorum
  66. signer := homesteadSigner
  67. if tx.IsPrivate() {
  68. signer = types.QuorumPrivateTxSigner{}
  69. }
  70. // / Quorum
  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. // 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. var homesteadSigner types.Signer = types.HomesteadSigner{}
  87. return &TransactOpts{
  88. From: keyAddr,
  89. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  90. if address != keyAddr {
  91. return nil, ErrNotAuthorized
  92. }
  93. // Quorum
  94. signer := homesteadSigner
  95. if tx.IsPrivate() {
  96. signer = types.QuorumPrivateTxSigner{}
  97. }
  98. // / Quorum
  99. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return tx.WithSignature(signer, signature)
  104. },
  105. }
  106. }
  107. // NewTransactorWithChainID is a utility method to easily create a transaction signer from
  108. // an encrypted json key stream and the associated passphrase.
  109. func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
  110. json, err := ioutil.ReadAll(keyin)
  111. if err != nil {
  112. return nil, err
  113. }
  114. key, err := keystore.DecryptKey(json, passphrase)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
  119. }
  120. // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
  121. // an decrypted key from a keystore.
  122. func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
  123. if chainID == nil {
  124. return nil, ErrNoChainID
  125. }
  126. latestSigner := types.LatestSignerForChainID(chainID)
  127. log.Info("NewKeyStoreTransactorWithChainID", "latestSigner", reflect.TypeOf(latestSigner))
  128. return &TransactOpts{
  129. From: account.Address,
  130. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  131. if address != account.Address {
  132. return nil, ErrNotAuthorized
  133. }
  134. // Quorum
  135. signer := latestSigner
  136. if tx.IsPrivate() {
  137. signer = types.QuorumPrivateTxSigner{}
  138. }
  139. // / Quorum
  140. signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
  141. if err != nil {
  142. return nil, err
  143. }
  144. return tx.WithSignature(signer, signature)
  145. },
  146. }, nil
  147. }
  148. // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
  149. // from a single private key.
  150. func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
  151. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  152. if chainID == nil {
  153. return nil, ErrNoChainID
  154. }
  155. latestSigner := types.LatestSignerForChainID(chainID)
  156. return &TransactOpts{
  157. From: keyAddr,
  158. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  159. if address != keyAddr {
  160. return nil, ErrNotAuthorized
  161. }
  162. // Quorum
  163. signer := latestSigner
  164. if tx.IsPrivate() {
  165. signer = types.QuorumPrivateTxSigner{}
  166. }
  167. // / Quorum
  168. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return tx.WithSignature(signer, signature)
  173. },
  174. }, nil
  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. log.Info("Signing with NewClefTransactor")
  186. return clef.SignTx(account, transaction, transaction.ChainId()) // Clef enforces its own chain id
  187. },
  188. }
  189. }
  190. // Quorum
  191. //
  192. // NewWalletTransactor is a utility method to easily create a transaction signer
  193. // from a wallet account
  194. func NewWalletTransactor(w accounts.Wallet, account accounts.Account, chainId *big.Int) *TransactOpts {
  195. return &TransactOpts{
  196. From: account.Address,
  197. Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
  198. if address != account.Address {
  199. return nil, ErrNotAuthorized
  200. }
  201. if transaction.ChainId() == nil {
  202. chainId = transaction.ChainId()
  203. }
  204. return w.SignTx(account, transaction, chainId)
  205. },
  206. }
  207. }