wallet.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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/hid"
  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 hid.DeviceInfo // Known USB device infos about the wallet
  68. device *hid.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. deriveNextPath accounts.DerivationPath // Next derivation path for account auto-discovery
  72. deriveNextAddr common.Address // Next derived account address for auto-discovery
  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. w.driver.Close()
  242. return nil
  243. }
  244. // Accounts implements accounts.Wallet, returning the list of accounts pinned to
  245. // the USB hardware wallet. If self-derivation was enabled, the account list is
  246. // periodically expanded based on current chain state.
  247. func (w *wallet) Accounts() []accounts.Account {
  248. // Attempt self-derivation if it's running
  249. reqc := make(chan struct{}, 1)
  250. select {
  251. case w.deriveReq <- reqc:
  252. // Self-derivation request accepted, wait for it
  253. <-reqc
  254. default:
  255. // Self-derivation offline, throttled or busy, skip
  256. }
  257. // Return whatever account list we ended up with
  258. w.stateLock.RLock()
  259. defer w.stateLock.RUnlock()
  260. cpy := make([]accounts.Account, len(w.accounts))
  261. copy(cpy, w.accounts)
  262. return cpy
  263. }
  264. // selfDerive is an account derivation loop that upon request attempts to find
  265. // new non-zero accounts.
  266. func (w *wallet) selfDerive() {
  267. w.log.Debug("USB wallet self-derivation started")
  268. defer w.log.Debug("USB wallet self-derivation stopped")
  269. // Execute self-derivations until termination or error
  270. var (
  271. reqc chan struct{}
  272. errc chan error
  273. err error
  274. )
  275. for errc == nil && err == nil {
  276. // Wait until either derivation or termination is requested
  277. select {
  278. case errc = <-w.deriveQuit:
  279. // Termination requested
  280. continue
  281. case reqc = <-w.deriveReq:
  282. // Account discovery requested
  283. }
  284. // Derivation needs a chain and device access, skip if either unavailable
  285. w.stateLock.RLock()
  286. if w.device == nil || w.deriveChain == nil {
  287. w.stateLock.RUnlock()
  288. reqc <- struct{}{}
  289. continue
  290. }
  291. select {
  292. case <-w.commsLock:
  293. default:
  294. w.stateLock.RUnlock()
  295. reqc <- struct{}{}
  296. continue
  297. }
  298. // Device lock obtained, derive the next batch of accounts
  299. var (
  300. accs []accounts.Account
  301. paths []accounts.DerivationPath
  302. nextAddr = w.deriveNextAddr
  303. nextPath = w.deriveNextPath
  304. context = context.Background()
  305. )
  306. for empty := false; !empty; {
  307. // Retrieve the next derived Ethereum account
  308. if nextAddr == (common.Address{}) {
  309. if nextAddr, err = w.driver.Derive(nextPath); 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, nextAddr, 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, nextAddr, 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 it nonetheless
  330. if balance.Sign() == 0 && nonce == 0 {
  331. empty = true
  332. }
  333. // We've just self-derived a new account, start tracking it locally
  334. path := make(accounts.DerivationPath, len(nextPath))
  335. copy(path[:], nextPath[:])
  336. paths = append(paths, path)
  337. account := accounts.Account{
  338. Address: nextAddr,
  339. URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
  340. }
  341. accs = append(accs, account)
  342. // Display a log message to the user for new (or previously empty accounts)
  343. if _, known := w.paths[nextAddr]; !known || (!empty && nextAddr == w.deriveNextAddr) {
  344. w.log.Info("USB wallet discovered new account", "address", nextAddr, "path", path, "balance", balance, "nonce", nonce)
  345. }
  346. // Fetch the next potential account
  347. if !empty {
  348. nextAddr = common.Address{}
  349. nextPath[len(nextPath)-1]++
  350. }
  351. }
  352. // Self derivation complete, release device lock
  353. w.commsLock <- struct{}{}
  354. w.stateLock.RUnlock()
  355. // Insert any accounts successfully derived
  356. w.stateLock.Lock()
  357. for i := 0; i < len(accs); i++ {
  358. if _, ok := w.paths[accs[i].Address]; !ok {
  359. w.accounts = append(w.accounts, accs[i])
  360. w.paths[accs[i].Address] = paths[i]
  361. }
  362. }
  363. // Shift the self-derivation forward
  364. // TODO(karalabe): don't overwrite changes from wallet.SelfDerive
  365. w.deriveNextAddr = nextAddr
  366. w.deriveNextPath = nextPath
  367. w.stateLock.Unlock()
  368. // Notify the user of termination and loop after a bit of time (to avoid trashing)
  369. reqc <- struct{}{}
  370. if err == nil {
  371. select {
  372. case errc = <-w.deriveQuit:
  373. // Termination requested, abort
  374. case <-time.After(selfDeriveThrottling):
  375. // Waited enough, willing to self-derive again
  376. }
  377. }
  378. }
  379. // In case of error, wait for termination
  380. if err != nil {
  381. w.log.Debug("USB wallet self-derivation failed", "err", err)
  382. errc = <-w.deriveQuit
  383. }
  384. errc <- err
  385. }
  386. // Contains implements accounts.Wallet, returning whether a particular account is
  387. // or is not pinned into this wallet instance. Although we could attempt to resolve
  388. // unpinned accounts, that would be an non-negligible hardware operation.
  389. func (w *wallet) Contains(account accounts.Account) bool {
  390. w.stateLock.RLock()
  391. defer w.stateLock.RUnlock()
  392. _, exists := w.paths[account.Address]
  393. return exists
  394. }
  395. // Derive implements accounts.Wallet, deriving a new account at the specific
  396. // derivation path. If pin is set to true, the account will be added to the list
  397. // of tracked accounts.
  398. func (w *wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
  399. // Try to derive the actual account and update its URL if successful
  400. w.stateLock.RLock() // Avoid device disappearing during derivation
  401. if w.device == nil {
  402. w.stateLock.RUnlock()
  403. return accounts.Account{}, accounts.ErrWalletClosed
  404. }
  405. <-w.commsLock // Avoid concurrent hardware access
  406. address, err := w.driver.Derive(path)
  407. w.commsLock <- struct{}{}
  408. w.stateLock.RUnlock()
  409. // If an error occurred or no pinning was requested, return
  410. if err != nil {
  411. return accounts.Account{}, err
  412. }
  413. account := accounts.Account{
  414. Address: address,
  415. URL: accounts.URL{Scheme: w.url.Scheme, Path: fmt.Sprintf("%s/%s", w.url.Path, path)},
  416. }
  417. if !pin {
  418. return account, nil
  419. }
  420. // Pinning needs to modify the state
  421. w.stateLock.Lock()
  422. defer w.stateLock.Unlock()
  423. if _, ok := w.paths[address]; !ok {
  424. w.accounts = append(w.accounts, account)
  425. w.paths[address] = path
  426. }
  427. return account, nil
  428. }
  429. // SelfDerive implements accounts.Wallet, trying to discover accounts that the
  430. // user used previously (based on the chain state), but ones that he/she did not
  431. // explicitly pin to the wallet manually. To avoid chain head monitoring, self
  432. // derivation only runs during account listing (and even then throttled).
  433. func (w *wallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {
  434. w.stateLock.Lock()
  435. defer w.stateLock.Unlock()
  436. w.deriveNextPath = make(accounts.DerivationPath, len(base))
  437. copy(w.deriveNextPath[:], base[:])
  438. w.deriveNextAddr = common.Address{}
  439. w.deriveChain = chain
  440. }
  441. // signHash implements accounts.Wallet, however signing arbitrary data is not
  442. // supported for hardware wallets, so this method will always return an error.
  443. func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error) {
  444. return nil, accounts.ErrNotSupported
  445. }
  446. // SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
  447. func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
  448. return w.signHash(account, crypto.Keccak256(data))
  449. }
  450. // SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given
  451. // data with the given account using passphrase as extra authentication.
  452. // Since USB wallets don't rely on passphrases, these are silently ignored.
  453. func (w *wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) {
  454. return w.SignData(account, mimeType, data)
  455. }
  456. func (w *wallet) SignText(account accounts.Account, text []byte) ([]byte, error) {
  457. return w.signHash(account, accounts.TextHash(text))
  458. }
  459. // SignTx implements accounts.Wallet. It sends the transaction over to the Ledger
  460. // wallet to request a confirmation from the user. It returns either the signed
  461. // transaction or a failure if the user denied the transaction.
  462. //
  463. // Note, if the version of the Ethereum application running on the Ledger wallet is
  464. // too old to sign EIP-155 transactions, but such is requested nonetheless, an error
  465. // will be returned opposed to silently signing in Homestead mode.
  466. func (w *wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  467. w.stateLock.RLock() // Comms have own mutex, this is for the state fields
  468. defer w.stateLock.RUnlock()
  469. // If the wallet is closed, abort
  470. if w.device == nil {
  471. return nil, accounts.ErrWalletClosed
  472. }
  473. // Make sure the requested account is contained within
  474. path, ok := w.paths[account.Address]
  475. if !ok {
  476. return nil, accounts.ErrUnknownAccount
  477. }
  478. // All infos gathered and metadata checks out, request signing
  479. <-w.commsLock
  480. defer func() { w.commsLock <- struct{}{} }()
  481. // Ensure the device isn't screwed with while user confirmation is pending
  482. // TODO(karalabe): remove if hotplug lands on Windows
  483. w.hub.commsLock.Lock()
  484. w.hub.commsPend++
  485. w.hub.commsLock.Unlock()
  486. defer func() {
  487. w.hub.commsLock.Lock()
  488. w.hub.commsPend--
  489. w.hub.commsLock.Unlock()
  490. }()
  491. // Sign the transaction and verify the sender to avoid hardware fault surprises
  492. sender, signed, err := w.driver.SignTx(path, tx, chainID)
  493. if err != nil {
  494. return nil, err
  495. }
  496. if sender != account.Address {
  497. return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
  498. }
  499. return signed, nil
  500. }
  501. // SignHashWithPassphrase implements accounts.Wallet, however signing arbitrary
  502. // data is not supported for Ledger wallets, so this method will always return
  503. // an error.
  504. func (w *wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) {
  505. return w.SignText(account, accounts.TextHash(text))
  506. }
  507. // SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
  508. // transaction with the given account using passphrase as extra authentication.
  509. // Since USB wallets don't rely on passphrases, these are silently ignored.
  510. func (w *wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
  511. return w.SignTx(account, tx, chainID)
  512. }