Skip to content

Commit

Permalink
[DDW-722] Change usb detection strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
renanvalentin committed Apr 12, 2022
1 parent b591026 commit 804ef8b
Show file tree
Hide file tree
Showing 16 changed files with 858 additions and 127 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
!source/
!features/
!storybook/
!hardware-wallet-tests/

# Now we ignore all files
*.*
Expand Down
78 changes: 78 additions & 0 deletions hardware-wallet-tests/cardano-app-already-connected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable jest/no-standalone-expect */
import expect from 'expect';

import {
createAndRegisterHardwareWalletChannels,
createHardwareWalletConnectionChannel,
initLedgerChannel,
createSequentialPromptMessages,
createCardanoAppChannel,
createGetPublicKeyChannel,
ipcRenderer,
} from './utils';

export const run = () => {
expect.assertions(3);

createSequentialPromptMessages([
'Plug Ledger Nano S to your computer',
'Start Cardano APP on Nano S',
'Run the test again with Cardano App opened',
'Export the public key',
]);

createAndRegisterHardwareWalletChannels();

const cardanoAppChannel = createCardanoAppChannel();
const publicKeyChannel = createGetPublicKeyChannel();
const hardwareWalletConnectionChannel = createHardwareWalletConnectionChannel();

return new Promise((resolve) => {
hardwareWalletConnectionChannel.onReceive(
async (params: { path: string }) => {
expect(params).toEqual({
disconnected: expect.any(Boolean),
deviceType: expect.any(String),
deviceId: null,
deviceModel: expect.any(String),
deviceName: expect.any(String),
path: expect.any(String),
});

const cardanoAppChannelReply = await cardanoAppChannel.request(
{ path: params.path },
ipcRenderer,
ipcRenderer
);

expect(cardanoAppChannelReply).toEqual({
minor: expect.any(Number),
major: expect.any(Number),
patch: expect.any(Number),
deviceId: expect.any(String),
});

const extendedPublicKey = await publicKeyChannel.request(
{
path: "1852'/1815'/0'",
// Shelley 1852 ADA 1815 indicator for account '0'
isTrezor: false,
devicePath: params.path,
},
ipcRenderer,
ipcRenderer
);

expect(extendedPublicKey).toEqual({
chainCodeHex: expect.any(String),
publicKeyHex: expect.any(String),
deviceId: expect.any(String),
});

resolve(null);
}
);

initLedgerChannel();
});
};
78 changes: 78 additions & 0 deletions hardware-wallet-tests/cardano-app-not-started.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable jest/no-standalone-expect */
import expect from 'expect';

import {
createAndRegisterHardwareWalletChannels,
createHardwareWalletConnectionChannel,
initLedgerChannel,
createSequentialPromptMessages,
createGetPublicKeyChannel,
ipcRenderer,
pollCardarnoApp,
} from './utils';

export const run = () => {
expect.assertions(3);

createSequentialPromptMessages([
'Start test runner',
'Plug Ledger Nano S to your computer',
'Start Cardano APP on Nano S',
'Nano S will prompt to export the public key',
'Export the public key',
]);

createAndRegisterHardwareWalletChannels();

const publicKeyChannel = createGetPublicKeyChannel();
const hardwareWalletConnectionChannel = createHardwareWalletConnectionChannel();

return new Promise((resolve) => {
hardwareWalletConnectionChannel.onReceive(
async (params: { path: string }) => {
expect(params).toEqual({
disconnected: expect.any(Boolean),
deviceType: expect.any(String),
deviceId: null,
deviceModel: expect.any(String),
deviceName: expect.any(String),
path: expect.any(String),
});

try {
const cardanoAppChannelReply = await pollCardarnoApp(params.path);

expect(cardanoAppChannelReply).toEqual({
minor: expect.any(Number),
major: expect.any(Number),
patch: expect.any(Number),
deviceId: expect.any(String),
});

const extendedPublicKey = await publicKeyChannel.request(
{
path: "1852'/1815'/0'",
// Shelley 1852 ADA 1815 indicator for account '0'
isTrezor: false,
devicePath: params.path,
},
ipcRenderer,
ipcRenderer
);

expect(extendedPublicKey).toEqual({
chainCodeHex: expect.any(String),
publicKeyHex: expect.any(String),
deviceId: expect.any(String),
});

resolve(null);
} catch (err) {
return null;
}
}
);

initLedgerChannel();
});
};
69 changes: 69 additions & 0 deletions hardware-wallet-tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import prompts from 'prompts';

import { run as runRemoveMultipleHardwareWallets } from './remove-multiple-hardware-wallets';
import { run as runCardanoAppAlreadyConnected } from './cardano-app-already-connected';
import { run as runCardanoAppNotStarted } from './cardano-app-not-started';
import { run as runRemoveSingleHardwareWallet } from './remove-single-hardware-wallet';
import { run as runMultipleHardwareWallets } from './multiple-hardware-wallets';

const CARDANO_APP_ALREADY_LAUNCHED = 'CARDANO_APP_ALREADY_LAUNCHED';
const CARDANO_APP_NOT_STARTED = 'CARDANO_APP_NOT_STARTED';
const SINGLE_LEDGER_REMOVED = 'SINGLE_LEDGER_REMOVED';
const MULTIPLE_HARDWARE_WALLETS = 'MULTIPLE_HARDWARE_WALLETS';
const MULTIPLE_HARDWARE_WALLETS_REMOVED = 'MULTIPLE_HARDWARE_WALLETS_REMOVED';

(async () => {
const { testType } = await prompts({
type: 'select',
name: 'testType',
message: 'Ledger Hardware Wallets Channel',
choices: [
{
title: 'export public key when Cardano APP is already connected',
value: CARDANO_APP_ALREADY_LAUNCHED,
},
{
title:
'export public key when Cardano APP is launched after test runner already has started',
value: CARDANO_APP_NOT_STARTED,
},
{ title: 'detect when ledger is removed', value: SINGLE_LEDGER_REMOVED },
{
title: 'connect both Nano S and Nano X',
value: MULTIPLE_HARDWARE_WALLETS,
},
{
title: 'detect when multiple hardware wallets are removed',
value: MULTIPLE_HARDWARE_WALLETS_REMOVED,
},
{ title: 'exit', value: 'exit' },
],
});

switch (testType) {
case CARDANO_APP_ALREADY_LAUNCHED:
await runCardanoAppAlreadyConnected();
break;

case CARDANO_APP_NOT_STARTED:
await runCardanoAppNotStarted();
break;

case SINGLE_LEDGER_REMOVED:
await runRemoveSingleHardwareWallet();
break;

case MULTIPLE_HARDWARE_WALLETS:
await runMultipleHardwareWallets();
break;

case MULTIPLE_HARDWARE_WALLETS_REMOVED:
await runRemoveMultipleHardwareWallets();
break;

default:
break;
}

process.exit(0);
})();
52 changes: 52 additions & 0 deletions hardware-wallet-tests/multiple-hardware-wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable jest/no-standalone-expect */
import expect from 'expect';

import {
createAndRegisterHardwareWalletChannels,
createHardwareWalletConnectionChannel,
createSequentialResult,
initLedgerChannel,
createSequentialPromptMessages,
} from './utils';

export const run = () => {
expect.assertions(2);

createAndRegisterHardwareWalletChannels();

const hardwareWalletConnectionChannel = createHardwareWalletConnectionChannel();

const promptMessages = createSequentialPromptMessages([
'Start test runner',
'Plug Ledger Nano S to your computer',
'Plug Ledger Nano X to your computer',
]);

promptMessages();

const expectedSequence = createSequentialResult([
{
disconnected: false,
deviceModel: 'nanoS',
},
{
disconnected: false,
deviceModel: 'nanoX',
},
]);

return new Promise((resolve) => {
hardwareWalletConnectionChannel.onReceive(
async (params: { path: string; deviceModel: string }) => {
const [expectedValue, isOver] = expectedSequence();
expect(params).toEqual(expectedValue);

if (isOver) {
resolve(null);
}
}
);

initLedgerChannel();
});
};
64 changes: 64 additions & 0 deletions hardware-wallet-tests/remove-multiple-hardware-wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable jest/no-standalone-expect */
import expect from 'expect';

import {
createAndRegisterHardwareWalletChannels,
createHardwareWalletConnectionChannel,
createSequentialResult,
initLedgerChannel,
createSequentialPromptMessages,
} from './utils';

const expectedSequence = createSequentialResult([
{
disconnected: false,
deviceModel: 'nanoS',
},
{
disconnected: false,
deviceModel: 'nanoX',
},
{
disconnected: true,
deviceModel: 'nanoS',
},
{
disconnected: true,
deviceModel: 'nanoX',
},
]);

export const run = () => {
expect.assertions(4);

createAndRegisterHardwareWalletChannels();

const hardwareWalletConnectionChannel = createHardwareWalletConnectionChannel();

const promptMessages = createSequentialPromptMessages([
'Connect Nano S',
'Connect Nano X',
'Disconnect Nano S',
'Disconnect Nano X',
]);

promptMessages();

return new Promise((resolve) => {
hardwareWalletConnectionChannel.onReceive(
async (params: { path: string; deviceModel: string }) => {
const [expectedValue, isOver] = expectedSequence();

expect(params).toEqual(expectedValue);

if (isOver) {
return resolve(null);
}

promptMessages();
}
);

initLedgerChannel();
});
};
Loading

0 comments on commit 804ef8b

Please sign in to comment.