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