-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ledger Ethereum chrome://untrusted refactor
This change isolates the ledgerjs libraries required to interact with the Ethereum app on Ledger devices to an iFrame in a chrome untrusted context. It reuses the framework established in the Ledger Solana Ethereum refactor, but generalizes some code such that it can be reused for both Solana and Ethereum. For example: * Shared logic in SolanaLedgerBridgeKeyring class has been moved to a new parent class that LedgerBridgeKeyring that both SolanaLedgerBridgeKeyring and EthereumLedgerBridgeKeyring that both inherit from * Solana specific logic was moved out of LedgerUntrustedMessagingTransport and into a new child class, SolanaLedgerUntrustedMessagingTransport. Similarly, Ethereum specific logic is now in a new child class, EthereumLedgerUntrustedMessagingTransport that inherits from LedgerUntrustedMessagingTransport. * Common message types were kept in ledger-messages.ts, but Solana and Ethereum specific message types were separated into their own sol-ledger-messages.ts and eth-ledger-messages.ts modules respectively.
- Loading branch information
Showing
25 changed files
with
1,424 additions
and
728 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
components/brave_wallet_ui/common/hardware/ledgerjs/eth-ledger-messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import type { | ||
CommandMessage, | ||
LedgerCommand, | ||
LedgerError, | ||
LedgerResponsePayload | ||
} from './ledger-messages' | ||
|
||
// GetAccounts command | ||
export type EthGetAccountResponsePayload = LedgerResponsePayload & { | ||
publicKey: string | ||
address: string | ||
chainCode?: string | ||
} | ||
|
||
export type EthGetAccountResponse = CommandMessage & { | ||
payload: EthGetAccountResponsePayload | LedgerError | ||
} | ||
|
||
export type EthGetAccountCommand = CommandMessage & { | ||
command: LedgerCommand.GetAccount | ||
path: string | ||
} | ||
|
||
// SignTransaction command | ||
export type EthereumSignedTx = { | ||
v: string | ||
r: string | ||
s: string | ||
} | ||
export type EthSignTransactionResponsePayload = LedgerResponsePayload & EthereumSignedTx | ||
|
||
export type EthSignTransactionResponse = CommandMessage & { | ||
payload: EthSignTransactionResponsePayload | LedgerError | ||
} | ||
|
||
export type EthSignTransactionCommand = CommandMessage & { | ||
command: LedgerCommand.SignTransaction | ||
path: string | ||
rawTxHex: string | ||
} | ||
|
||
// SignPersonalMessage command | ||
export type EthSignPersonalMessageResponsePayload = LedgerResponsePayload & { | ||
v: number | ||
r: string | ||
s: string | ||
} | ||
|
||
export type EthSignPersonalMessageResponse = CommandMessage & { | ||
payload: EthSignPersonalMessageResponsePayload | LedgerError | ||
} | ||
|
||
export type EthSignPersonalMessageCommand = CommandMessage & { | ||
command: LedgerCommand.SignPersonalMessage | ||
path: string | ||
messageHex: string | ||
} | ||
|
||
// SignEip712Message command | ||
export type EthSignEip712MessageResponsePayload = EthSignPersonalMessageResponsePayload | ||
|
||
export type EthSignEip712MessageResponse = CommandMessage & { | ||
payload: EthSignEip712MessageResponsePayload | LedgerError | ||
} | ||
|
||
export type EthSignEip712MessageCommand = CommandMessage & { | ||
command: LedgerCommand.SignEip712Message | ||
path: string | ||
domainSeparatorHex: string | ||
hashStructMessageHex: string | ||
} | ||
|
||
export type EthLedgerFrameCommand = EthGetAccountCommand | EthSignTransactionCommand | EthSignPersonalMessageCommand | EthSignEip712MessageCommand | ||
export type EthLedgerFrameResponse = EthGetAccountResponse | EthSignTransactionResponse | EthSignPersonalMessageResponse | EthSignEip712MessageResponse |
162 changes: 162 additions & 0 deletions
162
components/brave_wallet_ui/common/hardware/ledgerjs/eth-ledger-untrusted-transport.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* Copyright (c) 2022 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import TransportWebHID from '@ledgerhq/hw-transport-webhid' | ||
import Eth from '@ledgerhq/hw-app-eth' | ||
import { | ||
LedgerCommand, | ||
UnlockResponse | ||
} from './ledger-messages' | ||
import { | ||
EthGetAccountCommand, | ||
EthGetAccountResponse, | ||
EthGetAccountResponsePayload, | ||
EthSignTransactionCommand, | ||
EthSignTransactionResponsePayload, | ||
EthSignTransactionResponse, | ||
EthSignPersonalMessageCommand, | ||
EthSignPersonalMessageResponsePayload, | ||
EthSignPersonalMessageResponse, | ||
EthSignEip712MessageCommand, | ||
EthSignEip712MessageResponsePayload, | ||
EthSignEip712MessageResponse | ||
} from './eth-ledger-messages' | ||
import { LedgerUntrustedMessagingTransport } from './ledger-untrusted-transport' | ||
|
||
// EthereumLedgerUntrustedMessagingTransport makes calls to the Ethereum app on a Ledger device | ||
export class EthereumLedgerUntrustedMessagingTransport extends LedgerUntrustedMessagingTransport { | ||
constructor (targetWindow: Window, targetUrl: string) { | ||
super(targetWindow, targetUrl) | ||
this.addCommandHandler<UnlockResponse>(LedgerCommand.Unlock, this.handleUnlock) | ||
this.addCommandHandler<EthGetAccountResponse>(LedgerCommand.GetAccount, this.handleGetAccount) | ||
this.addCommandHandler<EthSignTransactionResponse>(LedgerCommand.SignTransaction, this.handleSignTransaction) | ||
this.addCommandHandler<EthSignPersonalMessageResponse>(LedgerCommand.SignPersonalMessage, this.handleSignPersonalMessage) | ||
this.addCommandHandler<EthSignEip712MessageResponse>(LedgerCommand.SignEip712Message, this.handleSignEip712Message) | ||
} | ||
|
||
private handleGetAccount = async (command: EthGetAccountCommand): Promise<EthGetAccountResponse> => { | ||
const transport = await TransportWebHID.create() | ||
const app = new Eth(transport) | ||
try { | ||
const result = await app.getAddress(command.path) | ||
const getAccountResponsePayload: EthGetAccountResponsePayload = { | ||
success: true, | ||
publicKey: result.publicKey, | ||
address: result.address, | ||
chainCode: result.chainCode | ||
} | ||
const response: EthGetAccountResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: getAccountResponsePayload, | ||
origin: command.origin | ||
} | ||
return response | ||
} catch (error) { | ||
const response: EthGetAccountResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: error, | ||
origin: command.origin | ||
} | ||
return response | ||
} finally { | ||
await transport.close() | ||
} | ||
} | ||
|
||
private handleSignTransaction = async (command: EthSignTransactionCommand): Promise<EthSignTransactionResponse> => { | ||
const transport = await TransportWebHID.create() | ||
const app = new Eth(transport) | ||
try { | ||
const result = await app.signTransaction(command.path, command.rawTxHex) | ||
const signTransactionResponsePayload: EthSignTransactionResponsePayload = { | ||
success: true, | ||
v: result.v, | ||
r: result.r, | ||
s: result.s | ||
} | ||
const response: EthSignTransactionResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: signTransactionResponsePayload, | ||
origin: command.origin | ||
} | ||
return response | ||
} catch (error) { | ||
const response: EthSignTransactionResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: error, | ||
origin: command.origin | ||
} | ||
return response | ||
} finally { | ||
await transport.close() | ||
} | ||
} | ||
|
||
private handleSignPersonalMessage = async (command: EthSignPersonalMessageCommand): Promise<EthSignPersonalMessageResponse> => { | ||
const transport = await TransportWebHID.create() | ||
const app = new Eth(transport) | ||
try { | ||
const result = await app.signPersonalMessage(command.path, command.messageHex) | ||
const signPersonalMessageResponsePayload: EthSignPersonalMessageResponsePayload = { | ||
success: true, | ||
v: result.v, | ||
r: result.r, | ||
s: result.s | ||
} | ||
const response: EthSignPersonalMessageResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: signPersonalMessageResponsePayload, | ||
origin: command.origin | ||
} | ||
return response | ||
} catch (error) { | ||
const response: EthSignPersonalMessageResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: error, | ||
origin: command.origin | ||
} | ||
return response | ||
} finally { | ||
await transport.close() | ||
} | ||
} | ||
|
||
private handleSignEip712Message = async (command: EthSignEip712MessageCommand): Promise<EthSignEip712MessageResponse> => { | ||
const transport = await TransportWebHID.create() | ||
const app = new Eth(transport) | ||
try { | ||
const result = await app.signEIP712HashedMessage(command.path, command.domainSeparatorHex, command.hashStructMessageHex) | ||
const signEip712MessageResponsePayload: EthSignEip712MessageResponsePayload = { | ||
success: true, | ||
v: result.v, | ||
r: result.r, | ||
s: result.s | ||
} | ||
const response: EthSignEip712MessageResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: signEip712MessageResponsePayload, | ||
origin: command.origin | ||
} | ||
return response | ||
} catch (error) { | ||
const response: EthSignEip712MessageResponse = { | ||
id: command.id, | ||
command: command.command, | ||
payload: error, | ||
origin: command.origin | ||
} | ||
return response | ||
} finally { | ||
await transport.close() | ||
} | ||
} | ||
} |
Oops, something went wrong.