keystore_wallet.go 6.0 KB

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