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