wallet.go 19 KB

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