wallet.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 usbwallet implements support for USB hardware wallets.
  17. package usbwallet
  18. import (
  19. "context"
  20. "fmt"
  21. "io"
  22. "math/big"
  23. "sync"
  24. "time"
  25. ethereum "github.com/ethereum/go-ethereum"
  26. "github.com/ethereum/go-ethereum/accounts"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/log"
  31. "github.com/karalabe/usb"
  32. )
  33. // Maximum time between wallet health checks to detect USB unplugs.
  34. const heartbeatCycle = time.Second
  35. // Minimum time to wait between self derivation attempts, even it the user is
  36. // requesting accounts like crazy.
  37. const selfDeriveThrottling = time.Second
  38. // driver defines the vendor specific functionality hardware wallets instances
  39. // must implement to allow using them with the wallet lifecycle management.
  40. type driver interface {
  41. // Status returns a textual status to aid the user in the current state of the
  42. // wallet. It also returns an error indicating any failure the wallet might have
  43. // encountered.
  44. Status() (string, error)
  45. // Open initializes access to a wallet instance. The passphrase parameter may
  46. // or may not be used by the implementation of a particular wallet instance.
  47. Open(device io.ReadWriter, passphrase string) error
  48. // Close releases any resources held by an open wallet instance.
  49. Close() error
  50. // Heartbeat performs a sanity check against the hardware wallet to see if it
  51. // is still online and healthy.
  52. Heartbeat() error
  53. // Derive sends a derivation request to the USB device and returns the Ethereum
  54. // address located on that path.
  55. Derive(path accounts.DerivationPath) (common.Address, error)
  56. // SignTx sends the transaction to the USB device and waits for the user to confirm
  57. // or deny the transaction.
  58. SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error)
  59. }
  60. // wallet represents the common functionality shared by all USB hardware
  61. // wallets to prevent reimplementing the same complex maintenance mechanisms
  62. // for different vendors.
  63. type wallet struct {
  64. hub *Hub // USB hub scanning
  65. driver driver // Hardware implementation of the low level device operations
  66. url *accounts.URL // Textual URL uniquely identifying this wallet
  67. info usb.DeviceInfo // Known USB device infos about the wallet
  68. device usb.Device // USB device advertising itself as a hardware wallet
  69. accounts []accounts.Account // List of derive accounts pinned on the hardware wallet
  70. paths map[common.Address]accounts.DerivationPath // Known derivation paths for signing operations
  71. deriveNextPaths []accounts.DerivationPath // Next derivation paths for account auto-discovery (multiple bases supported)
  72. deriveNextAddrs []common.Address // Next derived account addresses for auto-discovery (multiple bases supported)
  73. deriveChain ethereum.ChainStateReader // Blockchain state reader to discover used account with
  74. deriveReq chan chan struct{} // Channel to request a self-derivation on
  75. deriveQuit chan chan error // Channel to terminate the self-deriver with
  76. healthQuit chan chan error
  77. // Locking a hardware wallet is a bit special. Since hardware devices are lower
  78. // performing, any communication with them might take a non negligible amount of
  79. // time. Worse still, waiting for user confirmation can take arbitrarily long,
  80. // but exclusive communication must be upheld during. Locking the entire wallet
  81. // in the mean time however would stall any parts of the system that don't want
  82. // to communicate, just read some state (e.g. list the accounts).
  83. //
  84. // As such, a hardware wallet needs two locks to function correctly. A state
  85. // lock can be used to protect the wallet's software-side internal state, which
  86. // must not be held exclusively during hardware communication. A communication
  87. // lock can be used to achieve exclusive access to the device itself, this one
  88. // however should allow "skipping" waiting for operations that might want to
  89. // use the device, but can live without too (e.g. account self-derivation).
  90. //
  91. // Since we have two locks, it's important to know how to properly use them:
  92. // - Communication requires the `device` to not change, so obtaining the
  93. // commsLock should be done after having a stateLock.
  94. // - Communication must not disable read access to the wallet state, so it
  95. // must only ever hold a *read* lock to stateLock.
  96. commsLock chan struct{} // Mutex (buf=1) for the USB comms without keeping the state locked
  97. stateLock sync.RWMutex // Protects read and write access to the wallet struct fields
  98. log log.Logger // Contextual logger to tag the base with its id
  99. }
  100. // URL implements accounts.Wallet, returning the URL of the USB hardware device.
  101. func (w *wallet) URL() accounts.URL {
  102. return *w.url // Immutable, no need for a lock
  103. }
  104. // Status implements accounts.Wallet, returning a custom status message from the
  105. // underlying vendor-specific hardware wallet implementation.
  106. func (w *wallet) Status() (string, error) {
  107. w.stateLock.RLock() // No device communication, state lock is enough
  108. defer w.stateLock.RUnlock()
  109. status, failure := w.driver.Status()
  110. if w.device == nil {
  111. return "Closed", failure
  112. }
  113. return status, failure
  114. }
  115. // Open implements accounts.Wallet, attempting to open a USB connection to the
  116. // hardware wallet.
  117. func (w *wallet) Open(passphrase string) error {
  118. w.stateLock.Lock() // State lock is enough since there's no connection yet at this point
  119. defer w.stateLock.Unlock()
  120. // If the device was already opened once, refuse to try again
  121. if w.paths != nil {
  122. return accounts.ErrWalletAlreadyOpen
  123. }
  124. // Make sure the actual device connection is done only once
  125. if w.device == nil {
  126. device, err := w.info.Open()
  127. if err != nil {
  128. return err
  129. }
  130. w.device = device
  131. w.commsLock = make(chan struct{}, 1)
  132. w.commsLock <- struct{}{} // Enable lock
  133. }
  134. // Delegate device initialization to the underlying driver
  135. if err := w.driver.Open(w.device, passphrase); err != nil {
  136. return err
  137. }
  138. // Connection successful, start life-cycle management
  139. w.paths = make(map[common.Address]accounts.DerivationPath)
  140. w.deriveReq = make(chan chan struct{})
  141. w.deriveQuit = make(chan chan error)
  142. w.healthQuit = make(chan chan error)
  143. go w.heartbeat()
  144. go w.selfDerive()
  145. // Notify anyone listening for wallet events that a new device is accessible
  146. go w.hub.updateFeed.Send(accounts.WalletEvent{Wallet: w, Kind: accounts.WalletOpened})
  147. return nil
  148. }
  149. // heartbeat is a health check loop for the USB wallets to periodically verify
  150. // whether they are still present or if they malfunctioned.
  151. func (w *wallet) heartbeat() {
  152. w.log.Debug("USB wallet health-check started")
  153. defer w.log.Debug("USB wallet health-check stopped")
  154. // Execute heartbeat checks until termination or error
  155. var (
  156. errc chan error
  157. err error
  158. )
  159. for errc == nil && err == nil {
  160. // Wait until termination is requested or the heartbeat cycle arrives
  161. select {
  162. case errc = <-w.healthQuit:
  163. // Termination requested
  164. continue
  165. case <-time.After(heartbeatCycle):
  166. // Heartbeat time
  167. }
  168. // Execute a tiny data exchange to see responsiveness
  169. w.stateLock.RLock()
  170. if w.device == nil {
  171. // Terminated while waiting for the lock
  172. w.stateLock.RUnlock()
  173. continue
  174. }
  175. <-w.commsLock // Don't lock state while resolving version
  176. err = w.driver.Heartbeat()
  177. w.commsLock <- struct{}{}
  178. w.stateLock.RUnlock()
  179. if err != nil {
  180. w.stateLock.Lock() // Lock state to tear the wallet down
  181. w.close()
  182. w.stateLock.Unlock()
  183. }
  184. // Ignore non hardware related errors
  185. err = nil
  186. }
  187. // In case of error, wait for termination
  188. if err != nil {
  189. w.log.Debug("USB wallet health-check failed", "err", err)
  190. errc = <-w.healthQuit
  191. }
  192. errc <- err
  193. }
  194. // Close implements accounts.Wallet, closing the USB connection to the device.
  195. func (w *wallet) Close() error {
  196. // Ensure the wallet was opened
  197. w.stateLock.RLock()
  198. hQuit, dQuit := w.healthQuit, w.deriveQuit
  199. w.stateLock.RUnlock()
  200. // Terminate the health checks
  201. var herr error
  202. if hQuit != nil {
  203. errc := make(chan error)
  204. hQuit <- errc
  205. herr = <-errc // Save for later, we *must* close the USB
  206. }
  207. // Terminate the self-derivations
  208. var derr error
  209. if dQuit != nil {
  210. errc := make(chan error)
  211. dQuit <- errc
  212. derr = <-errc // Save for later, we *must* close the USB
  213. }
  214. // Terminate the device connection
  215. w.stateLock.Lock()
  216. defer w.stateLock.Unlock()
  217. w.healthQuit = nil
  218. w.deriveQuit = nil
  219. w.deriveReq = nil
  220. if err := w.close(); err != nil {
  221. return err
  222. }
  223. if herr != nil {
  224. return herr
  225. }
  226. return derr
  227. }
  228. // close is the internal wallet closer that terminates the USB connection and
  229. // resets all the fields to their defaults.
  230. //
  231. // Note, close assumes the state lock is held!
  232. func (w *wallet) close() error {
  233. // Allow duplicate closes, especially for health-check failures
  234. if w.device == nil {
  235. return nil
  236. }
  237. // Close the device, clear everything, then return
  238. w.device.Close()
  239. w.device = nil
  240. w.accounts, w.paths = nil, nil
  241. return w.driver.Close()
  242. }
  243. // Accounts implements accounts.Wallet, returning the list of accounts pinned to
  244. // the USB hardware wallet. If self-derivation was enabled, the account list is
  245. // periodically expanded based on current chain state.
  246. func (w *wallet) Accounts() []accounts.Account {
  247. // Attempt self-derivation if it's running
  248. reqc := make(chan struct{}, 1)
  249. select {
  250. case w.deriveReq <- reqc:
  251. // Self-derivation request accepted, wait for it
  252. <-reqc
  253. default:
  254. // Self-derivation offline, throttled or busy, skip
  255. }
  256. // Return whatever account list we ended up with
  257. w.stateLock.RLock()
  258. defer w.stateLock.RUnlock()
  259. cpy := make([]accounts.Account, len(w.accounts))
  260. copy(cpy, w.accounts)
  261. return cpy
  262. }
  263. // selfDerive is an account derivation loop that upon request attempts to find
  264. // new non-zero accounts.
  265. func (w *wallet) selfDerive() {
  266. w.log.Debug("USB wallet self-derivation started")
  267. defer w.log.Debug("USB wallet self-derivation stopped")
  268. // Execute self-derivations until termination or error
  269. var (
  270. reqc chan struct{}
  271. errc chan error
  272. err error
  273. )
  274. for errc == nil && err == nil {
  275. // Wait until either derivation or termination is requested
  276. select {
  277. case errc = <-w.deriveQuit:
  278. // Termination requested
  279. continue
  280. case reqc = <-w.deriveReq:
  281. // Account discovery requested
  282. }
  283. // Derivation needs a chain and device access, skip if either unavailable
  284. w.stateLock.RLock()
  285. if w.device == nil || w.deriveChain == nil {
  286. w.stateLock.RUnlock()
  287. reqc <- struct{}{}
  288. continue
  289. }
  290. select {
  291. case <-w.commsLock:
  292. default:
  293. w.stateLock.RUnlock()
  294. reqc <- struct{}{}
  295. continue
  296. }
  297. // Device lock obtained, derive the next batch of accounts
  298. var (
  299. accs []accounts.Account
  300. paths []accounts.DerivationPath
  301. nextPaths = append([]accounts.DerivationPath{}, w.deriveNextPaths...)
  302. nextAddrs = append([]common.Address{}, w.deriveNextAddrs...)
  303. context = context.Background()
  304. )
  305. for i := 0; i < len(nextAddrs); i++ {
  306. for empty := false; !empty; {
  307. // Retrieve the next derived Ethereum account
  308. if nextAddrs[i] == (common.Address{}) {
  309. if nextAddrs[i], err = w.driver.Derive(nextPaths[i]); err != nil {
  310. w.log.Warn("USB wallet account derivation failed", "err", err)
  311. break
  312. }
  313. }
  314. // Check the account's status against the current chain state
  315. var (
  316. balance *big.Int
  317. nonce uint64
  318. )
  319. balance, err = w.deriveChain.BalanceAt(context, nextAddrs[i], nil)
  320. if err != nil {
  321. w.log.Warn("USB wallet balance retrieval failed", "err", err)
  322. break
  323. }
  324. nonce, err = w.deriveChain.NonceAt(context, nextAddrs[i], nil)
  325. if err != nil {
  326. w.log.Warn("USB wallet nonce retrieval failed", "err", err)
  327. break
  328. }
  329. // If the next account is empty, stop self-derivation, but add for the last base path
  330. if balance.Sign() == 0 && nonce == 0 {
  331. empty = true
  332. if i < len(nextAddrs)-1 {
  333. break
  334. }
  335. }
  336. // We've just self-derived a new account, start tracking it locally
  337. path := make(accounts.DerivationPath, len(nextPaths[i]))
  338. copy(path[:], nextPaths[i][:])
  339. paths = append(paths, path)
  340. account := accounts.Account{
  341. Address: nextAddrs[i],
  342. URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
  343. }
  344. accs = append(accs, account)
  345. // Display a log message to the user for new (or previously empty accounts)
  346. if _, known := w.paths[nextAddrs[i]]; !known || (!empty && nextAddrs[i] == w.deriveNextAddrs[i]) {
  347. w.log.Info("USB wallet discovered new account", "address", nextAddrs[i], "path", path, "balance", balance, "nonce", nonce)
  348. }
  349. // Fetch the next potential account
  350. if !empty {
  351. nextAddrs[i] = common.Address{}
  352. nextPaths[i][len(nextPaths[i])-1]++
  353. }
  354. }
  355. }
  356. // Self derivation complete, release device lock
  357. w.commsLock <- struct{}{}
  358. w.stateLock.RUnlock()
  359. // Insert any accounts successfully derived
  360. w.stateLock.Lock()
  361. for i := 0; i < len(accs); i++ {
  362. if _, ok := w.paths[accs[i].Address]; !ok {
  363. w.accounts = append(w.accounts, accs[i])
  364. w.paths[accs[i].Address] = paths[i]
  365. }
  366. }
  367. // Shift the self-derivation forward
  368. // TODO(karalabe): don't overwrite changes from wallet.SelfDerive
  369. w.deriveNextAddrs = nextAddrs
  370. w.deriveNextPaths = nextPaths
  371. w.stateLock.Unlock()
  372. // Notify the user of termination and loop after a bit of time (to avoid trashing)
  373. reqc <- struct{}{}
  374. if err == nil {
  375. select {
  376. case errc = <-w.deriveQuit:
  377. // Termination requested, abort
  378. case <-time.After(selfDeriveThrottling):
  379. // Waited enough, willing to self-derive again
  380. }
  381. }
  382. }
  383. // In case of error, wait for termination
  384. if err != nil {
  385. w.log.Debug("USB wallet self-derivation failed", "err", err)
  386. errc = <-w.deriveQuit
  387. }
  388. errc <- err
  389. }
  390. // Contains implements accounts.Wallet, returning whether a particular account is
  391. // or is not pinned into this wallet instance. Although we could attempt to resolve
  392. // unpinned accounts, that would be an non-negligible hardware operation.
  393. func (w *wallet) Contains(account accounts.Account) bool {
  394. w.stateLock.RLock()
  395. defer w.stateLock.RUnlock()
  396. _, exists := w.paths[account.Address]
  397. return exists
  398. }
  399. // Derive implements accounts.Wallet, deriving a new account at the specific
  400. // derivation path. If pin is set to true, the account will be added to the list
  401. // of tracked accounts.
  402. func (w *wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
  403. // Try to derive the actual account and update its URL if successful
  404. w.stateLock.RLock() // Avoid device disappearing during derivation
  405. if w.device == nil {
  406. w.stateLock.RUnlock()
  407. return accounts.Account{}, accounts.ErrWalletClosed
  408. }
  409. <-w.commsLock // Avoid concurrent hardware access
  410. address, err := w.driver.Derive(path)
  411. w.commsLock <- struct{}{}
  412. w.stateLock.RUnlock()
  413. // If an error occurred or no pinning was requested, return
  414. if err != nil {
  415. return accounts.Account{}, err
  416. }
  417. account := accounts.Account{
  418. Address: address,
  419. URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
  420. }
  421. if !pin {
  422. return account, nil
  423. }
  424. // Pinning needs to modify the state
  425. w.stateLock.Lock()
  426. defer w.stateLock.Unlock()
  427. if _, ok := w.paths[address]; !ok {
  428. w.accounts = append(w.accounts, account)
  429. w.paths[address] = path
  430. }
  431. return account, nil
  432. }
  433. // SelfDerive sets a base account derivation path from which the wallet attempts
  434. // to discover non zero accounts and automatically add them to list of tracked
  435. // accounts.
  436. //
  437. // Note, self derivaton will increment the last component of the specified path
  438. // opposed to decending into a child path to allow discovering accounts starting
  439. // from non zero components.
  440. //
  441. // Some hardware wallets switched derivation paths through their evolution, so
  442. // this method supports providing multiple bases to discover old user accounts
  443. // too. Only the last base will be used to derive the next empty account.
  444. //
  445. // You can disable automatic account discovery by calling SelfDerive with a nil
  446. // chain state reader.
  447. func (w *wallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) {
  448. w.stateLock.Lock()
  449. defer w.stateLock.Unlock()
  450. w.deriveNextPaths = make([]accounts.DerivationPath, len(bases))
  451. for i, base := range bases {
  452. w.deriveNextPaths[i] = make(accounts.DerivationPath, len(base))
  453. copy(w.deriveNextPaths[i][:], base[:])
  454. }
  455. w.deriveNextAddrs = make([]common.Address, len(bases))
  456. w.deriveChain = chain
  457. }
  458. // signHash implements accounts.Wallet, however signing arbitrary data is not
  459. // supported for hardware wallets, so this method will always return an error.
  460. func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
  461. return nil, accounts.ErrNotSupported
  462. }
  463. // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
  464. func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
  465. return w.signHash(account, crypto.Keccak256(data))
  466. }
  467. // SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given
  468. // data with the given account using passphrase as extra authentication.
  469. // Since USB wallets don't rely on passphrases, these are silently ignored.
  470. func (w *wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
  471. return w.SignData(account, mimeType, data)
  472. }
  473. func (w *wallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
  474. return w.signHash(account, accounts.TextHash(text))
  475. }
  476. // SignTx implements accounts.Wallet. It sends the transaction over to the Ledger
  477. // wallet to request a confirmation from the user. It returns either the signed
  478. // transaction or a failure if the user denied the transaction.
  479. //
  480. // Note, if the version of the Ethereum application running on the Ledger wallet is
  481. // too old to sign EIP-155 transactions, but such is requested nonetheless, an error
  482. // will be returned opposed to silently signing in Homestead mode.
  483. func (w *wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  484. w.stateLock.RLock() // Comms have own mutex, this is for the state fields
  485. defer w.stateLock.RUnlock()
  486. // If the wallet is closed, abort
  487. if w.device == nil {
  488. return nil, accounts.ErrWalletClosed
  489. }
  490. // Make sure the requested account is contained within
  491. path, ok := w.paths[account.Address]
  492. if !ok {
  493. return nil, accounts.ErrUnknownAccount
  494. }
  495. // All infos gathered and metadata checks out, request signing
  496. <-w.commsLock
  497. defer func() { w.commsLock <- struct{}{} }()
  498. // Ensure the device isn't screwed with while user confirmation is pending
  499. // TODO(karalabe): remove if hotplug lands on Windows
  500. w.hub.commsLock.Lock()
  501. w.hub.commsPend++
  502. w.hub.commsLock.Unlock()
  503. defer func() {
  504. w.hub.commsLock.Lock()
  505. w.hub.commsPend--
  506. w.hub.commsLock.Unlock()
  507. }()
  508. // Sign the transaction and verify the sender to avoid hardware fault surprises
  509. sender, signed, err := w.driver.SignTx(path, tx, chainID)
  510. if err != nil {
  511. return nil, err
  512. }
  513. if sender != account.Address {
  514. return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
  515. }
  516. return signed, nil
  517. }
  518. // SignHashWithPassphrase implements accounts.Wallet, however signing arbitrary
  519. // data is not supported for Ledger wallets, so this method will always return
  520. // an error.
  521. func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
  522. return w.SignText(account, accounts.TextHash(text))
  523. }
  524. // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
  525. // transaction with the given account using passphrase as extra authentication.
  526. // Since USB wallets don't rely on passphrases, these are silently ignored.
  527. func (w *wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  528. return w.SignTx(account, tx, chainID)
  529. }