diff --git a/.env.template b/.env.template index d45166ec..9181581d 100644 --- a/.env.template +++ b/.env.template @@ -1,6 +1,7 @@ -REUSE_KEY=0 +APP_NAME="SDK Test" DEVICE_ID="" +PASSWORD="" + ENC_PW= ETHERSCAN_KEY="" -name="SDK Test" baseUrl="https://signing.gridpl.us" diff --git a/src/__test__/e2e/api.test.ts b/src/__test__/e2e/api.test.ts index e198fe29..f349cc9b 100644 --- a/src/__test__/e2e/api.test.ts +++ b/src/__test__/e2e/api.test.ts @@ -206,7 +206,6 @@ describe('API', () => { "44'/60'/0'/0/0", ); expect(addresses).toHaveLength(1); - console.log(addresses[0]); expect(addresses[0]).toBeTruthy(); }); @@ -217,7 +216,6 @@ describe('API', () => { n: 5, }, ); - console.log(addresses[0]); expect(addresses).toHaveLength(5); addresses.forEach((address) => expect(address).toBeTruthy()); }); @@ -230,7 +228,6 @@ describe('API', () => { startPathIndex: 10, }, ); - console.log(addresses[0]); expect(addresses).toHaveLength(3); addresses.forEach((address) => expect(address).toBeTruthy()); }); diff --git a/src/__test__/e2e/btc.test.ts b/src/__test__/e2e/btc.test.ts index 75e202bc..50f9f6f8 100644 --- a/src/__test__/e2e/btc.test.ts +++ b/src/__test__/e2e/btc.test.ts @@ -19,8 +19,9 @@ import { setup_btc_sig_test, stripDER, } from '../utils/helpers'; -import { initializeClient } from '../utils/initializeClient'; import { testRequest } from '../utils/testRequest'; +import { setupClient } from '../utils/setup'; +import { Wallet } from 'ethers'; const prng = getPrng(); const TEST_TESTNET = !!getTestnet() || false; @@ -104,7 +105,11 @@ async function runTestSet( } describe('Bitcoin', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); describe('wallet seeds', () => { it('Should get GP_SUCCESS for a known, connected wallet', async () => { diff --git a/src/__test__/e2e/eth.msg.test.ts b/src/__test__/e2e/eth.msg.test.ts index 788dfece..d3df54be 100644 --- a/src/__test__/e2e/eth.msg.test.ts +++ b/src/__test__/e2e/eth.msg.test.ts @@ -19,11 +19,15 @@ import { HARDENED_OFFSET } from '../../constants'; import { randomBytes } from '../../util'; import { buildEthMsgReq, buildRandomMsg } from '../utils/builders'; -import { initializeClient } from '../utils/initializeClient'; import { runEthMsg } from '../utils/runners'; +import { setupClient } from '../utils/setup'; describe('ETH Messages', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); describe('Test ETH personalSign', function () { it('Should throw error when message contains non-ASCII characters', async () => { diff --git a/src/__test__/e2e/general.test.ts b/src/__test__/e2e/general.test.ts index 9b122128..7c7646da 100644 --- a/src/__test__/e2e/general.test.ts +++ b/src/__test__/e2e/general.test.ts @@ -18,7 +18,7 @@ */ import { TransactionFactory as EthTxFactory } from '@ethereumjs/tx'; import { question } from 'readline-sync'; -import { HARDENED_OFFSET, LatticeResponseCode } from '../../constants'; +import { HARDENED_OFFSET } from '../../constants'; import { LatticeResponseCode, ProtocolConstants } from '../../protocol'; import { randomBytes } from '../../util'; import { buildEthSignRequest } from '../utils/builders'; @@ -32,12 +32,17 @@ import { ETH_COIN, setupTestClient, } from '../utils/helpers'; -import { initializeClient } from '../utils/initializeClient'; + +import { setupClient } from '../utils/setup'; const id = getDeviceId(); describe('General', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); it('Should test SDK dehydration/rehydration', async () => { const addrData = { diff --git a/src/__test__/e2e/kv.test.ts b/src/__test__/e2e/kv.test.ts index 7caa40ea..5cb2ce3b 100644 --- a/src/__test__/e2e/kv.test.ts +++ b/src/__test__/e2e/kv.test.ts @@ -8,7 +8,8 @@ import { question } from 'readline-sync'; import { HARDENED_OFFSET } from '../../constants'; import { LatticeResponseCode, ProtocolConstants } from '../../protocol'; import { BTC_PURPOSE_P2PKH, ETH_COIN } from '../utils/helpers'; -import { initializeClient } from '../utils/initializeClient'; + +import { setupClient } from '../utils/setup'; // Random address to test the screen with. // IMPORTANT NOTE: For Ethereum addresses you should always add the lower case variety since @@ -34,7 +35,11 @@ const ETH_REQ = { }; describe('key-value', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); it('Should ask if the user wants to reset state', async () => { const answer = question( diff --git a/src/__test__/e2e/non-exportable.test.ts b/src/__test__/e2e/non-exportable.test.ts index 130aea3a..761b4b76 100644 --- a/src/__test__/e2e/non-exportable.test.ts +++ b/src/__test__/e2e/non-exportable.test.ts @@ -26,12 +26,17 @@ import { ecdsaRecover } from 'secp256k1'; import { Constants } from '../..'; import { DEFAULT_SIGNER } from '../utils/builders'; import { getSigStr } from '../utils/helpers'; -import { initializeClient } from '../utils/initializeClient'; + +import { setupClient } from '../utils/setup'; let runTests = true; describe('Non-Exportable Seed', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); describe('Setup', () => { it('Should ask if the user wants to test a card with a non-exportable seed', async () => { diff --git a/src/__test__/e2e/signing/bls.test.ts b/src/__test__/e2e/signing/bls.test.ts index 98a5725c..9046a953 100644 --- a/src/__test__/e2e/signing/bls.test.ts +++ b/src/__test__/e2e/signing/bls.test.ts @@ -19,8 +19,9 @@ import { mnemonicToSeedSync } from 'bip39'; import { deriveSeedTree } from 'bls12-381-keygen'; import { question } from 'readline-sync'; +import { Constants } from '../../../index'; +import { getPathStr } from '../../../shared/utilities'; import { getEncPw } from '../../utils/getters'; -import { initializeClient, initializeSeed } from '../../utils/initializeClient'; import { buildPath, copyBuffer, @@ -31,9 +32,9 @@ import { parseWalletJobResp, serializeJobData, } from '../../utils/helpers'; +import { initializeSeed } from '../../utils/initializeClient'; +import { setupClient } from '../../utils/setup'; import { testRequest } from '../../utils/testRequest'; -import { Constants } from '../../../index'; -import { getPathStr } from '../../../shared/utilities'; const globalVectors = getTestVectors(); @@ -46,7 +47,12 @@ const KNOWN_MNEMONIC = globalVectors.ethDeposit.mnemonic; const KNOWN_SEED = mnemonicToSeedSync(KNOWN_MNEMONIC); describe('[BLS keys]', () => { - client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); + it('Should get the device encryption password', async () => { encPw = getEncPw(); if (!encPw) { diff --git a/src/__test__/e2e/signing/determinism.test.ts b/src/__test__/e2e/signing/determinism.test.ts index f4ecd92f..5194ec5a 100644 --- a/src/__test__/e2e/signing/determinism.test.ts +++ b/src/__test__/e2e/signing/determinism.test.ts @@ -24,12 +24,17 @@ import { gpErrors, jobTypes, } from '../../utils/helpers'; -import { initializeClient, initializeSeed } from '../../utils/initializeClient'; +import { initializeSeed } from '../../utils/initializeClient'; import { runTestCase } from '../../utils/runners'; +import { setupClient } from '../../utils/setup'; let seed: Buffer; describe('[Determinism]', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); describe('Setup and validate seed', () => { it('Should re-connect to the Lattice and update the walletUID.', async () => { @@ -152,42 +157,42 @@ describe('[Determinism]', () => { }); describe('Test uniformity of Ethereum transaction sigs', () => { - it('Should validate uniformity sigs on m/44\'/60\'/0\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/0'/0/0", async () => { const tx = buildTx(); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET; await testUniformSigs(txReq, tx, client); }); - it('Should validate uniformity sigs on m/44\'/60\'/1\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/1'/0/0", async () => { const tx = buildTx(); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET + 1; await testUniformSigs(txReq, tx, client); }); - it('Should validate uniformity sigs on m/44\'/60\'/8\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/8'/0/0", async () => { const tx = buildTx(); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET + 8; await testUniformSigs(txReq, tx, client); }); - it('Should validate uniformity sigs on m/44\'/60\'/0\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/0'/0/0", async () => { const tx = buildTx(`0x${randomBytes(4000).toString('hex')}`); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET; await testUniformSigs(txReq, tx, client); }); - it('Should validate uniformity sigs on m/44\'/60\'/1\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/1'/0/0", async () => { const tx = buildTx(`0x${randomBytes(4000).toString('hex')}`); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET + 1; await testUniformSigs(txReq, tx, client); }); - it('Should validate uniformity sigs on m/44\'/60\'/8\'/0/0', async () => { + it("Should validate uniformity sigs on m/44'/60'/8'/0/0", async () => { const tx = buildTx(`0x${randomBytes(4000).toString('hex')}`); const txReq = buildTxReq(tx); txReq.data.signerPath[2] = HARDENED_OFFSET + 8; diff --git a/src/__test__/e2e/signing/evm-abi.test.ts b/src/__test__/e2e/signing/evm-abi.test.ts index db919a9b..582a8471 100644 --- a/src/__test__/e2e/signing/evm-abi.test.ts +++ b/src/__test__/e2e/signing/evm-abi.test.ts @@ -16,7 +16,8 @@ import { } from '../../utils/builders'; import { getEtherscanKey } from '../../utils/getters'; import { runEvm } from '../../utils/runners'; -import { initializeClient, initializeSeed } from '../../utils/initializeClient'; +import { initializeSeed } from '../../utils/initializeClient'; +import { setupClient } from '../../utils/setup'; const globalVectors = getTestVectors(); const vectors = globalVectors.evm.calldata; @@ -31,7 +32,11 @@ const { encDefs, encDefsCalldata } = buildEncDefs(vectors); // TESTS //--------------------------------------- describe('[EVM ABI]', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); const runEvmTestForReq = ( req?: any, bypassSetPayload?: boolean, diff --git a/src/__test__/e2e/signing/evm-tx.test.ts b/src/__test__/e2e/signing/evm-tx.test.ts index caf885e6..c57eb0a4 100644 --- a/src/__test__/e2e/signing/evm-tx.test.ts +++ b/src/__test__/e2e/signing/evm-tx.test.ts @@ -10,7 +10,8 @@ import { encode as rlpEncode } from 'rlp'; import { randomBytes } from '../../../util'; import { DEFAULT_SIGNER, buildEvmReq, getNumIter } from '../../utils/builders'; import { runEvm } from '../../utils/runners'; -import { initializeClient, initializeSeed } from '../../utils/initializeClient'; +import { initializeSeed } from '../../utils/initializeClient'; +import { setupClient } from '../../utils/setup'; //--------------------------------------- // STATE DATA //--------------------------------------- @@ -20,7 +21,12 @@ let CURRENT_SEED = null; // TESTS //--------------------------------------- describe('[EVM TX]', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); + const runEvmTestForReq = ( req?: any, bypassSetPayload?: boolean, diff --git a/src/__test__/e2e/signing/solana/solana.programs.test.ts b/src/__test__/e2e/signing/solana/solana.programs.test.ts index 9bb6cfe4..3f2f4f09 100644 --- a/src/__test__/e2e/signing/solana/solana.programs.test.ts +++ b/src/__test__/e2e/signing/solana/solana.programs.test.ts @@ -1,9 +1,13 @@ import { Constants } from '../../../..'; -import { initializeClient } from '../../../utils/initializeClient'; +import { setupClient } from '../../../utils/setup'; import { dexlabProgram, raydiumProgram } from './__mocks__/programs'; describe('Solana Programs', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); it('should sign Dexlab program', async () => { const payload = dexlabProgram; diff --git a/src/__test__/e2e/signing/solana/solana.test.ts b/src/__test__/e2e/signing/solana/solana.test.ts index f7782979..e2934f47 100644 --- a/src/__test__/e2e/signing/solana/solana.test.ts +++ b/src/__test__/e2e/signing/solana/solana.test.ts @@ -8,11 +8,9 @@ import { Constants } from '../../../..'; import { HARDENED_OFFSET } from '../../../../constants'; import { getPrng } from '../../../utils/getters'; import { deriveED25519Key, prandomBuf } from '../../../utils/helpers'; -import { - initializeClient, - initializeSeed, -} from '../../../utils/initializeClient'; +import { initializeSeed } from '../../../utils/initializeClient'; import { runGeneric } from '../../../utils/runners'; +import { setupClient } from '../../../utils/setup'; //--------------------------------------- // STATE DATA @@ -26,7 +24,12 @@ const DEFAULT_SOLANA_SIGNER = [ const prng = getPrng(); describe('[Solana]', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); + const getReq = (overrides: any) => ({ data: { curveType: Constants.SIGNING.CURVES.ED25519, diff --git a/src/__test__/e2e/signing/unformatted.test.ts b/src/__test__/e2e/signing/unformatted.test.ts index 23f0448a..6abce9c0 100644 --- a/src/__test__/e2e/signing/unformatted.test.ts +++ b/src/__test__/e2e/signing/unformatted.test.ts @@ -4,7 +4,7 @@ import { ethPersonalSignMsg, prandomBuf } from '../../utils/helpers'; import { runGeneric } from '../../utils/runners'; import { HARDENED_OFFSET } from '../../../constants'; import { getPrng } from '../../utils/getters'; -import { initializeClient } from '../../utils/initializeClient'; +import { setupClient } from '../../utils/setup'; const prng = getPrng(); const numIter = getNumIter(); @@ -17,7 +17,12 @@ const DEFAULT_SIGNER = [ ]; describe('[Unformatted]', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); + const getReq = (overrides: any) => ({ data: { signerPath: DEFAULT_SIGNER, diff --git a/src/__test__/e2e/wallet-jobs.test.ts b/src/__test__/e2e/wallet-jobs.test.ts index 773a46e0..e207a5de 100644 --- a/src/__test__/e2e/wallet-jobs.test.ts +++ b/src/__test__/e2e/wallet-jobs.test.ts @@ -15,7 +15,7 @@ import { mnemonicToSeedSync } from 'bip39'; import { privateToAddress, privateToPublic } from 'ethereumjs-util'; import { question } from 'readline-sync'; import { ecdsaRecover } from 'secp256k1'; -import { Constants } from '../..'; +import { Constants, getClient, pair } from '../..'; import { HARDENED_OFFSET } from '../../constants'; import { parseDER, randomBytes } from '../../util'; import { DEFAULT_SIGNER } from '../utils/builders'; @@ -40,8 +40,8 @@ import { validateDerivedPublicKeys, validateETHAddresses, } from '../utils/helpers'; -import { initializeClient } from '../utils/initializeClient'; import { testRequest } from '../utils/testRequest'; +import { setupClient } from '../utils/setup'; const id = getDeviceId(); //--------------------------------------- @@ -69,7 +69,11 @@ const KNOWN_SEED = mnemonicToSeedSync(KNOWN_MNEMONIC); const wallet = bip32.fromSeed(KNOWN_SEED); describe('Test Wallet Jobs', () => { - const client = initializeClient(); + let client; + + test('pair', async () => { + client = await setupClient(); + }); it('Should make sure client has active wallets', async () => { expect(client.isPaired).toEqual(true); @@ -687,7 +691,7 @@ describe('Test Wallet Jobs', () => { ); }); let basePath = DEFAULT_SIGNER; - let parentPathStr = 'm/44\'/60\'/0\'/0'; + let parentPathStr = "m/44'/60'/0'/0"; it('Should remove the current seed', async () => { jobType = jobTypes.WALLET_JOB_DELETE_SEED; @@ -755,27 +759,27 @@ describe('Test Wallet Jobs', () => { }); // One leading privKey zero -> P(1/256) - it('Should test address m/44\'/60\'/0\'/0/396 (1 leading zero byte)', async () => { + it("Should test address m/44'/60'/0'/0/396 (1 leading zero byte)", async () => { await runZerosTest(basePath, parentPathStr, 396, 1); }); - it('Should test address m/44\'/60\'/0\'/0/406 (1 leading zero byte)', async () => { + it("Should test address m/44'/60'/0'/0/406 (1 leading zero byte)", async () => { await runZerosTest(basePath, parentPathStr, 406, 1); }); - it('Should test address m/44\'/60\'/0\'/0/668 (1 leading zero byte)', async () => { + it("Should test address m/44'/60'/0'/0/668 (1 leading zero byte)", async () => { await runZerosTest(basePath, parentPathStr, 668, 1); }); // Two leading privKey zeros -> P(1/65536) - it('Should test address m/44\'/60\'/0\'/0/71068 (2 leading zero bytes)', async () => { + it("Should test address m/44'/60'/0'/0/71068 (2 leading zero bytes)", async () => { await runZerosTest(basePath, parentPathStr, 71068, 2); }); - it('Should test address m/44\'/60\'/0\'/0/82173 (2 leading zero bytes)', async () => { + it("Should test address m/44'/60'/0'/0/82173 (2 leading zero bytes)", async () => { await runZerosTest(basePath, parentPathStr, 82173, 2); }); // Three leading privKey zeros -> P(1/16777216) // Unlikely any user ever runs into these but I wanted to derive the addrs for funsies - it('Should test address m/44\'/60\'/0\'/0/11981831 (3 leading zero bytes)', async () => { + it("Should test address m/44'/60'/0'/0/11981831 (3 leading zero bytes)", async () => { await runZerosTest(basePath, parentPathStr, 11981831, 3); }); @@ -786,7 +790,7 @@ describe('Test Wallet Jobs', () => { // We want this leading-zero pubkey to be a parent derivation path to then // test all further derivations it('Should switch to testing public keys', async () => { - parentPathStr = 'm/44\'/60\'/0\''; + parentPathStr = "m/44'/60'/0'"; basePath[3] = 153; basePath = basePath.slice(0, 4); }); @@ -794,34 +798,34 @@ describe('Test Wallet Jobs', () => { // There should be no problems with the parent path here because the result // is the leading-zero pubkey directly. Since we do not do a further derivation // with that leading-zero pubkey, there should never be any issues. - it('Should test address m/44\'/60\'/0\'/153', async () => { + it("Should test address m/44'/60'/0'/153", async () => { await runZerosTest(basePath, parentPathStr, 153, 1, true); }); it('Should prepare for one more derivation step', async () => { - parentPathStr = 'm/44\'/60\'/0\'/153'; + parentPathStr = "m/44'/60'/0'/153"; basePath.push(0); }); // Now we will derive one more step with the leading zero pubkey feeding // into the derivation. This tests an edge case in firmware. - it('Should test address m/44\'/60\'/0\'/153/0', async () => { + it("Should test address m/44'/60'/0'/153/0", async () => { await runZerosTest(basePath, parentPathStr, 0, 0); }); - it('Should test address m/44\'/60\'/0\'/153/1', async () => { + it("Should test address m/44'/60'/0'/153/1", async () => { await runZerosTest(basePath, parentPathStr, 1, 0); }); - it('Should test address m/44\'/60\'/0\'/153/5', async () => { + it("Should test address m/44'/60'/0'/153/5", async () => { await runZerosTest(basePath, parentPathStr, 5, 0); }); - it('Should test address m/44\'/60\'/0\'/153/10000', async () => { + it("Should test address m/44'/60'/0'/153/10000", async () => { await runZerosTest(basePath, parentPathStr, 10000, 0); }); - it('Should test address m/44\'/60\'/0\'/153/9876543', async () => { + it("Should test address m/44'/60'/0'/153/9876543", async () => { await runZerosTest(basePath, parentPathStr, 9876543, 0); }); }); diff --git a/src/__test__/utils/helpers.ts b/src/__test__/utils/helpers.ts index 0419abba..6f61f825 100644 --- a/src/__test__/utils/helpers.ts +++ b/src/__test__/utils/helpers.ts @@ -48,7 +48,7 @@ export function setupTestClient( return new Client({ stateData }); } const setup: any = { - name: env.name || 'SDK Test', + name: env.APP_NAME || 'SDK Test', baseUrl: env.baseUrl || 'https://signing.gridpl.us', timeout: 120000, }; diff --git a/src/__test__/utils/initializeClient.ts b/src/__test__/utils/initializeClient.ts index 55ec15e6..a2e05727 100644 --- a/src/__test__/utils/initializeClient.ts +++ b/src/__test__/utils/initializeClient.ts @@ -1,38 +1,12 @@ -import { question } from 'readline-sync'; import { Client } from '../../client'; import { buildTestRequestPayload } from './builders'; -import { getDeviceId } from './getters'; import { deserializeExportSeedJobResult, jobTypes, parseWalletJobResp, - setupTestClient, } from './helpers'; import { testRequest } from './testRequest'; -export const initializeClient = () => { - const id = getDeviceId(); - const client = setupTestClient(); - describe('Initializing client', () => { - it('Connecting to Lattice', async () => { - const _id = id - ? id - : question('Please enter the ID of your test device: '); - const isPaired = await client.connect(_id); - if (!isPaired) { - expect(client.isPaired).toEqual(false); - const secret = question('Please enter the pairing secret: '); - await client.pair(secret.toUpperCase()); - expect(!!client.getActiveWallet()).toEqual(true); - } - expect(client.isPaired).toEqual(true); - expect(!!client.getActiveWallet()).toEqual(true); - }); - }); - - return client; -}; - export const initializeSeed = async (client: Client) => { const jobType = jobTypes.WALLET_JOB_EXPORT_SEED; const jobData = {}; diff --git a/src/__test__/utils/setup.ts b/src/__test__/utils/setup.ts index 963f158a..4fe2bd72 100644 --- a/src/__test__/utils/setup.ts +++ b/src/__test__/utils/setup.ts @@ -1,6 +1,7 @@ import fetch, { Request } from 'node-fetch'; import * as fs from 'fs'; -import { setup } from '../..'; +import { question } from 'readline-sync'; +import { getClient, pair, setup } from '../..'; import * as dotenv from 'dotenv'; dotenv.config(); @@ -40,12 +41,17 @@ export const getStoredClient = () => { export const setupClient = async () => { const deviceId = process.env.DEVICE_ID; const password = process.env.PASSWORD || 'password'; - const name = process.env.name || 'SDK Test'; - return setup({ + const name = process.env.APP_NAME || 'SDK Test'; + const isPaired = await setup({ deviceId, password, name, getStoredClient, setStoredClient, }); + if (!isPaired) { + const secret = question('Please enter the pairing secret: '); + await pair(secret.toUpperCase()); + } + return getClient(); };