accounts.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 accounts implements high level Ethereum account management.
  17. package accounts
  18. import (
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/event"
  23. )
  24. // Account represents an Ethereum account located at a specific location defined
  25. // by the optional URL field.
  26. type Account struct {
  27. Address common.Address `json:"address"` // Ethereum account address derived from the key
  28. URL URL `json:"url"` // Optional resource locator within a backend
  29. }
  30. // Wallet represents a software or hardware wallet that might contain one or more
  31. // accounts (derived from the same seed).
  32. type Wallet interface {
  33. // URL retrieves the canonical path under which this wallet is reachable. It is
  34. // user by upper layers to define a sorting order over all wallets from multiple
  35. // backends.
  36. URL() URL
  37. // Status returns a textual status to aid the user in the current state of the
  38. // wallet.
  39. Status() string
  40. // Open initializes access to a wallet instance. It is not meant to unlock or
  41. // decrypt account keys, rather simply to establish a connection to hardware
  42. // wallets and/or to access derivation seeds.
  43. //
  44. // The passphrase parameter may or may not be used by the implementation of a
  45. // particular wallet instance. The reason there is no passwordless open method
  46. // is to strive towards a uniform wallet handling, oblivious to the different
  47. // backend providers.
  48. //
  49. // Please note, if you open a wallet, you must close it to release any allocated
  50. // resources (especially important when working with hardware wallets).
  51. Open(passphrase string) error
  52. // Close releases any resources held by an open wallet instance.
  53. Close() error
  54. // Accounts retrieves the list of signing accounts the wallet is currently aware
  55. // of. For hierarchical deterministic wallets, the list will not be exhaustive,
  56. // rather only contain the accounts explicitly pinned during account derivation.
  57. Accounts() []Account
  58. // Contains returns whether an account is part of this particular wallet or not.
  59. Contains(account Account) bool
  60. // Derive attempts to explicitly derive a hierarchical deterministic account at
  61. // the specified derivation path. If requested, the derived account will be added
  62. // to the wallet's tracked account list.
  63. Derive(path string, pin bool) (Account, error)
  64. // SignHash requests the wallet to sign the given hash.
  65. //
  66. // It looks up the account specified either solely via its address contained within,
  67. // or optionally with the aid of any location metadata from the embedded URL field.
  68. //
  69. // If the wallet requires additional authentication to sign the request (e.g.
  70. // a password to decrypt the account, or a PIN code o verify the transaction),
  71. // an AuthNeededError instance will be returned, containing infos for the user
  72. // about which fields or actions are needed. The user may retry by providing
  73. // the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
  74. // the account in a keystore).
  75. SignHash(account Account, hash []byte) ([]byte, error)
  76. // SignTx requests the wallet to sign the given transaction.
  77. //
  78. // It looks up the account specified either solely via its address contained within,
  79. // or optionally with the aid of any location metadata from the embedded URL field.
  80. //
  81. // If the wallet requires additional authentication to sign the request (e.g.
  82. // a password to decrypt the account, or a PIN code o verify the transaction),
  83. // an AuthNeededError instance will be returned, containing infos for the user
  84. // about which fields or actions are needed. The user may retry by providing
  85. // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
  86. // the account in a keystore).
  87. SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  88. // SignHashWithPassphrase requests the wallet to sign the given hash with the
  89. // given passphrase as extra authentication information.
  90. //
  91. // It looks up the account specified either solely via its address contained within,
  92. // or optionally with the aid of any location metadata from the embedded URL field.
  93. SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
  94. // SignTxWithPassphrase requests the wallet to sign the given transaction, with the
  95. // given passphrase as extra authentication information.
  96. //
  97. // It looks up the account specified either solely via its address contained within,
  98. // or optionally with the aid of any location metadata from the embedded URL field.
  99. SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  100. }
  101. // Backend is a "wallet provider" that may contain a batch of accounts they can
  102. // sign transactions with and upon request, do so.
  103. type Backend interface {
  104. // Wallets retrieves the list of wallets the backend is currently aware of.
  105. //
  106. // The returned wallets are not opened by default. For software HD wallets this
  107. // means that no base seeds are decrypted, and for hardware wallets that no actual
  108. // connection is established.
  109. //
  110. // The resulting wallet list will be sorted alphabetically based on its internal
  111. // URL assigned by the backend. Since wallets (especially hardware) may come and
  112. // go, the same wallet might appear at a different positions in the list during
  113. // subsequent retrievals.
  114. Wallets() []Wallet
  115. // Subscribe creates an async subscription to receive notifications when the
  116. // backend detects the arrival or departure of a wallet.
  117. Subscribe(sink chan<- WalletEvent) event.Subscription
  118. }
  119. // WalletEvent is an event fired by an account backend when a wallet arrival or
  120. // departure is detected.
  121. type WalletEvent struct {
  122. Wallet Wallet // Wallet instance arrived or departed
  123. Arrive bool // Whether the wallet was added or removed
  124. }