|
| 1 | +package usbwallet |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/accounts" |
| 8 | + "github.com/ethereum/go-ethereum/crypto" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + // Add support for ledger sign personal message. |
| 13 | + ledgerP1InitPersonalMessageData ledgerParam1 = 0x00 // First chunk of Personal Message data |
| 14 | + ledgerP1ContPersonalMessageData ledgerParam1 = 0x80 // Next chunk of Personal Message data |
| 15 | + ledgerOpSignPersonalMessage ledgerOpcode = 0x08 // Signs an ethereum personal message |
| 16 | +) |
| 17 | + |
| 18 | +// signMessage implements accounts.Wallet |
| 19 | +// Used for EIP191 (personal signing). |
| 20 | +func (w *wallet) signMessage(account accounts.Account, message []byte) ([]byte, error) { |
| 21 | + w.stateLock.RLock() // Comms have own mutex, this is for the state fields |
| 22 | + defer w.stateLock.RUnlock() |
| 23 | + |
| 24 | + // If the wallet is closed, abort |
| 25 | + if w.device == nil { |
| 26 | + return nil, accounts.ErrWalletClosed |
| 27 | + } |
| 28 | + // Make sure the requested account is contained within |
| 29 | + path, ok := w.paths[account.Address] |
| 30 | + if !ok { |
| 31 | + return nil, accounts.ErrUnknownAccount |
| 32 | + } |
| 33 | + // All infos gathered and metadata checks out, request signing |
| 34 | + <-w.commsLock |
| 35 | + defer func() { w.commsLock <- struct{}{} }() |
| 36 | + |
| 37 | + // Ensure the device isn't screwed with while user confirmation is pending |
| 38 | + // TODO(karalabe): remove if hotplug lands on Windows |
| 39 | + w.hub.commsLock.Lock() |
| 40 | + w.hub.commsPend++ |
| 41 | + w.hub.commsLock.Unlock() |
| 42 | + |
| 43 | + defer func() { |
| 44 | + w.hub.commsLock.Lock() |
| 45 | + w.hub.commsPend-- |
| 46 | + w.hub.commsLock.Unlock() |
| 47 | + }() |
| 48 | + signature, err := w.driver.SignPersonalMessage(path, message) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + return signature, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (w *ledgerDriver) SignPersonalMessage(path accounts.DerivationPath, message []byte) ([]byte, error) { |
| 56 | + // If the Ethereum app doesn't run, abort |
| 57 | + if w.offline() { |
| 58 | + return nil, accounts.ErrWalletClosed |
| 59 | + } |
| 60 | + // Ensure the wallet is capable of signing the given transaction |
| 61 | + if w.version[0] < 1 && w.version[1] < 2 { |
| 62 | + //lint:ignore ST1005 brand name displayed on the console |
| 63 | + return nil, fmt.Errorf("Ledger version >= 1.2.0 required for personal signing (found version v%d.%d.%d)", w.version[0], w.version[1], w.version[2]) |
| 64 | + } |
| 65 | + return w.ledgerSignPersonalMessage(path, message) |
| 66 | +} |
| 67 | + |
| 68 | +func (w *ledgerDriver) ledgerSignPersonalMessage(derivationPath []uint32, message []byte) ([]byte, error) { |
| 69 | + // Flatten the derivation path into the Ledger request |
| 70 | + path := make([]byte, 1+4*len(derivationPath)) |
| 71 | + path[0] = byte(len(derivationPath)) |
| 72 | + for i, component := range derivationPath { |
| 73 | + binary.BigEndian.PutUint32(path[1+4*i:], component) |
| 74 | + } |
| 75 | + var messageLength [4]byte |
| 76 | + binary.BigEndian.PutUint32(messageLength[:], uint32(len(message))) |
| 77 | + payload := append(path, messageLength[:]...) |
| 78 | + payload = append(payload, message...) |
| 79 | + // Send the request and wait for the response |
| 80 | + var ( |
| 81 | + op = ledgerP1InitPersonalMessageData |
| 82 | + reply []byte |
| 83 | + err error |
| 84 | + ) |
| 85 | + fmt.Println("Derivation path: ", path) |
| 86 | + // Chunk size selection to mitigate an underlying RLP deserialization issue on the ledger app. |
| 87 | + // https://github.com/LedgerHQ/app-ethereum/issues/409 |
| 88 | + chunk := 255 |
| 89 | + for ; len(payload)%chunk <= ledgerEip155Size; chunk-- { |
| 90 | + } |
| 91 | + |
| 92 | + for len(payload) > 0 { |
| 93 | + // Calculate the size of the next data chunk |
| 94 | + if chunk > len(payload) { |
| 95 | + chunk = len(payload) |
| 96 | + } |
| 97 | + // Send the chunk over, ensuring it's processed correctly |
| 98 | + reply, err = w.ledgerExchange(ledgerOpSignPersonalMessage, op, 0, payload[:chunk]) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + // Shift the payload and ensure subsequent chunks are marked as such |
| 103 | + payload = payload[chunk:] |
| 104 | + op = ledgerP1ContPersonalMessageData |
| 105 | + } |
| 106 | + |
| 107 | + // Extract the Ethereum signature and do a sanity validation |
| 108 | + if len(reply) != crypto.SignatureLength { |
| 109 | + return nil, fmt.Errorf("reply lacks signature: reploy %v", reply) |
| 110 | + } |
| 111 | + signature := append(reply[1:], reply[0]) |
| 112 | + return signature, nil |
| 113 | +} |
0 commit comments