|
|
|
@ -33,6 +33,7 @@ import ( |
|
|
|
|
ethereum "github.com/ethereum/go-ethereum" |
|
|
|
|
"github.com/ethereum/go-ethereum/accounts" |
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
@ -122,6 +123,8 @@ type ledgerWallet struct { |
|
|
|
|
// must only ever hold a *read* lock to stateLock.
|
|
|
|
|
commsLock chan struct{} // Mutex (buf=1) for the USB comms without keeping the state locked
|
|
|
|
|
stateLock sync.RWMutex // Protects read and write access to the wallet struct fields
|
|
|
|
|
|
|
|
|
|
logger log.Logger // Contextual logger to tag the ledger with its id
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// URL implements accounts.Wallet, returning the URL of the Ledger device.
|
|
|
|
@ -219,8 +222,8 @@ func (w *ledgerWallet) Open(passphrase string) error { |
|
|
|
|
// - libusb on Windows doesn't support hotplug, so we can't detect USB unplugs
|
|
|
|
|
// - communication timeout on the Ledger requires a device power cycle to fix
|
|
|
|
|
func (w *ledgerWallet) heartbeat() { |
|
|
|
|
log.Debug(fmt.Sprintf("%s health-check started", w.url.String())) |
|
|
|
|
defer log.Debug(fmt.Sprintf("%s health-check stopped", w.url.String())) |
|
|
|
|
w.logger.Debug("Ledger health-check started") |
|
|
|
|
defer w.logger.Debug("Ledger health-check stopped") |
|
|
|
|
|
|
|
|
|
// Execute heartbeat checks until termination or error
|
|
|
|
|
var ( |
|
|
|
@ -259,7 +262,7 @@ func (w *ledgerWallet) heartbeat() { |
|
|
|
|
} |
|
|
|
|
// In case of error, wait for termination
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Debug(fmt.Sprintf("%s health-check failed: %v", w.url.String(), err)) |
|
|
|
|
w.logger.Debug("Ledger health-check failed", "error", err) |
|
|
|
|
errc = <-w.healthQuit |
|
|
|
|
} |
|
|
|
|
errc <- err |
|
|
|
@ -347,8 +350,8 @@ func (w *ledgerWallet) Accounts() []accounts.Account { |
|
|
|
|
// selfDerive is an account derivation loop that upon request attempts to find
|
|
|
|
|
// new non-zero accounts.
|
|
|
|
|
func (w *ledgerWallet) selfDerive() { |
|
|
|
|
log.Debug(fmt.Sprintf("%s self-derivation started", w.url.String())) |
|
|
|
|
defer log.Debug(fmt.Sprintf("%s self-derivation stopped", w.url.String())) |
|
|
|
|
w.logger.Debug("Ledger self-derivation started") |
|
|
|
|
defer w.logger.Debug("Ledger self-derivation stopped") |
|
|
|
|
|
|
|
|
|
// Execute self-derivations until termination or error
|
|
|
|
|
var ( |
|
|
|
@ -393,7 +396,7 @@ func (w *ledgerWallet) selfDerive() { |
|
|
|
|
// Retrieve the next derived Ethereum account
|
|
|
|
|
if nextAddr == (common.Address{}) { |
|
|
|
|
if nextAddr, err = w.ledgerDerive(nextPath); err != nil { |
|
|
|
|
log.Warn(fmt.Sprintf("%s self-derivation failed: %v", w.url.String(), err)) |
|
|
|
|
w.logger.Warn("Ledger account derivation failed", "error", err) |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -404,12 +407,12 @@ func (w *ledgerWallet) selfDerive() { |
|
|
|
|
) |
|
|
|
|
balance, err = w.deriveChain.BalanceAt(context, nextAddr, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Warn(fmt.Sprintf("%s self-derivation balance retrieval failed: %v", w.url.String(), err)) |
|
|
|
|
w.logger.Warn("Ledger balance retrieval failed", "error", err) |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
nonce, err = w.deriveChain.NonceAt(context, nextAddr, nil) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Warn(fmt.Sprintf("%s self-derivation nonce retrieval failed: %v", w.url.String(), err)) |
|
|
|
|
w.logger.Warn("Ledger nonce retrieval failed", "error", err) |
|
|
|
|
break |
|
|
|
|
} |
|
|
|
|
// If the next account is empty, stop self-derivation, but add it nonetheless
|
|
|
|
@ -429,7 +432,7 @@ func (w *ledgerWallet) selfDerive() { |
|
|
|
|
|
|
|
|
|
// Display a log message to the user for new (or previously empty accounts)
|
|
|
|
|
if _, known := w.paths[nextAddr]; !known || (!empty && nextAddr == w.deriveNextAddr) { |
|
|
|
|
log.Info(fmt.Sprintf("%s discovered %s (balance %22v, nonce %4d) at %s", w.url.String(), nextAddr.Hex(), balance, nonce, path)) |
|
|
|
|
w.logger.Info("Ledger discovered new account", "address", nextAddr.Hex(), "path", path, "balance", balance, "nonce", nonce) |
|
|
|
|
} |
|
|
|
|
// Fetch the next potential account
|
|
|
|
|
if !empty { |
|
|
|
@ -468,7 +471,7 @@ func (w *ledgerWallet) selfDerive() { |
|
|
|
|
} |
|
|
|
|
// In case of error, wait for termination
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Debug(fmt.Sprintf("%s self-derivation failed: %s", w.url.String(), err)) |
|
|
|
|
w.logger.Debug("Ledger self-derivation failed", "error", err) |
|
|
|
|
errc = <-w.deriveQuit |
|
|
|
|
} |
|
|
|
|
errc <- err |
|
|
|
@ -848,7 +851,7 @@ func (w *ledgerWallet) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 l |
|
|
|
|
apdu = nil |
|
|
|
|
} |
|
|
|
|
// Send over to the device
|
|
|
|
|
log.Trace("", "msg", log.Lazy{Fn: func() string { return fmt.Sprintf("-> %s: %x", w.device.Path, chunk) }}) |
|
|
|
|
w.logger.Trace("Data chunk sent to the Ledger", "chunk", hexutil.Bytes(chunk)) |
|
|
|
|
if _, err := w.device.Write(chunk); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -861,7 +864,7 @@ func (w *ledgerWallet) ledgerExchange(opcode ledgerOpcode, p1 ledgerParam1, p2 l |
|
|
|
|
if _, err := io.ReadFull(w.device, chunk); err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
log.Trace("", "msg", log.Lazy{Fn: func() string { return fmt.Sprintf("<- %s: %x", w.device.Path, chunk) }}) |
|
|
|
|
w.logger.Trace("Data chunk received from the Ledger", "chunk", hexutil.Bytes(chunk)) |
|
|
|
|
|
|
|
|
|
// Make sure the transport header matches
|
|
|
|
|
if chunk[0] != 0x01 || chunk[1] != 0x01 || chunk[2] != 0x05 { |
|
|
|
|