wallet.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2017 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 keystore
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum"
  20. "github.com/ethereum/go-ethereum/accounts"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. )
  24. // keystoreWallet implements the accounts.Wallet interface for the original
  25. // keystore.
  26. type keystoreWallet struct {
  27. account accounts.Account // Single account contained in this wallet
  28. keystore *KeyStore // Keystore where the account originates from
  29. }
  30. // URL implements accounts.Wallet, returning the URL of the account within.
  31. func (w *keystoreWallet) URL() accounts.URL {
  32. return w.account.URL
  33. }
  34. // Status implements accounts.Wallet, returning whether the account held by the
  35. // keystore wallet is unlocked or not.
  36. func (w *keystoreWallet) Status() (string, error) {
  37. w.keystore.mu.RLock()
  38. defer w.keystore.mu.RUnlock()
  39. if _, ok := w.keystore.unlocked[w.account.Address]; ok {
  40. return "Unlocked", nil
  41. }
  42. return "Locked", nil
  43. }
  44. // Open implements accounts.Wallet, but is a noop for plain wallets since there
  45. // is no connection or decryption step necessary to access the list of accounts.
  46. func (w *keystoreWallet) Open(passphrase string) error { return nil }
  47. // Close implements accounts.Wallet, but is a noop for plain wallets since there
  48. // is no meaningful open operation.
  49. func (w *keystoreWallet) Close() error { return nil }
  50. // Accounts implements accounts.Wallet, returning an account list consisting of
  51. // a single account that the plain keystore wallet contains.
  52. func (w *keystoreWallet) Accounts() []accounts.Account {
  53. return []accounts.Account{w.account}
  54. }
  55. // Contains implements accounts.Wallet, returning whether a particular account is
  56. // or is not wrapped by this wallet instance.
  57. func (w *keystoreWallet) Contains(account accounts.Account) bool {
  58. return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL)
  59. }
  60. // Derive implements accounts.Wallet, but is a noop for plain wallets since there
  61. // is no notion of hierarchical account derivation for plain keystore accounts.
  62. func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
  63. return accounts.Account{}, accounts.ErrNotSupported
  64. }
  65. // SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
  66. // there is no notion of hierarchical account derivation for plain keystore accounts.
  67. func (w *keystoreWallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
  68. }
  69. // signHash attempts to sign the given hash with
  70. // the given account. If the wallet does not wrap this particular account, an
  71. // error is returned to avoid account leakage (even though in theory we may be
  72. // able to sign via our shared keystore backend).
  73. func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
  74. // Make sure the requested account is contained within
  75. if !w.Contains(account) {
  76. return nil, accounts.ErrUnknownAccount
  77. }
  78. // Account seems valid, request the keystore to sign
  79. return w.keystore.SignHash(account, hash)
  80. }
  81. // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed.
  82. func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
  83. return w.signHash(account, crypto.Keccak256(data))
  84. }
  85. // SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed.
  86. func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
  87. // Make sure the requested account is contained within
  88. if !w.Contains(account) {
  89. return nil, accounts.ErrUnknownAccount
  90. }
  91. // Account seems valid, request the keystore to sign
  92. return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data))
  93. }
  94. // SignText implements accounts.Wallet, attempting to sign the hash of
  95. // the given text with the given account.
  96. func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
  97. return w.signHash(account, accounts.TextHash(text))
  98. }
  99. // SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
  100. // hash of the given text with the given account using passphrase as extra authentication.
  101. func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
  102. // Make sure the requested account is contained within
  103. if !w.Contains(account) {
  104. return nil, accounts.ErrUnknownAccount
  105. }
  106. // Account seems valid, request the keystore to sign
  107. return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text))
  108. }
  109. // SignTx implements accounts.Wallet, attempting to sign the given transaction
  110. // with the given account. If the wallet does not wrap this particular account,
  111. // an error is returned to avoid account leakage (even though in theory we may
  112. // be able to sign via our shared keystore backend).
  113. func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  114. // Make sure the requested account is contained within
  115. if !w.Contains(account) {
  116. return nil, accounts.ErrUnknownAccount
  117. }
  118. // Account seems valid, request the keystore to sign
  119. return w.keystore.SignTx(account, tx, chainID)
  120. }
  121. // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
  122. // transaction with the given account using passphrase as extra authentication.
  123. func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  124. // Make sure the requested account is contained within
  125. if !w.Contains(account) {
  126. return nil, accounts.ErrUnknownAccount
  127. }
  128. // Account seems valid, request the keystore to sign
  129. return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
  130. }