auth.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "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 := ioutil.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. }, nil
  71. }
  72. // NewKeyedTransactor is a utility method to easily create a transaction signer
  73. // from a single private key.
  74. //
  75. // Deprecated: Use NewKeyedTransactorWithChainID instead.
  76. func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
  77. log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
  78. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  79. signer := types.HomesteadSigner{}
  80. return &TransactOpts{
  81. From: keyAddr,
  82. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  83. if address != keyAddr {
  84. return nil, ErrNotAuthorized
  85. }
  86. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return tx.WithSignature(signer, signature)
  91. },
  92. }
  93. }
  94. // NewTransactorWithChainID is a utility method to easily create a transaction signer from
  95. // an encrypted json key stream and the associated passphrase.
  96. func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
  97. json, err := ioutil.ReadAll(keyin)
  98. if err != nil {
  99. return nil, err
  100. }
  101. key, err := keystore.DecryptKey(json, passphrase)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
  106. }
  107. // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
  108. // an decrypted key from a keystore.
  109. func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
  110. if chainID == nil {
  111. return nil, ErrNoChainID
  112. }
  113. signer := types.NewEIP155Signer(chainID)
  114. return &TransactOpts{
  115. From: account.Address,
  116. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  117. if address != account.Address {
  118. return nil, ErrNotAuthorized
  119. }
  120. signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
  121. if err != nil {
  122. return nil, err
  123. }
  124. return tx.WithSignature(signer, signature)
  125. },
  126. }, nil
  127. }
  128. // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
  129. // from a single private key.
  130. func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
  131. keyAddr := crypto.PubkeyToAddress(key.PublicKey)
  132. if chainID == nil {
  133. return nil, ErrNoChainID
  134. }
  135. signer := types.NewEIP155Signer(chainID)
  136. return &TransactOpts{
  137. From: keyAddr,
  138. Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
  139. if address != keyAddr {
  140. return nil, ErrNotAuthorized
  141. }
  142. signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return tx.WithSignature(signer, signature)
  147. },
  148. }, nil
  149. }
  150. // NewClefTransactor is a utility method to easily create a transaction signer
  151. // with a clef backend.
  152. func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
  153. return &TransactOpts{
  154. From: account.Address,
  155. Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) {
  156. if address != account.Address {
  157. return nil, ErrNotAuthorized
  158. }
  159. return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
  160. },
  161. }
  162. }