Skip to content

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions accounts/usbwallet/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (w *ledgerDriver) SignTx(path accounts.DerivationPath, tx *types.Transactio
return w.ledgerSign(path, tx, chainID)
}

// SignData sends a blob of data to the USB device and waits for the user to confirm
// or deny the signing request.
func (w *ledgerDriver) SignData(path accounts.DerivationPath, data []byte) (common.Address, []byte, error) {
return common.Address{}, nil, accounts.ErrNotSupported
}

// ledgerVersion retrieves the current version of the Ethereum wallet app running
// on the Ledger wallet.
//
Expand Down
23 changes: 23 additions & 0 deletions accounts/usbwallet/trezor.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ func (w *trezorDriver) SignTx(path accounts.DerivationPath, tx *types.Transactio
return w.trezorSign(path, tx, chainID)
}

// SignData sends a blob of data to the USB device and waits for the user to confirm
// or deny the signing request.
func (w *trezorDriver) SignData(path accounts.DerivationPath, data []byte) (common.Address, []byte, error) {
request := &trezor.EthereumSignMessage{
AddressN: path,
Message: data,
}
response := new(trezor.EthereumMessageSignature)
if _, err := w.trezorExchange(request, response); err != nil {
return common.Address{}, nil, err
}

var address common.Address
if addr := response.GetAddressBin(); len(addr) > 0 { // Older firmwares use binary fomats
address = common.BytesToAddress(addr)
}
if addr := response.GetAddressHex(); len(addr) > 0 { // Newer firmwares use hexadecimal fomats
address = common.HexToAddress(addr)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls drop empty line.

return address, response.Signature, nil
}

// trezorDerive sends a derivation request to the Trezor device and returns the
// Ethereum address located on that path.
func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, error) {
Expand Down
52 changes: 46 additions & 6 deletions accounts/usbwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about passing a hash vs. the data itself. Won't some hardware wallet or Clef itself need the entire data?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do :)

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand All @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down
14 changes: 9 additions & 5 deletions signer/core/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ func (api *SignerAPI) sign(addr common.MixedcaseAddress, req *SignDataRequest, l
if err != nil {
return nil, err
}
pw, err := api.lookupOrQueryPassword(account.Address,
"Password for signing",
fmt.Sprintf("Please enter password for signing data with account %s", account.Address.Hex()))
if err != nil {
return nil, err
// Only ask for the passphrase if the account is keystore based
pw := ""
if wallet.URL().Scheme == "keystore" {
pw, err = api.lookupOrQueryPassword(account.Address,
"Password for signing",
fmt.Sprintf("Please enter password for signing data with account %s", account.Address.Hex()))
if err != nil {
return nil, err
}
}
// Sign the data with the wallet
signature, err := wallet.SignDataWithPassphrase(account, pw, req.ContentType, req.Rawdata)
Expand Down