Skip to content
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

feature: Add ADR 0008 option for opening Ledger wallet #761

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"dependencies": {
"@ledgerhq/hw-transport-webusb": "6.20.0",
"@oasisprotocol/client": "0.1.0-alpha8",
"@oasisprotocol/ledger": "0.2.1",
"@oasisprotocol/ledger": "0.2.2",
"@reduxjs/toolkit": "1.7.1",
"babel-jest": "27.5.1",
"babel-plugin-istanbul": "6.1.1",
Expand Down
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"must" is a bit odd. I'd just name it getPath

switch (pathType) {
case DerivationPathTypeAdr8:
return [44, 474, i]
case DerivationPathTypeLegacy:
return [44, 474, 0, 0, i]
}
Comment on lines +40 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum would be a better type

export enum DerivationPathType {
  Adr8 = 'adr8',
  Legacy = 'legacy',
}

  public static mustGetPath(pathType: DerivationPathType, i: number) {
    switch (pathType) {
      case DerivationPathType.Adr8:
        return [44, 474, i]
      case DerivationPathType.Legacy:
        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