From 98f249f1a0c60a696f0ea2161e0da22fb6fb5df6 Mon Sep 17 00:00:00 2001 From: netbonus <151201453+netbonus@users.noreply.github.com> Date: Mon, 18 Dec 2023 08:07:32 -0500 Subject: [PATCH 01/27] Improves firmware flag error in btc tests --- src/__test__/e2e/btc.test.ts | 55 ++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/__test__/e2e/btc.test.ts b/src/__test__/e2e/btc.test.ts index cbdc9f6e..75e202bc 100644 --- a/src/__test__/e2e/btc.test.ts +++ b/src/__test__/e2e/btc.test.ts @@ -1,6 +1,12 @@ -import { getPrng, getTestnet } from '../utils/getters'; -// You must have `FEATURE_TEST_RUNNER=0` enabled in firmware to run these tests. +/** + * IMPORTANT: ENABLE `FEATURE_TEST_RUNNER=0` IN FIRMWARE SETTINGS + * + * Failure to enable this setting will result in an `Invalid Request` error. + * Ensure `FEATURE_TEST_RUNNER=0` is active before executing these tests. + */ + import bip32 from 'bip32'; +import { getPrng, getTestnet } from '../utils/getters'; import { BTC_PURPOSE_P2PKH, BTC_PURPOSE_P2SH_P2WPKH, @@ -11,7 +17,7 @@ import { parseWalletJobResp, serializeJobData, setup_btc_sig_test, - stripDER + stripDER, } from '../utils/helpers'; import { initializeClient } from '../utils/initializeClient'; import { testRequest } from '../utils/testRequest'; @@ -19,11 +25,11 @@ import { testRequest } from '../utils/testRequest'; const prng = getPrng(); const TEST_TESTNET = !!getTestnet() || false; let wallet: Wallet | null = null; -type InputObj = { hash: string, value: number, signerIdx: number, idx: number }; +type InputObj = { hash: string; value: number; signerIdx: number; idx: number }; // Build the inputs. By default we will build 10. Note that there are `n` tests for // *each category*, where `n` is the number of inputs. -function rand32Bit () { +function rand32Bit() { return Math.floor(prng.quick() * 2 ** 32); } const inputs: InputObj[] = []; @@ -40,9 +46,9 @@ for (let i = 0; i < count; i++) { inputs.push({ hash: hash.toString('hex'), value, signerIdx, idx }); } -async function testSign ({ txReq, signingKeys, sigHashes, client }: any) { +async function testSign({ txReq, signingKeys, sigHashes, client }: any) { const tx = await client.sign(txReq); - const len = tx?.sigs?.length ?? 0 + const len = tx?.sigs?.length ?? 0; expect(len).toEqual(signingKeys.length); expect(len).toEqual(sigHashes.length); for (let i = 0; i < len; i++) { @@ -53,12 +59,12 @@ async function testSign ({ txReq, signingKeys, sigHashes, client }: any) { `Signature validation failed for priv=${signingKeys[ i ].privateKey.toString('hex')}, ` + - `hash=${sigHashes[i].toString('hex')}, sig=${sig.toString('hex')}`, + `hash=${sigHashes[i].toString('hex')}, sig=${sig.toString('hex')}`, ); } } -async function runTestSet ( +async function runTestSet( opts: any, wallet: Wallet | null, inputsSlice: InputObj[], @@ -69,20 +75,32 @@ async function runTestSet ( // Testnet + change opts.isTestnet = true; opts.useChange = true; - await testSign({ ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), client }); + 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), client }); + 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), client }); + 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), client }); + await testSign({ + ...setup_btc_sig_test(opts, wallet, inputsSlice, prng), + client, + }); } describe('Bitcoin', () => { @@ -99,7 +117,14 @@ describe('Bitcoin', () => { testID: 0, // wallet_job test ID payload: serializeJobData(jobType, activeWalletUID, jobData), }; - const res = await testRequest(jobReq); + const res = await testRequest(jobReq).catch((err) => { + if (err.message.includes('Invalid Request')) { + console.error( + 'Ensure FEATURE_TEST_RUNNER=0 is active in firmware settings', + ); + } + throw err; + }); //@ts-expect-error - accessing private property const _res = parseWalletJobResp(res, client.fwVersion); expect(_res.resultStatus).toEqual(0); @@ -190,6 +215,6 @@ describe('Bitcoin', () => { await runTestSet(opts, wallet, inputsSlice, client); }); }); - }) + }); } }); From b2e4fb88265db8f0bfdecde4b161e2aa0021ca78 Mon Sep 17 00:00:00 2001 From: netbonus <151201453+netbonus@users.noreply.github.com> Date: Mon, 18 Dec 2023 08:26:03 -0500 Subject: [PATCH 02/27] Runs prettier against entire codebase --- .eslintrc | 54 +- .prettierrc | 24 +- .readthedocs.yml | 2 +- README.md | 4 +- example/index.html | 35 +- src/__test__/e2e/eth.msg.test.ts | 64 +- src/__test__/e2e/general.test.ts | 42 +- src/__test__/e2e/kv.test.ts | 16 +- src/__test__/e2e/non-exportable.test.ts | 177 +- .../e2e/signing/__mocks__/programs.ts | 1540 +---------------- src/__test__/e2e/signing/bls.test.ts | 129 +- src/__test__/e2e/signing/determinism.test.ts | 75 +- src/__test__/e2e/signing/evm-abi.test.ts | 66 +- src/__test__/e2e/signing/evm-tx.test.ts | 2 +- src/__test__/e2e/signing/solana.test.ts | 2 +- src/__test__/e2e/signing/unformatted.test.ts | 2 +- src/__test__/e2e/wallet-jobs.test.ts | 131 +- .../integration/__mocks__/addKvRecords.json | 2 +- .../integration/__mocks__/connect.json | 2 +- .../__mocks__/fetchActiveWallet.json | 2 +- .../integration/__mocks__/getAddresses.json | 2 +- .../integration/__mocks__/getKvRecords.json | 2 +- .../integration/__mocks__/handlers.ts | 86 +- .../__mocks__/removeKvRecords.json | 2 +- src/__test__/integration/__mocks__/server.ts | 6 +- src/__test__/integration/__mocks__/setup.ts | 10 +- src/__test__/integration/__mocks__/sign.json | 2 +- src/__test__/integration/connect.test.ts | 66 +- .../integration/fetchCalldataDecoder.test.ts | 2 +- src/__test__/unit/__mocks__/decoderData.ts | 58 +- src/__test__/unit/decoders.test.ts | 16 +- src/__test__/unit/validators.test.ts | 6 +- src/__test__/utils/__test__/builders.test.ts | 13 +- .../utils/__test__/serializers.test.ts | 38 +- src/__test__/utils/builders.ts | 4 +- src/__test__/utils/determinism.ts | 10 +- src/__test__/utils/ethers.ts | 15 +- src/__test__/utils/getters.ts | 6 +- src/__test__/utils/helpers.ts | 69 +- src/__test__/utils/runners.ts | 24 +- src/__test__/utils/serializers.ts | 4 +- src/__test__/utils/testRequest.ts | 2 +- src/__test__/vectors.jsonc | 18 +- src/api/signing.ts | 4 +- src/api/utilities.ts | 10 +- src/bitcoin.ts | 23 +- src/calldata/evm.ts | 77 +- src/calldata/index.ts | 10 +- src/client.ts | 90 +- src/constants.ts | 9 +- src/ethereum.ts | 317 ++-- src/functions/addKvRecords.ts | 2 +- src/functions/connect.ts | 2 +- src/functions/fetchActiveWallet.ts | 2 +- src/functions/fetchEncData.ts | 72 +- src/functions/getAddresses.ts | 2 +- src/functions/getKvRecords.ts | 2 +- src/functions/pair.ts | 2 +- src/functions/removeKvRecords.ts | 2 +- src/functions/sign.ts | 2 +- src/genericSigning.ts | 12 +- src/protocol/index.ts | 2 +- src/protocol/secureMessages.ts | 2 +- src/shared/predicates.ts | 1 - src/shared/utilities.ts | 10 +- src/shared/validators.ts | 2 +- src/types/addKvRecords.d.ts | 2 +- src/types/connect.d.ts | 2 +- src/types/firmware.d.ts | 8 +- src/types/messages.d.ts | 2 +- src/types/removeKvRecords.d.ts | 2 +- src/types/secureMessages.d.ts | 4 +- src/types/shared.d.ts | 2 +- src/types/utils.d.ts | 2 +- src/types/vitest.d.ts | 7 +- src/util.ts | 203 ++- 76 files changed, 1170 insertions(+), 2553 deletions(-) diff --git a/.eslintrc b/.eslintrc index 85c4444c..a8334e6d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,30 +1,30 @@ { - "env": { - "es6": false, - "mocha": true, - "node": true - }, - "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], - "parser": "@typescript-eslint/parser", - "plugins": ["@typescript-eslint"], - "root": true, - "globals": { - "expect": true, - "shallow": true, - "render": true, - "mount": true, - "beforeAll": true, - "cy": true - }, - "rules": { - "eqeqeq": ["error"], - "no-var": ["warn"], - "no-duplicate-imports": ["error"], - "prefer-const": ["error"], - "prefer-spread": ["error"], - "no-console": ["off"], - "quotes": ["error", "single"], - "react/react-in-jsx-scope": "off", - "@typescript-eslint/no-explicit-any": "off" + "env": { + "es6": false, + "mocha": true, + "node": true + }, + "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + "parser": "@typescript-eslint/parser", + "plugins": ["@typescript-eslint"], + "root": true, + "globals": { + "expect": true, + "shallow": true, + "render": true, + "mount": true, + "beforeAll": true, + "cy": true + }, + "rules": { + "eqeqeq": ["error"], + "no-var": ["warn"], + "no-duplicate-imports": ["error"], + "prefer-const": ["error"], + "prefer-spread": ["error"], + "no-console": ["off"], + "quotes": ["error", "single"], + "react/react-in-jsx-scope": "off", + "@typescript-eslint/no-explicit-any": "off" } } diff --git a/.prettierrc b/.prettierrc index ee81f71e..23899362 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,13 +1,13 @@ { - "overrides": [ - { - "files": "**/src/*.ts", - "options": { - "parser": "typescript" - } - } - ], - "singleQuote": true, - "tabWidth": 2, - "trailingComma": "all" -} \ No newline at end of file + "overrides": [ + { + "files": "**/src/*.ts", + "options": { + "parser": "typescript" + } + } + ], + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "all" +} diff --git a/.readthedocs.yml b/.readthedocs.yml index 6f00923b..a36db8a1 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -20,4 +20,4 @@ formats: all python: version: 3.7 install: - - requirements: docs/requirements.txt \ No newline at end of file + - requirements: docs/requirements.txt diff --git a/README.md b/README.md index f9594cb6..3142d5ee 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ # GridPlus Lattice1 SDK -* **For help with this SDK, see the [GridPlus SDK Documentation](https://gridplus.github.io/gridplus-sdk)** -* **For help with your Lattice1 hardware, see the [Lattice1 Documentation](https://docs.gridplus.io)** +- **For help with this SDK, see the [GridPlus SDK Documentation](https://gridplus.github.io/gridplus-sdk)** +- **For help with your Lattice1 hardware, see the [Lattice1 Documentation](https://docs.gridplus.io)** This SDK is designed to facilitate communication with a user's [Lattice1 hardware wallet](https://gridplus.io/lattice). Once paired to a given Lattice, an instance of this SDK is used to make encrypted requests for things like getting addresses/public keys and making signatures. diff --git a/example/index.html b/example/index.html index 1a808336..2ee3e1d6 100644 --- a/example/index.html +++ b/example/index.html @@ -1,22 +1,19 @@ +
+ + + +