accounts.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.
  40. Status() string
  41. // Open initializes access to a wallet instance. It is not meant to unlock or
  42. // decrypt account keys, rather simply to establish a connection to hardware
  43. // wallets and/or to access derivation seeds.
  44. //
  45. // The passphrase parameter may or may not be used by the implementation of a
  46. // particular wallet instance. The reason there is no passwordless open method
  47. // is to strive towards a uniform wallet handling, oblivious to the different
  48. // backend providers.
  49. //
  50. // Please note, if you open a wallet, you must close it to release any allocated
  51. // resources (especially important when working with hardware wallets).
  52. Open(passphrase string) error
  53. // Close releases any resources held by an open wallet instance.
  54. Close() error
  55. // Accounts retrieves the list of signing accounts the wallet is currently aware
  56. // of. For hierarchical deterministic wallets, the list will not be exhaustive,
  57. // rather only contain the accounts explicitly pinned during account derivation.
  58. Accounts() []Account
  59. // Contains returns whether an account is part of this particular wallet or not.
  60. Contains(account Account) bool
  61. // Derive attempts to explicitly derive a hierarchical deterministic account at
  62. // the specified derivation path. If requested, the derived account will be added
  63. // to the wallet's tracked account list.
  64. Derive(path DerivationPath, pin bool) (Account, error)
  65. // SelfDerive sets a base account derivation path from which the wallet attempts
  66. // to discover non zero accounts and automatically add them to list of tracked
  67. // accounts.
  68. //
  69. // Note, self derivaton will increment the last component of the specified path
  70. // opposed to decending into a child path to allow discovering accounts starting
  71. // from non zero components.
  72. //
  73. // You can disable automatic account discovery by calling SelfDerive with a nil
  74. // chain state reader.
  75. SelfDerive(base DerivationPath, chain ethereum.ChainStateReader)
  76. // SignHash requests the wallet to sign the given hash.
  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 SignHashWithPassphrase, or by other means (e.g. unlock
  86. // the account in a keystore).
  87. SignHash(account Account, hash []byte) ([]byte, error)
  88. // SignTx requests the wallet to sign the given transaction.
  89. //
  90. // It looks up the account specified either solely via its address contained within,
  91. // or optionally with the aid of any location metadata from the embedded URL field.
  92. //
  93. // If the wallet requires additional authentication to sign the request (e.g.
  94. // a password to decrypt the account, or a PIN code o verify the transaction),
  95. // an AuthNeededError instance will be returned, containing infos for the user
  96. // about which fields or actions are needed. The user may retry by providing
  97. // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
  98. // the account in a keystore).
  99. SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  100. // SignHashWithPassphrase requests the wallet to sign the given hash with the
  101. // given passphrase as extra authentication information.
  102. //
  103. // It looks up the account specified either solely via its address contained within,
  104. // or optionally with the aid of any location metadata from the embedded URL field.
  105. SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
  106. // SignTxWithPassphrase requests the wallet to sign the given transaction, with the
  107. // given passphrase as extra authentication information.
  108. //
  109. // It looks up the account specified either solely via its address contained within,
  110. // or optionally with the aid of any location metadata from the embedded URL field.
  111. SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
  112. }
  113. // Backend is a "wallet provider" that may contain a batch of accounts they can
  114. // sign transactions with and upon request, do so.
  115. type Backend interface {
  116. // Wallets retrieves the list of wallets the backend is currently aware of.
  117. //
  118. // The returned wallets are not opened by default. For software HD wallets this
  119. // means that no base seeds are decrypted, and for hardware wallets that no actual
  120. // connection is established.
  121. //
  122. // The resulting wallet list will be sorted alphabetically based on its internal
  123. // URL assigned by the backend. Since wallets (especially hardware) may come and
  124. // go, the same wallet might appear at a different positions in the list during
  125. // subsequent retrievals.
  126. Wallets() []Wallet
  127. // Subscribe creates an async subscription to receive notifications when the
  128. // backend detects the arrival or departure of a wallet.
  129. Subscribe(sink chan<- WalletEvent) event.Subscription
  130. }
  131. // WalletEvent is an event fired by an account backend when a wallet arrival or
  132. // departure is detected.
  133. type WalletEvent struct {
  134. Wallet Wallet // Wallet instance arrived or departed
  135. Arrive bool // Whether the wallet was added or removed
  136. }