wallet.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "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 kestore 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(base accounts.DerivationPath, chain ethereum.ChainStateReader) {}
  68. // signHash attempts to sign the given hash with
  69. // the given account. If the wallet does not wrap this particular account, an
  70. // error is returned to avoid account leakage (even though in theory we may be
  71. // able to sign via our shared keystore backend).
  72. func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
  73. // Make sure the requested account is contained within
  74. if !w.Contains(account) {
  75. return nil, accounts.ErrUnknownAccount
  76. }
  77. // Account seems valid, request the keystore to sign
  78. return w.keystore.SignHash(account, hash)
  79. }
  80. // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
  81. func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
  82. return w.signHash(account, crypto.Keccak256(data))
  83. }
  84. func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
  85. return w.signHash(account, accounts.TextHash(text))
  86. }
  87. // SignTx implements accounts.Wallet, attempting to sign the given transaction
  88. // with the given account. If the wallet does not wrap this particular account,
  89. // an error is returned to avoid account leakage (even though in theory we may
  90. // be able to sign via our shared keystore backend).
  91. func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  92. // Make sure the requested account is contained within
  93. if !w.Contains(account) {
  94. return nil, accounts.ErrUnknownAccount
  95. }
  96. // Account seems valid, request the keystore to sign
  97. return w.keystore.SignTx(account, tx, chainID)
  98. }
  99. // SignHashWithPassphrase implements accounts.Wallet, attempting to sign the
  100. // given hash 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. // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
  110. // transaction with the given account using passphrase as extra authentication.
  111. func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  112. // Make sure the requested account is contained within
  113. if !w.Contains(account) {
  114. return nil, accounts.ErrUnknownAccount
  115. }
  116. // Account seems valid, request the keystore to sign
  117. return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID)
  118. }