auth.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "context"
  19. "crypto/ecdsa"
  20. "errors"
  21. "io"
  22. "math/big"
  23. "github.com/ethereum/go-ethereum/accounts"
  24. "github.com/ethereum/go-ethereum/accounts/external"
  25. "github.com/ethereum/go-ethereum/accounts/keystore"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/log"
  30. )
  31. // ErrNoChainID is returned whenever the user failed to specify a chain id.
  32. var ErrNoChainID = errors.New("no chain id specified")
  33. // ErrNotAuthorized is returned when an account is not properly unlocked.
  34. var ErrNotAuthorized = errors.New("not authorized to sign this account")
  35. // NewTransactor is a utility method to easily create a transaction signer from
  36. // an encrypted json key stream and the associated passphrase.
  37. //
  38. // Deprecated: Use NewTransactorWithChainID instead.
  39. func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
  40. log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
  41. json, err := io.ReadAll(keyin)
  42. if err != nil {
  43. return nil, err
  44. }
  45. key, err := keystore.DecryptKey(json, passphrase)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return NewKeyedTransactor(key.PrivateKey), nil
  50. }
  51. // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
  52. // an decrypted key from a keystore.
  53. //
  54. // Deprecated: Use NewKeyStoreTransactorWithChainID instead.
  55. func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
  56. log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
  57. signer := types.HomesteadSigner{}
  58. return &TransactOpts{
  59. From: account.Address,
  60. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  61. if address != account.Address {
  62. return nil, ErrNotAuthorized
  63. }
  64. signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
  65. if err != nil {
  66. return nil, err
  67. }
  68. return tx.WithSignature(signer, signature)
  69. },
  70. Context: context.Background(),
  71. }, nil
  72. }
  73. // NewKeyedTransactor is a utility method to easily create a transaction signer
  74. // from a single private key.
  75. //
  76. // Deprecated: Use NewKeyedTransactorWithChainID instead.
  77. func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
  78. log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
  79. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  80. signer := types.HomesteadSigner{}
  81. return &TransactOpts{
  82. From: keyAddr,
  83. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  84. if address != keyAddr {
  85. return nil, ErrNotAuthorized
  86. }
  87. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return tx.WithSignature(signer, signature)
  92. },
  93. Context: context.Background(),
  94. }
  95. }
  96. // NewTransactorWithChainID is a utility method to easily create a transaction signer from
  97. // an encrypted json key stream and the associated passphrase.
  98. func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
  99. json, err := io.ReadAll(keyin)
  100. if err != nil {
  101. return nil, err
  102. }
  103. key, err := keystore.DecryptKey(json, passphrase)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
  108. }
  109. // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
  110. // an decrypted key from a keystore.
  111. func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
  112. if chainID == nil {
  113. return nil, ErrNoChainID
  114. }
  115. signer := types.LatestSignerForChainID(chainID)
  116. return &TransactOpts{
  117. From: account.Address,
  118. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  119. if address != account.Address {
  120. return nil, ErrNotAuthorized
  121. }
  122. signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
  123. if err != nil {
  124. return nil, err
  125. }
  126. return tx.WithSignature(signer, signature)
  127. },
  128. Context: context.Background(),
  129. }, nil
  130. }
  131. // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
  132. // from a single private key.
  133. func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
  134. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  135. if chainID == nil {
  136. return nil, ErrNoChainID
  137. }
  138. signer := types.LatestSignerForChainID(chainID)
  139. return &TransactOpts{
  140. From: keyAddr,
  141. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  142. if address != keyAddr {
  143. return nil, ErrNotAuthorized
  144. }
  145. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return tx.WithSignature(signer, signature)
  150. },
  151. Context: context.Background(),
  152. }, nil
  153. }
  154. // NewClefTransactor is a utility method to easily create a transaction signer
  155. // with a clef backend.
  156. func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
  157. return &TransactOpts{
  158. From: account.Address,
  159. Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
  160. if address != account.Address {
  161. return nil, ErrNotAuthorized
  162. }
  163. return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
  164. },
  165. Context: context.Background(),
  166. }
  167. }