Skip to content

Do not allow unlocking without the device id and fix error message for Ledger #12291

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

Merged
merged 1 commit into from
Feb 16, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const createLedgerKeyring = () => {
return ledgerHardwareKeyring
}

const unlockedLedgerKeyring = () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
ledgerHardwareKeyring.deviceId = 'device1'
return ledgerHardwareKeyring
}

test('Extracting accounts from device', () => {
return expect(createLedgerKeyring().getAccounts(-2, 1, LedgerDerivationPaths.LedgerLive))
.resolves.toStrictEqual({
Expand Down Expand Up @@ -85,10 +92,18 @@ test('Check ledger bridge type', () => {
return expect(ledgerHardwareKeyring.type()).toStrictEqual(BraveWallet.LEDGER_HARDWARE_VENDOR)
})

test('Check locks for device', () => {
test('Check locks for device app only', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
expect(ledgerHardwareKeyring.isUnlocked()).toStrictEqual(false)
ledgerHardwareKeyring.app = new MockApp()
expect(ledgerHardwareKeyring.isUnlocked()).toStrictEqual(false)
})

test('Check locks for device app and device id', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
expect(ledgerHardwareKeyring.isUnlocked()).toStrictEqual(false)
ledgerHardwareKeyring.app = new MockApp()
ledgerHardwareKeyring.deviceId = 'test'
expect(ledgerHardwareKeyring.isUnlocked()).toStrictEqual(true)
})

Expand All @@ -102,42 +117,37 @@ test('Extract accounts from locked device', () => {
})

test('Extract accounts from unknown device', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
const ledgerHardwareKeyring = unlockedLedgerKeyring()
return expect(ledgerHardwareKeyring.getAccounts(-2, 1, 'unknown'))
.rejects.toThrow()
})

test('Sign personal message successfully with padding v<27', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
const ledgerHardwareKeyring = unlockedLedgerKeyring()
ledgerHardwareKeyring.app.signature = { v: 0, r: 'b68983', s: 'r68983' }
return expect(ledgerHardwareKeyring.signPersonalMessage(
'm/44\'/60\'/0\'/0/0', 'message'))
.resolves.toStrictEqual({ payload: '0xb68983r6898300', success: true })
})

test('Sign personal message successfully with padding v>=27', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
const ledgerHardwareKeyring = unlockedLedgerKeyring()
ledgerHardwareKeyring.app.signature = { v: 28, r: 'b68983', s: 'r68983' }
return expect(ledgerHardwareKeyring.signPersonalMessage(
'm/44\'/60\'/0\'/0/0', 'message'))
.resolves.toStrictEqual({ payload: '0xb68983r6898301', success: true })
})

test('Sign personal message successfully without padding v>=27', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
const ledgerHardwareKeyring = unlockedLedgerKeyring()
ledgerHardwareKeyring.app.signature = { v: 44, r: 'b68983', s: 'r68983' }
return expect(ledgerHardwareKeyring.signPersonalMessage(
'm/44\'/60\'/0\'/0/0', 'message'))
.resolves.toStrictEqual({ payload: '0xb68983r6898311', success: true })
})

test('Sign personal message successfully without padding v<27', () => {
const ledgerHardwareKeyring = new LedgerBridgeKeyring()
ledgerHardwareKeyring.app = new MockApp()
const ledgerHardwareKeyring = unlockedLedgerKeyring()
ledgerHardwareKeyring.app.signature = { v: 17, r: 'b68983', s: 'r68983' }
return expect(ledgerHardwareKeyring.signPersonalMessage(
'm/44\'/60\'/0\'/0/0', 'message'))
Expand All @@ -146,7 +156,6 @@ test('Sign personal message successfully without padding v<27', () => {

test('Sign personal message failed', () => {
const ledgerHardwareKeyring = createLedgerKeyring()
ledgerHardwareKeyring.app = new MockApp()
return expect(ledgerHardwareKeyring.signPersonalMessage(
'm/44\'/60\'/0\'/0/0', 'message'))
.resolves.toMatchObject({ success: false })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,28 @@ export default class LedgerBridgeKeyring extends LedgerEthereumKeyring {
}

isUnlocked = (): boolean => {
return this.app !== undefined
return this.app !== undefined && this.deviceId !== undefined
}

makeApp = async () => {
this.app = new Eth(await TransportWebHID.create())
}

unlock = async (): Promise<HardwareOperationResult> => {
if (this.app) {
if (this.isUnlocked()) {
return { success: true }
}

await this.makeApp()
if (!this.app) {
return { success: false }
await this.makeApp()
}
if (this.app && !this.deviceId) {
const eth: Eth = this.app
eth.transport.on('disconnect', this.onDisconnected)
const zeroPath = this.getPathForIndex(0, LedgerDerivationPaths.LedgerLive)
const address = (await eth.getAddress(zeroPath)).address
this.deviceId = await hardwareDeviceIdFromAddress(address)
}

const eth: Eth = this.app
eth.transport.on('disconnect', this.onDisconnected)
const zeroPath = this.getPathForIndex(0, LedgerDerivationPaths.LedgerLive)
const address = (await eth.getAddress(zeroPath)).address
this.deviceId = await hardwareDeviceIdFromAddress(address)

return { success: this.isUnlocked() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export default function (props: Props) {
LedgerDerivationPaths.LedgerLive
)
const [showAccountsList, setShowAccountsList] = React.useState<boolean>(false)
const getErrorMessage = (error: any) => {
const getErrorMessage = (error: any, accountTypeName: string) => {
if (error.statusCode && error.statusCode === 27404) { // Unknown Error
return { error: getLocale('braveWalletConnectHardwareInfo2'), userHint: '' }
return { error: getLocale('braveWalletConnectHardwareInfo2').replace('$1', accountTypeName), userHint: '' }
}

if (error.statusCode && (error.statusCode === 27904 || error.statusCode === 26368)) { // INCORRECT_LENGTH or INS_NOT_SUPPORTED
Expand All @@ -81,7 +81,7 @@ export default function (props: Props) {
}).then((result) => {
setAccounts(result)
}).catch((error) => {
setConnectionError(getErrorMessage(error))
setConnectionError(getErrorMessage(error, selectedAccountType.name))
setShowAccountsList(false)
}).finally(
() => setIsConnecting(false)
Expand Down Expand Up @@ -150,7 +150,7 @@ export default function (props: Props) {
setAccounts([...accounts, ...result])
setShowAccountsList(true)
}).catch((error) => {
setConnectionError(getErrorMessage(error))
setConnectionError(getErrorMessage(error, selectedAccountType.name))
setShowAccountsList(false)
}).finally(
() => setIsConnecting(false)
Expand Down