Skip to content

Commit

Permalink
Responds to feedback from PR #413
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jun 28, 2022
1 parent 3ba8c3b commit 388ef63
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 246 deletions.
32 changes: 17 additions & 15 deletions src/__test__/e2e/btc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { testRequest } from '../utils/testRequest';

const prng = getPrng();
const TEST_TESTNET = !!getTestnet() || false;
const client = initializeClient();
let wallet: Wallet | null = null;
type InputObj = { hash: string, value: number, signerIdx: number, idx: number };

Expand All @@ -41,7 +40,7 @@ for (let i = 0; i < count; i++) {
inputs.push({ hash: hash.toString('hex'), value, signerIdx, idx });
}

async function testSign ({ txReq, signingKeys, sigHashes }: any) {
async function testSign ({ txReq, signingKeys, sigHashes, client }: any) {
const tx = await client.sign(txReq);
const len = tx?.sigs?.length ?? 0
expect(len).toEqual(signingKeys.length);
Expand All @@ -63,29 +62,32 @@ async function runTestSet (
opts: any,
wallet: Wallet | null,
inputsSlice: InputObj[],
client,
) {
expect(wallet).not.toEqualElseLog(null, 'Wallet not available');
if (TEST_TESTNET) {
// Testnet + change
opts.isTestnet = true;
opts.useChange = true;
await testSign(setup_btc_sig_test(opts, wallet, inputsSlice, prng));
await testSign({ ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), client });
// Testnet + no change
opts.isTestnet = true;
opts.useChange = false;
await testSign(setup_btc_sig_test(opts, wallet, inputsSlice, prng));
await testSign({ ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), client });
}
// Mainnet + change
opts.isTestnet = false;
opts.useChange = true;
await testSign(setup_btc_sig_test(opts, wallet, inputsSlice, prng));
await testSign({ ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), client });
// Mainnet + no change
opts.isTestnet = false;
opts.useChange = false;
await testSign(setup_btc_sig_test(opts, wallet, inputsSlice, prng));
await testSign({ ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), client });
}

describe('Bitcoin', () => {
const client = initializeClient();

describe('wallet seeds', () => {
it('Should get GP_SUCCESS for a known, connected wallet', async () => {
const activeWalletUID = client.getActiveWallet()?.uid;
Expand Down Expand Up @@ -117,23 +119,23 @@ describe('Bitcoin', () => {
spenderPurpose: BTC_PURPOSE_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2PKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2wpkh->p2sh-p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2SH_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2wpkh->p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});
});

Expand All @@ -143,23 +145,23 @@ describe('Bitcoin', () => {
spenderPurpose: BTC_PURPOSE_P2SH_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2PKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2sh-p2wpkh->p2sh-p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2SH_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2SH_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2sh-p2wpkh->p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2SH_P2WPKH,
recipientPurpose: BTC_PURPOSE_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});
});

Expand All @@ -169,23 +171,23 @@ describe('Bitcoin', () => {
spenderPurpose: BTC_PURPOSE_P2PKH,
recipientPurpose: BTC_PURPOSE_P2PKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2pkh->p2sh-p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2PKH,
recipientPurpose: BTC_PURPOSE_P2SH_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});

it('p2pkh->p2wpkh', async () => {
const opts = {
spenderPurpose: BTC_PURPOSE_P2PKH,
recipientPurpose: BTC_PURPOSE_P2WPKH,
};
await runTestSet(opts, wallet, inputsSlice);
await runTestSet(opts, wallet, inputsSlice, client);
});
});
})
Expand Down
3 changes: 2 additions & 1 deletion src/__test__/e2e/eth.msg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import { getN } from '../utils/getters';
import { initializeClient } from '../utils/initializeClient';
import { runEthMsg } from '../utils/runners';

const client = initializeClient();
const numRandom = getN() ? getN() : 20; // Number of random tests to conduct

describe('ETH Messages', () => {
const client = initializeClient();

describe('Test ETH personalSign', function () {
it('Should throw error when message contains non-ASCII characters', async () => {
const protocol = 'signPersonal';
Expand Down
23 changes: 16 additions & 7 deletions src/__test__/e2e/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import {
import { initializeClient } from '../utils/initializeClient';

const id = getDeviceId();
const client = initializeClient();

describe('General', () => {
const client = initializeClient();

it('Should test SDK dehydration/rehydration', async () => {
const addrData = {
startPath: [BTC_PURPOSE_P2SH_P2WPKH, BTC_COIN, HARDENED_OFFSET, 0, 0],
Expand All @@ -49,12 +50,14 @@ describe('General', () => {

const client1 = setupTestClient();
await client1.connect(id);
expect(client1.isPaired).toBeTruthy()
const addrs1 = await client1.getAddresses(addrData);

const stateData = client1.getStateData();

const client2 = setupTestClient(null, stateData);
await client2.connect(id);
expect(client2.isPaired).toBeTruthy()
const addrs2 = await client2.getAddresses(addrData);

expect(addrs1).toEqual(addrs2);
Expand Down Expand Up @@ -145,26 +148,32 @@ describe('General', () => {
await client.sign(req);
});
it('should sign newer transactions', async () => {
// Test data range
const { txData, req, maxDataSz, common } = await buildEthSignRequest(
const { txData, req, common } = await buildEthSignRequest(
client,
{
type: 1,
gasPrice: 1200000000,
nonce: 0,
gasLimit: 50000,
to: '0xe242e54155b1abc71fc118065270cecaaf8b7768',
value: 1000000000000,
data: '0x17e914679b7e160613be4f8c2d3203d236286d74eb9192f6d6f71b9118a42bb033ccd8e8',
}
);
// NOTE: This will display a prehashed payload for bridged general signing
// requests because `ethMaxDataSz` represents the `data` field for legacy
// requests, but it represents the entire payload for general signing requests.
txData.data = randomBytes(maxDataSz);
const tx = EthTxFactory.fromTxData(txData, { common });
req.data.payload = tx.getMessageToSign(false);
await client.sign(req);
});

it('should sign bad transactions', async () => {
const { txData, req, maxDataSz, common } = await buildEthSignRequest(
client,
);
const { txData, req, maxDataSz, common } = await buildEthSignRequest(client);
await question(
'Please REJECT the next request if the warning screen displays. Press enter to continue.',
);
txData.data = randomBytes(maxDataSz)
req.data.data = randomBytes(maxDataSz + 1);
const tx = EthTxFactory.fromTxData(txData, { common });
req.data.payload = tx.getMessageToSign(false);
Expand Down
3 changes: 2 additions & 1 deletion src/__test__/e2e/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { BTC_PURPOSE_P2PKH, ETH_COIN } from '../utils/helpers';
import { initializeClient } from '../utils/initializeClient';

const client = initializeClient();

// Random address to test the screen with.
// IMPORTANT NOTE: For Ethereum addresses you should always add the lower case variety since
Expand All @@ -40,6 +39,8 @@ const ETH_REQ = {
};

describe('key-value', () => {
const client = initializeClient();

it('Should ask if the user wants to reset state', async () => {
const answer = question(
'Do you want to clear all kv records and start anew? (Y/N) ',
Expand Down
3 changes: 2 additions & 1 deletion src/__test__/e2e/non-exportable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import { DEFAULT_SIGNER } from '../utils/builders';
import { getSigStr } from '../utils/helpers';
import { initializeClient } from '../utils/initializeClient';

const client = initializeClient();
describe('Non-Exportable Seed', () => {
const client = initializeClient();

describe('Setup', () => {
it('Should ask if the user wants to test a card with a non-exportable seed', async () => {
// NOTE: non-exportable seeds were deprecated from the normal setup pathway in firmware v0.12.0
Expand Down
4 changes: 2 additions & 2 deletions src/__test__/e2e/signing/signing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { runEvmTests } from './evm.test';
import { runSolanaTests } from './solana.test';
import { runUnformattedTests } from './unformatted.test';

const client = initializeClient();

describe('Test General Signing', () => {
const client = initializeClient();

it('Should verify firmware version.', async () => {
const fwConstants = client.getFwConstants();
if (!fwConstants.genericSigning) {
Expand Down
Loading

0 comments on commit 388ef63

Please sign in to comment.