-
Notifications
You must be signed in to change notification settings - Fork 20.9k
accounts/usbwallet: trezor signed message support #19831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ import ( | |
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/karalabe/usb" | ||
) | ||
|
@@ -67,6 +66,10 @@ type driver interface { | |
// SignTx sends the transaction to the USB device and waits for the user to confirm | ||
// or deny the transaction. | ||
SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) | ||
|
||
// SignData sends a blob of data to the USB device and waits for the user to confirm | ||
// or deny the signing request. | ||
SignData(path accounts.DerivationPath, hash []byte) (common.Address, []byte, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure about passing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually the message itself is passed, I'm going to change the variable name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter name is still hash |
||
} | ||
|
||
// wallet represents the common functionality shared by all USB hardware | ||
|
@@ -512,15 +515,52 @@ func (w *wallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.Chai | |
w.deriveChain = chain | ||
} | ||
|
||
// signHash implements accounts.Wallet, however signing arbitrary data is not | ||
// signData implements accounts.Wallet, however signing arbitrary data is not | ||
// supported for hardware wallets, so this method will always return an error. | ||
func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error) { | ||
return nil, accounts.ErrNotSupported | ||
func (w *wallet) signData(account accounts.Account, data []byte) ([]byte, error) { | ||
w.stateLock.RLock() // Comms have their own mutex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this method should be renamed signData? Also please rename the hash variables everywhere to data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please read the comment and update it too accordingly :P |
||
defer w.stateLock.RUnlock() | ||
|
||
// If the wallet is closed, abort | ||
if w.device == nil { | ||
return nil, accounts.ErrWalletClosed | ||
} | ||
// Make sure the requested account is contained within | ||
path, ok := w.paths[account.Address] | ||
if !ok { | ||
return nil, accounts.ErrUnknownAccount | ||
} | ||
|
||
// All infos gathered and metadata checks out, request signing | ||
<-w.commsLock | ||
defer func() { w.commsLock <- struct{}{} }() | ||
|
||
// Ensure the device isn't screwed with while user confirmation is pending | ||
// TODO(karalabe): remove if hotplug lands on Windows | ||
w.hub.commsLock.Lock() | ||
w.hub.commsPend++ | ||
w.hub.commsLock.Unlock() | ||
|
||
defer func() { | ||
w.hub.commsLock.Lock() | ||
w.hub.commsPend-- | ||
w.hub.commsLock.Unlock() | ||
}() | ||
|
||
// Sign the transaction and verify the sender to avoid hardware fault surprises | ||
sender, signed, err := w.driver.SignData(path, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if sender != account.Address { | ||
return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex()) | ||
} | ||
return signed, nil | ||
} | ||
|
||
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed | ||
func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { | ||
return w.signHash(account, crypto.Keccak256(data)) | ||
return w.signData(account, data) | ||
} | ||
|
||
// SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given | ||
|
@@ -531,7 +571,7 @@ func (w *wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mi | |
} | ||
|
||
func (w *wallet) SignText(account accounts.Account, text []byte) ([]byte, error) { | ||
return w.signHash(account, accounts.TextHash(text)) | ||
return w.signData(account, accounts.TextHash(text)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be wrong now. If we changed the signature to sign data, then we can't hash the text here now. What does sign text even do? It's not my method. Ping @holiman ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an extra catch here that I've spotted yesterday during a debug session: the Trezor actually adds the prefix no matter what, so right now the prefix is added twice. So the whole prefix management has to be changed, I'm still untangling some bits, and it would be interesting to discuss it. |
||
} | ||
|
||
// SignTx implements accounts.Wallet. It sends the transaction over to the Ledger | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls drop empty line.