accounts.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. ethereum "github.com/ethereum/go-ethereum"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/event"
  24. )
  25. // Account represents an Ethereum account located at a specific location defined
  26. // by the optional URL field.
  27. type Account struct {
  28. Address common.Address `json:"address"` // Ethereum account address derived from the key
  29. URL URL `json:"url"` // Optional resource locator within a backend
  30. }
  31. // Wallet represents a software or hardware wallet that might contain one or more
  32. // accounts (derived from the same seed).
  33. type Wallet interface {
  34. // URL retrieves the canonical path under which this wallet is reachable. It is
  35. // user by upper layers to define a sorting order over all wallets from multiple
  36. // backends.
  37. URL() URL
  38. // Status returns a textual status to aid the user in the current state of the
  39. // wallet. It also returns an error indicating any failure the wallet might have
  40. // encountered.
  41. Status() (string, error)
  42. // Open initializes access to a wallet instance. It is not meant to unlock or
  43. // decrypt account keys, rather simply to establish a connection to hardware
  44. // wallets and/or to access derivation seeds.
  45. //
  46. // The passphrase parameter may or may not be used by the implementation of a
  47. // particular wallet instance. The reason there is no passwordless open method
  48. // is to strive towards a uniform wallet handling, oblivious to the different
  49. // backend providers.
  50. //
  51. // Please note, if you open a wallet, you must close it to release any allocated
  52. // resources (especially important when working with hardware wallets).
  53. Open(passphrase string) error
  54. // Close releases any resources held by an open wallet instance.
  55. Close() error
  56. // Accounts retrieves the list of signing accounts the wallet is currently aware
  57. // of. For hierarchical deterministic wallets, the list will not be exhaustive,
  58. // rather only contain the accounts explicitly pinned during account derivation.
  59. Accounts() []Account
  60. // Contains returns whether an account is part of this particular wallet or not.
  61. Contains(account Account) bool
  62. // Derive attempts to explicitly derive a hierarchical deterministic account at
  63. // the specified derivation path. If requested, the derived account will be added
  64. // to the wallet's tracked account list.
  65. Derive(path DerivationPath, pin bool) (Account, error)
  66. // SelfDerive sets a base account derivation path from which the wallet attempts
  67. // to discover non zero accounts and automatically add them to list of tracked
  68. // accounts.
  69. //
  70. // Note, self derivaton will increment the last component of the specified path
  71. // opposed to decending into a child path to allow discovering accounts starting
  72. // from non zero components.
  73. //
  74. // You can disable automatic account discovery by calling SelfDerive with a nil
  75. // chain state reader.
  76. SelfDerive(base DerivationPath, chain ethereum.ChainStateReader)
  77. // SignHash requests the wallet to sign the given hash.
  78. //
  79. // It looks up the account specified either solely via its address contained within,
  80. // or optionally with the aid of any location metadata from the embedded URL field.
  81. //
  82. // If the wallet requires additional authentication to sign the request (e.g.
  83. // a password to decrypt the account, or a PIN code o verify the transaction),
  84. // an AuthNeededError instance will be returned, containing infos for the user
  85. // about which fields or actions are needed. The user may retry by providing
  86. // the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
  87. // the account in a keystore).
  88. SignHash(account Account, hash []byte) ([]byte, error)
  89. // SignTx requests the wallet to sign the given transaction.
  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. //
  94. // If the wallet requires additional authentication to sign the request (e.g.
  95. // a password to decrypt the account, or a PIN code o verify the transaction),
  96. // an AuthNeededError instance will be returned, containing infos for the user
  97. // about which fields or actions are needed. The user may retry by providing
  98. // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
  99. // the account in a keystore).
  100. SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  101. // SignHashWithPassphrase requests the wallet to sign the given hash with the
  102. // given passphrase as extra authentication information.
  103. //
  104. // It looks up the account specified either solely via its address contained within,
  105. // or optionally with the aid of any location metadata from the embedded URL field.
  106. SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
  107. // SignTxWithPassphrase requests the wallet to sign the given transaction, with the
  108. // given passphrase as extra authentication information.
  109. //
  110. // It looks up the account specified either solely via its address contained within,
  111. // or optionally with the aid of any location metadata from the embedded URL field.
  112. SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  113. }
  114. // Backend is a "wallet provider" that may contain a batch of accounts they can
  115. // sign transactions with and upon request, do so.
  116. type Backend interface {
  117. // Wallets retrieves the list of wallets the backend is currently aware of.
  118. //
  119. // The returned wallets are not opened by default. For software HD wallets this
  120. // means that no base seeds are decrypted, and for hardware wallets that no actual
  121. // connection is established.
  122. //
  123. // The resulting wallet list will be sorted alphabetically based on its internal
  124. // URL assigned by the backend. Since wallets (especially hardware) may come and
  125. // go, the same wallet might appear at a different positions in the list during
  126. // subsequent retrievals.
  127. Wallets() []Wallet
  128. // Subscribe creates an async subscription to receive notifications when the
  129. // backend detects the arrival or departure of a wallet.
  130. Subscribe(sink chan<- WalletEvent) event.Subscription
  131. }
  132. // WalletEventType represents the different event types that can be fired by
  133. // the wallet subscription subsystem.
  134. type WalletEventType int
  135. const (
  136. // WalletArrived is fired when a new wallet is detected either via USB or via
  137. // a filesystem event in the keystore.
  138. WalletArrived WalletEventType = iota
  139. // WalletOpened is fired when a wallet is successfully opened with the purpose
  140. // of starting any background processes such as automatic key derivation.
  141. WalletOpened
  142. // WalletDropped
  143. WalletDropped
  144. )
  145. // WalletEvent is an event fired by an account backend when a wallet arrival or
  146. // departure is detected.
  147. type WalletEvent struct {
  148. Wallet Wallet // Wallet instance arrived or departed
  149. Kind WalletEventType // Event type that happened in the system
  150. }