github.com/theQRL/go-zond@v0.2.1/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 Zond account management. 18 package accounts 19 20 import ( 21 "fmt" 22 "math/big" 23 24 "github.com/theQRL/go-zond" 25 "github.com/theQRL/go-zond/common" 26 "github.com/theQRL/go-zond/core/types" 27 "github.com/theQRL/go-zond/event" 28 "golang.org/x/crypto/sha3" 29 ) 30 31 // Account represents an Zond account located at a specific location defined 32 // by the optional URL field. 33 type Account struct { 34 Address common.Address `json:"address"` // Zond account address derived from the key 35 URL URL `json:"url"` // Optional resource locator within a backend 36 } 37 38 const ( 39 MimetypeDataWithValidator = "data/validator" 40 MimetypeTypedData = "data/typed" 41 MimetypeTextPlain = "text/plain" 42 ) 43 44 // Wallet represents a software or hardware wallet that might contain one or more 45 // accounts (derived from the same seed). 46 type Wallet interface { 47 // URL retrieves the canonical path under which this wallet is reachable. It is 48 // used by upper layers to define a sorting order over all wallets from multiple 49 // backends. 50 URL() URL 51 52 // Status returns a textual status to aid the user in the current state of the 53 // wallet. It also returns an error indicating any failure the wallet might have 54 // encountered. 55 Status() (string, error) 56 57 // Open initializes access to a wallet instance. It is not meant to unlock or 58 // decrypt account keys, rather simply to establish a connection to hardware 59 // wallets and/or to access derivation seeds. 60 // 61 // The passphrase parameter may or may not be used by the implementation of a 62 // particular wallet instance. The reason there is no passwordless open method 63 // is to strive towards a uniform wallet handling, oblivious to the different 64 // backend providers. 65 // 66 // Please note, if you open a wallet, you must close it to release any allocated 67 // resources (especially important when working with hardware wallets). 68 Open(passphrase string) error 69 70 // Close releases any resources held by an open wallet instance. 71 Close() error 72 73 // Accounts retrieves the list of signing accounts the wallet is currently aware 74 // of. For hierarchical deterministic wallets, the list will not be exhaustive, 75 // rather only contain the accounts explicitly pinned during account derivation. 76 Accounts() []Account 77 78 // Contains returns whether an account is part of this particular wallet or not. 79 Contains(account Account) bool 80 81 // Derive attempts to explicitly derive a hierarchical deterministic account at 82 // the specified derivation path. If requested, the derived account will be added 83 // to the wallet's tracked account list. 84 Derive(path DerivationPath, pin bool) (Account, error) 85 86 // SelfDerive sets a base account derivation path from which the wallet attempts 87 // to discover non zero accounts and automatically add them to list of tracked 88 // accounts. 89 // 90 // Note, self derivation will increment the last component of the specified path 91 // opposed to descending into a child path to allow discovering accounts starting 92 // from non zero components. 93 // 94 // Some hardware wallets switched derivation paths through their evolution, so 95 // this method supports providing multiple bases to discover old user accounts 96 // too. Only the last base will be used to derive the next empty account. 97 // 98 // You can disable automatic account discovery by calling SelfDerive with a nil 99 // chain state reader. 100 SelfDerive(bases []DerivationPath, chain zond.ChainStateReader) 101 102 // SignData requests the wallet to sign the hash of the given data 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 // 106 // If the wallet requires additional authentication to sign the request (e.g. 107 // a password to decrypt the account, or a PIN code to verify the transaction), 108 // an AuthNeededError instance will be returned, containing infos for the user 109 // about which fields or actions are needed. The user may retry by providing 110 // the needed details via SignDataWithPassphrase, or by other means (e.g. unlock 111 // the account in a keystore). 112 SignData(account Account, mimeType string, data []byte) ([]byte, error) 113 114 // SignDataWithPassphrase is identical to SignData, but also takes a password 115 // NOTE: there's a chance that an erroneous call might mistake the two strings, and 116 // supply password in the mimetype field, or vice versa. Thus, an implementation 117 // should never echo the mimetype or return the mimetype in the error-response 118 SignDataWithPassphrase(account Account, passphrase, mimeType string, data []byte) ([]byte, error) 119 120 // SignText requests the wallet to sign the hash of a given piece of data, prefixed 121 // by the Zond prefix scheme 122 // It looks up the account specified either solely via its address contained within, 123 // or optionally with the aid of any location metadata from the embedded URL field. 124 // 125 // If the wallet requires additional authentication to sign the request (e.g. 126 // a password to decrypt the account, or a PIN code to verify the transaction), 127 // an AuthNeededError instance will be returned, containing infos for the user 128 // about which fields or actions are needed. The user may retry by providing 129 // the needed details via SignTextWithPassphrase, or by other means (e.g. unlock 130 // the account in a keystore). 131 // 132 // This method should return the signature in 'canonical' format, with v 0 or 1. 133 SignText(account Account, text []byte) ([]byte, error) 134 135 // SignTextWithPassphrase is identical to Signtext, but also takes a password 136 SignTextWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error) 137 138 // SignTx requests the wallet to sign the given transaction. 139 // 140 // It looks up the account specified either solely via its address contained within, 141 // or optionally with the aid of any location metadata from the embedded URL field. 142 // 143 // If the wallet requires additional authentication to sign the request (e.g. 144 // a password to decrypt the account, or a PIN code to verify the transaction), 145 // an AuthNeededError instance will be returned, containing infos for the user 146 // about which fields or actions are needed. The user may retry by providing 147 // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock 148 // the account in a keystore). 149 SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) 150 151 // SignTxWithPassphrase is identical to SignTx, but also takes a password 152 SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) 153 } 154 155 // Backend is a "wallet provider" that may contain a batch of accounts they can 156 // sign transactions with and upon request, do so. 157 type Backend interface { 158 // Wallets retrieves the list of wallets the backend is currently aware of. 159 // 160 // The returned wallets are not opened by default. For software HD wallets this 161 // means that no base seeds are decrypted, and for hardware wallets that no actual 162 // connection is established. 163 // 164 // The resulting wallet list will be sorted alphabetically based on its internal 165 // URL assigned by the backend. Since wallets (especially hardware) may come and 166 // go, the same wallet might appear at a different positions in the list during 167 // subsequent retrievals. 168 Wallets() []Wallet 169 170 // Subscribe creates an async subscription to receive notifications when the 171 // backend detects the arrival or departure of a wallet. 172 Subscribe(sink chan<- WalletEvent) event.Subscription 173 } 174 175 // TextHash is a helper function that calculates a hash for the given message that can be 176 // safely used to calculate a signature from. 177 // 178 // The hash is calculated as 179 // 180 // keccak256("\x19Zond Signed Message:\n"${message length}${message}). 181 // 182 // This gives context to the signed message and prevents signing of transactions. 183 func TextHash(data []byte) []byte { 184 hash, _ := TextAndHash(data) 185 return hash 186 } 187 188 // TextAndHash is a helper function that calculates a hash for the given message that can be 189 // safely used to calculate a signature from. 190 // 191 // The hash is calculated as 192 // 193 // keccak256("\x19Zond Signed Message:\n"${message length}${message}). 194 // 195 // This gives context to the signed message and prevents signing of transactions. 196 func TextAndHash(data []byte) ([]byte, string) { 197 msg := fmt.Sprintf("\x19Zond Signed Message:\n%d%s", len(data), string(data)) 198 hasher := sha3.NewLegacyKeccak256() 199 hasher.Write([]byte(msg)) 200 return hasher.Sum(nil), msg 201 } 202 203 // WalletEventType represents the different event types that can be fired by 204 // the wallet subscription subsystem. 205 type WalletEventType int 206 207 const ( 208 // WalletArrived is fired when a new wallet is detected either via USB or via 209 // a filesystem event in the keystore. 210 WalletArrived WalletEventType = iota 211 212 // WalletOpened is fired when a wallet is successfully opened with the purpose 213 // of starting any background processes such as automatic key derivation. 214 WalletOpened 215 216 // WalletDropped 217 WalletDropped 218 ) 219 220 // WalletEvent is an event fired by an account backend when a wallet arrival or 221 // departure is detected. 222 type WalletEvent struct { 223 Wallet Wallet // Wallet instance arrived or departed 224 Kind WalletEventType // Event type that happened in the system 225 }