Skip to content

Commit

Permalink
feat: Add ADR8 option for opening Ledger wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Mar 30, 2022
1 parent aea0915 commit 9f3bb9c
Show file tree
Hide file tree
Showing 14 changed files with 615 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const Account = memo((props: AccountProps) => {
</Box>
<Box direction="row-responsive">
<Box align="start" flex="grow" direction="row">
{walletTypes[props.type]} {props.details && <Text size="small">({props.details})</Text>}
{walletTypes[props.type]} {props.details && <Text size="small">&nbsp;({props.details})</Text>}
</Box>
<Box>
<AmountFormatter amount={props.balance} />
Expand Down
43 changes: 30 additions & 13 deletions src/app/lib/ledger.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ledger, LedgerSigner } from './ledger'
import { DerivationPathTypeLegacy, DerivationPathTypeAdr8, Ledger, LedgerSigner } from './ledger'
import OasisApp from '@oasisprotocol/ledger'
import { WalletError, WalletErrors } from 'types/errors'
import { Wallet, WalletType } from 'app/state/wallet/types'
Expand All @@ -18,29 +18,46 @@ describe('Ledger Library', () => {
describe('Ledger', () => {
it('enumerateAccounts should pass when Oasis App is open', async () => {
mockAppIsOpen('Oasis')
const accounts = Ledger.enumerateAccounts({} as any, 0)
const accounts = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy, 0)
await expect(accounts).resolves.toEqual([])
})
it('enumerateAccounts should pass when Oasis App is open', async () => {
mockAppIsOpen('Oasis')
const accounts = Ledger.enumerateAccounts({} as any, DerivationPathTypeAdr8, 0)
await expect(accounts).resolves.toEqual([])
})

it('Should catch "Oasis App is not open"', async () => {
mockAppIsOpen('BOLOS')
const accountsMainMenu = Ledger.enumerateAccounts({} as any, 0)
const accountsMainMenu = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy, 0)
await expect(accountsMainMenu).rejects.toThrowError(WalletError)
await expect(accountsMainMenu).rejects.toHaveProperty('type', WalletErrors.LedgerOasisAppIsNotOpen)

mockAppIsOpen('Ethereum')
const accountsEth = Ledger.enumerateAccounts({} as any, 0)
const accountsEth = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy, 0)
await expect(accountsEth).rejects.toThrowError(WalletError)
await expect(accountsEth).rejects.toHaveProperty('type', WalletErrors.LedgerOasisAppIsNotOpen)
})

it('Should enumerate and return the accounts', async () => {
it('Should enumerate and return adr8 accounts', async () => {
mockAppIsOpen('Oasis')
const pubKey: jest.Mock<any> = OasisApp.prototype.publicKey
pubKey.mockResolvedValueOnce({ return_code: 0x9000, pk: Buffer.from(new Uint8Array([1, 2, 3])) })
pubKey.mockResolvedValueOnce({ return_code: 0x9000, pk: Buffer.from(new Uint8Array([4, 5, 6])) })

const accounts = await Ledger.enumerateAccounts({} as any, DerivationPathTypeAdr8, 2)
expect(accounts).toHaveLength(2)
expect(accounts).toContainEqual({ path: [44, 474, 0], publicKey: new Uint8Array([1, 2, 3]) })
expect(accounts).toContainEqual({ path: [44, 474, 1], publicKey: new Uint8Array([4, 5, 6]) })
})

it('Should enumerate and return legacy accounts', async () => {
mockAppIsOpen('Oasis')
const pubKey: jest.Mock<any> = OasisApp.prototype.publicKey
pubKey.mockResolvedValueOnce({ return_code: 0x9000, pk: Buffer.from(new Uint8Array([1, 2, 3])) })
pubKey.mockResolvedValueOnce({ return_code: 0x9000, pk: Buffer.from(new Uint8Array([4, 5, 6])) })

const accounts = await Ledger.enumerateAccounts({} as any, 2)
const accounts = await Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy, 2)
expect(accounts).toHaveLength(2)
expect(accounts).toContainEqual({ path: [44, 474, 0, 0, 0], publicKey: new Uint8Array([1, 2, 3]) })
expect(accounts).toContainEqual({ path: [44, 474, 0, 0, 1], publicKey: new Uint8Array([4, 5, 6]) })
Expand All @@ -51,7 +68,7 @@ describe('Ledger Library', () => {
const pubKey: jest.Mock<any> = OasisApp.prototype.publicKey
pubKey.mockResolvedValueOnce({ return_code: 0x6804 })

const accounts = Ledger.enumerateAccounts({} as any)
const accounts = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy)
await expect(accounts).rejects.toThrowError(WalletError)
await expect(accounts).rejects.toHaveProperty('type', WalletErrors.LedgerCannotOpenOasisApp)
})
Expand All @@ -61,7 +78,7 @@ describe('Ledger Library', () => {
const pubKey: jest.Mock<any> = OasisApp.prototype.publicKey
pubKey.mockResolvedValueOnce({ return_code: 0x6400 })

const accounts = Ledger.enumerateAccounts({} as any)
const accounts = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy)
await expect(accounts).rejects.toThrowError(WalletError)
await expect(accounts).rejects.toHaveProperty('type', WalletErrors.LedgerAppVersionNotSupported)
})
Expand All @@ -71,7 +88,7 @@ describe('Ledger Library', () => {
const pubKey: jest.Mock<any> = OasisApp.prototype.publicKey
pubKey.mockResolvedValueOnce({ return_code: -1, error_message: 'unknown dummy error' })

const accounts = Ledger.enumerateAccounts({} as any)
const accounts = Ledger.enumerateAccounts({} as any, DerivationPathTypeLegacy)
await expect(accounts).rejects.toThrowError(WalletError)
await expect(accounts).rejects.toThrow(/unknown dummy error/)
await expect(accounts).rejects.toHaveProperty('type', WalletErrors.LedgerUnknownError)
Expand All @@ -96,7 +113,7 @@ describe('Ledger Library', () => {
it('Should fail without USB transport', () => {
const signer = new LedgerSigner({
type: WalletType.Ledger,
path: [44, 474, 0, 0, 0],
path: Ledger.mustGetPath(DerivationPathTypeAdr8, 0),
publicKey: '00',
} as Wallet)

Expand All @@ -111,7 +128,7 @@ describe('Ledger Library', () => {

const signer = new LedgerSigner({
type: WalletType.Ledger,
path: [44, 474, 0, 0, 0],
path: Ledger.mustGetPath(DerivationPathTypeAdr8, 0),
publicKey: 'aabbcc',
} as Wallet)

Expand All @@ -124,7 +141,7 @@ describe('Ledger Library', () => {

const signer = new LedgerSigner({
type: WalletType.Ledger,
path: [44, 474, 0, 0, 0],
path: Ledger.mustGetPath(DerivationPathTypeAdr8, 0),
publicKey: '00',
} as Wallet)

Expand All @@ -140,7 +157,7 @@ describe('Ledger Library', () => {

const signer = new LedgerSigner({
type: WalletType.Ledger,
path: [44, 474, 0, 0, 0],
path: Ledger.mustGetPath(DerivationPathTypeAdr8, 0),
publicKey: '00',
} as Wallet)

Expand Down
18 changes: 16 additions & 2 deletions src/app/lib/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { WalletError, WalletErrors } from 'types/errors'
import { hex2uint } from './helpers'
import type Transport from '@ledgerhq/hw-transport'

export const DerivationPathTypeAdr8 = 'adr8'
export const DerivationPathTypeLegacy = 'legacy'

interface Response {
return_code: number
error_message: string
Expand Down Expand Up @@ -34,7 +37,18 @@ const successOrThrow = (response: Response, message: string) => {
}

export class Ledger {
public static async enumerateAccounts(transport: Transport, count = 5) {
public static mustGetPath(pathType: string, i: number) {
switch (pathType) {
case DerivationPathTypeAdr8:
return [44, 474, i]
case DerivationPathTypeLegacy:
return [44, 474, 0, 0, i]
}

throw new TypeError('invalid pathType: ' + pathType)
}

public static async enumerateAccounts(transport: Transport, pathType: string, count = 5) {
const accounts: LedgerAccount[] = []

try {
Expand All @@ -44,7 +58,7 @@ export class Ledger {
throw new WalletError(WalletErrors.LedgerOasisAppIsNotOpen, 'Oasis App is not open')
}
for (let i = 0; i < count; i++) {
const path = [44, 474, 0, 0, i]
const path = Ledger.mustGetPath(pathType, i)
const publicKeyResponse = successOrThrow(await app.publicKey(path), 'ledger public key')
accounts.push({ path, publicKey: new Uint8Array(publicKeyResponse.pk as Buffer) })
}
Expand Down
Loading

0 comments on commit 9f3bb9c

Please sign in to comment.