Skip to content

Commit

Permalink
feat: suggest app
Browse files Browse the repository at this point in the history
  • Loading branch information
vivalaakam committed Jan 17, 2023
1 parent 8e6158b commit e6b1462
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haqq/provider-ledger-react-native",
"version": "0.0.6",
"version": "0.0.7",
"description": "Provider for react-native ledger",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
30 changes: 30 additions & 0 deletions src/commands/get-app-and-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';

export const getAppAndVersion = async (
transport: TransportBLE,
): Promise<{
name: string;
version: string;
flags: number | Buffer;
}> => {
const r = await transport.send(0xb0, 0x01, 0x00, 0x00);

let i = 0;
const format = r[i++];

if (format !== 1) {
throw new Error('getAppAndVersion: format not supported');
}

const nameLength = r[i++];
const name = r.slice(i, (i += nameLength)).toString('ascii');
const versionLength = r[i++];
const version = r.slice(i, (i += versionLength)).toString('ascii');
const flagLength = r[i++];
const flags = r.slice(i, (i += flagLength));
return {
name,
version,
flags,
};
};
8 changes: 8 additions & 0 deletions src/commands/open-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';

export const openApp = async (
transport: TransportBLE,
name: string,
): Promise<void> => {
await transport.send(0xe0, 0xd8, 0x00, 0x00, Buffer.from(name, 'ascii'));
};
5 changes: 5 additions & 0 deletions src/commands/quit-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';

export const quitApp = async (transport: TransportBLE) => {
await transport.send(0xb0, 0xa7, 0x00, 0x00);
}
26 changes: 26 additions & 0 deletions src/commands/suggest-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';
import {getAppAndVersion} from './get-app-and-version';
import {openApp} from './open-app';
import {quitApp} from './quit-app';

const dashboardNames = ['BOLOS', 'OLOS\u0000'];
export const isDashboardName = (name: string) => dashboardNames.includes(name);

export const suggestApp = async (
transport: TransportBLE,
name: string,
): Promise<void> => {
try {
const v = await getAppAndVersion(transport);

if (v.name !== name) {
if (!isDashboardName(v.name)) {
await quitApp(transport);
}

await openApp(transport, name);
}
} catch (e) {
console.log('suggestApp', e);
}
};
19 changes: 19 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import AppEth, {ledgerService} from '@ledgerhq/hw-app-eth';
import TransportBLE from '@ledgerhq/react-native-hw-transport-ble';
import {utils, UnsignedTransaction} from 'ethers';
import {suggestApp} from './commands/suggest-app';
import {getDeviceConnection} from './get-device-connection';
import {sleep} from './sleep';
import {ProviderLedgerReactNativeOptions,} from './types';
Expand All @@ -24,6 +25,11 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
if (!transport) {
throw new Error('can_not_connected');
}

if (this._options.appName) {
await suggestApp(transport, this._options.appName);
}

const eth = new AppEth(transport);

const response = await eth.getAddress(hdPath);
Expand Down Expand Up @@ -79,6 +85,10 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
throw new Error('can_not_connected');
}

if (this._options.appName) {
await suggestApp(transport, this._options.appName);
}

const eth = new AppEth(transport);

const signature = await eth.signTransaction(this._options.hdPath, unsignedTx, resolution);
Expand Down Expand Up @@ -111,6 +121,10 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
throw new Error('can_not_connected')
}

if (this._options.appName) {
await suggestApp(transport, this._options.appName);
}

const eth = new AppEth(transport);

const signature = await eth.signEIP712HashedMessage(this._options.hdPath, domainHash, valuesHash);
Expand Down Expand Up @@ -143,6 +157,11 @@ export class ProviderLedgerReactNative extends Provider<ProviderLedgerReactNativ
if (!transport) {
throw new Error('can_not_connected');
}

if (this._options.appName) {
await suggestApp(transport, this._options.appName);
}

const eth = new AppEth(transport);

const response = await eth.getAddress(hdPath, true);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export enum State {
export type ProviderLedgerReactNativeOptions = {
deviceId: string;
hdPath: string;
appName?: string;
}

0 comments on commit e6b1462

Please sign in to comment.