Skip to content

Commit aab3560

Browse files
authored
accounts: eip-712 signing for ledger (#22378)
* accounts: eip-712 signing for ledger * address review comments
1 parent eaccdba commit aab3560

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

accounts/usbwallet/ledger.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ const (
5252
ledgerOpRetrieveAddress ledgerOpcode = 0x02 // Returns the public key and Ethereum address for a given BIP 32 path
5353
ledgerOpSignTransaction ledgerOpcode = 0x04 // Signs an Ethereum transaction after having the user validate the parameters
5454
ledgerOpGetConfiguration ledgerOpcode = 0x06 // Returns specific wallet application configuration
55+
ledgerOpSignTypedMessage ledgerOpcode = 0x0c // Signs an Ethereum message following the EIP 712 specification
5556

5657
ledgerP1DirectlyFetchAddress ledgerParam1 = 0x00 // Return address directly from the wallet
58+
ledgerP1InitTypedMessageData ledgerParam1 = 0x00 // First chunk of Typed Message data
5759
ledgerP1InitTransactionData ledgerParam1 = 0x00 // First transaction data block for signing
5860
ledgerP1ContTransactionData ledgerParam1 = 0x80 // Subsequent transaction data block for signing
5961
ledgerP2DiscardAddressChainCode ledgerParam2 = 0x00 // Do not return the chain code along with the address
@@ -170,6 +172,24 @@ func (w *ledgerDriver) SignTx(path accounts.DerivationPath, tx *types.Transactio
170172
return w.ledgerSign(path, tx, chainID)
171173
}
172174

175+
// SignTypedMessage implements usbwallet.driver, sending the message to the Ledger and
176+
// waiting for the user to sign or deny the transaction.
177+
//
178+
// Note: this was introduced in the ledger 1.5.0 firmware
179+
func (w *ledgerDriver) SignTypedMessage(path accounts.DerivationPath, domainHash []byte, messageHash []byte) ([]byte, error) {
180+
// If the Ethereum app doesn't run, abort
181+
if w.offline() {
182+
return nil, accounts.ErrWalletClosed
183+
}
184+
// Ensure the wallet is capable of signing the given transaction
185+
if w.version[0] < 1 && w.version[1] < 5 {
186+
//lint:ignore ST1005 brand name displayed on the console
187+
return nil, fmt.Errorf("Ledger version >= 1.5.0 required for EIP-712 signing (found version v%d.%d.%d)", w.version[0], w.version[1], w.version[2])
188+
}
189+
// All infos gathered and metadata checks out, request signing
190+
return w.ledgerSignTypedMessage(path, domainHash, messageHash)
191+
}
192+
173193
// ledgerVersion retrieves the current version of the Ethereum wallet app running
174194
// on the Ledger wallet.
175195
//
@@ -367,6 +387,68 @@ func (w *ledgerDriver) ledgerSign(derivationPath []uint32, tx *types.Transaction
367387
return sender, signed, nil
368388
}
369389

390+
// ledgerSignTypedMessage sends the transaction to the Ledger wallet, and waits for the user
391+
// to confirm or deny the transaction.
392+
//
393+
// The signing protocol is defined as follows:
394+
//
395+
// CLA | INS | P1 | P2 | Lc | Le
396+
// ----+-----+----+-----------------------------+-----+---
397+
// E0 | 0C | 00 | implementation version : 00 | variable | variable
398+
//
399+
// Where the input is:
400+
//
401+
// Description | Length
402+
// -------------------------------------------------+----------
403+
// Number of BIP 32 derivations to perform (max 10) | 1 byte
404+
// First derivation index (big endian) | 4 bytes
405+
// ... | 4 bytes
406+
// Last derivation index (big endian) | 4 bytes
407+
// domain hash | 32 bytes
408+
// message hash | 32 bytes
409+
//
410+
//
411+
//
412+
// And the output data is:
413+
//
414+
// Description | Length
415+
// ------------+---------
416+
// signature V | 1 byte
417+
// signature R | 32 bytes
418+
// signature S | 32 bytes
419+
func (w *ledgerDriver) ledgerSignTypedMessage(derivationPath []uint32, domainHash []byte, messageHash []byte) ([]byte, error) {
420+
// Flatten the derivation path into the Ledger request
421+
path := make([]byte, 1+4*len(derivationPath))
422+
path[0] = byte(len(derivationPath))
423+
for i, component := range derivationPath {
424+
binary.BigEndian.PutUint32(path[1+4*i:], component)
425+
}
426+
// Create the 712 message
427+
payload := append(path, domainHash...)
428+
payload = append(payload, messageHash...)
429+
430+
// Send the request and wait for the response
431+
var (
432+
op = ledgerP1InitTypedMessageData
433+
reply []byte
434+
err error
435+
)
436+
437+
// Send the message over, ensuring it's processed correctly
438+
reply, err = w.ledgerExchange(ledgerOpSignTypedMessage, op, 0, payload)
439+
440+
if err != nil {
441+
return nil, err
442+
}
443+
444+
// Extract the Ethereum signature and do a sanity validation
445+
if len(reply) != crypto.SignatureLength {
446+
return nil, errors.New("reply lacks signature")
447+
}
448+
signature := append(reply[1:], reply[0])
449+
return signature, nil
450+
}
451+
370452
// ledgerExchange performs a data exchange with the Ledger wallet, sending it a
371453
// message and retrieving the response.
372454
//

accounts/usbwallet/trezor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ func (w *trezorDriver) SignTx(path accounts.DerivationPath, tx *types.Transactio
185185
return w.trezorSign(path, tx, chainID)
186186
}
187187

188+
func (w *trezorDriver) SignTypedMessage(path accounts.DerivationPath, domainHash []byte, messageHash []byte) ([]byte, error) {
189+
return nil, accounts.ErrNotSupported
190+
}
191+
188192
// trezorDerive sends a derivation request to the Trezor device and returns the
189193
// Ethereum address located on that path.
190194
func (w *trezorDriver) trezorDerive(derivationPath []uint32) (common.Address, error) {

accounts/usbwallet/wallet.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ type driver interface {
6767
// SignTx sends the transaction to the USB device and waits for the user to confirm
6868
// or deny the transaction.
6969
SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error)
70+
71+
SignTypedMessage(path accounts.DerivationPath, messageHash []byte, domainHash []byte) ([]byte, error)
7072
}
7173

7274
// wallet represents the common functionality shared by all USB hardware
@@ -524,7 +526,46 @@ func (w *wallet) signHash(account accounts.Account, hash []byte) ([]byte, error)
524526

525527
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
526528
func (w *wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) {
527-
return w.signHash(account, crypto.Keccak256(data))
529+
530+
// Unless we are doing 712 signing, simply dispatch to signHash
531+
if !(mimeType == accounts.MimetypeTypedData && len(data) == 66 && data[0] == 0x19 && data[1] == 0x01) {
532+
return w.signHash(account, crypto.Keccak256(data))
533+
}
534+
535+
// dispatch to 712 signing if the mimetype is TypedData and the format matches
536+
w.stateLock.RLock() // Comms have own mutex, this is for the state fields
537+
defer w.stateLock.RUnlock()
538+
539+
// If the wallet is closed, abort
540+
if w.device == nil {
541+
return nil, accounts.ErrWalletClosed
542+
}
543+
// Make sure the requested account is contained within
544+
path, ok := w.paths[account.Address]
545+
if !ok {
546+
return nil, accounts.ErrUnknownAccount
547+
}
548+
// All infos gathered and metadata checks out, request signing
549+
<-w.commsLock
550+
defer func() { w.commsLock <- struct{}{} }()
551+
552+
// Ensure the device isn't screwed with while user confirmation is pending
553+
// TODO(karalabe): remove if hotplug lands on Windows
554+
w.hub.commsLock.Lock()
555+
w.hub.commsPend++
556+
w.hub.commsLock.Unlock()
557+
558+
defer func() {
559+
w.hub.commsLock.Lock()
560+
w.hub.commsPend--
561+
w.hub.commsLock.Unlock()
562+
}()
563+
// Sign the transaction
564+
signature, err := w.driver.SignTypedMessage(path, data[2:34], data[34:66])
565+
if err != nil {
566+
return nil, err
567+
}
568+
return signature, nil
528569
}
529570

530571
// SignDataWithPassphrase implements accounts.Wallet, attempting to sign the given

0 commit comments

Comments
 (0)