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