From a820a1f3aa28b8df21df149f27808291ac73f051 Mon Sep 17 00:00:00 2001 From: woody <125113430+woodenfurniture@users.noreply.github.com> Date: Tue, 5 Dec 2023 06:57:08 +1100 Subject: [PATCH] perf: remove argon bench on boot, use sane defaults across the board (#654) --- examples/sandbox/package.json | 32 ++-- integration/package.json | 20 +-- lerna.json | 2 +- packages/hdwallet-coinbase/package.json | 4 +- packages/hdwallet-core/package.json | 2 +- .../hdwallet-keepkey-chromeusb/package.json | 6 +- .../hdwallet-keepkey-electron/package.json | 4 +- .../hdwallet-keepkey-nodehid/package.json | 4 +- .../hdwallet-keepkey-nodewebusb/package.json | 6 +- packages/hdwallet-keepkey-tcp/package.json | 6 +- packages/hdwallet-keepkey-webusb/package.json | 6 +- packages/hdwallet-keepkey/package.json | 4 +- packages/hdwallet-keplr/package.json | 4 +- packages/hdwallet-ledger-webhid/package.json | 6 +- packages/hdwallet-ledger-webusb/package.json | 6 +- packages/hdwallet-ledger/package.json | 4 +- .../package.json | 4 +- packages/hdwallet-metamask/package.json | 4 +- packages/hdwallet-native-vault/package.json | 4 +- .../src/argonBenchmark.test.ts | 57 ------ .../src/argonBenchmark.ts | 168 ------------------ packages/hdwallet-native-vault/src/index.ts | 1 - .../hdwallet-native-vault/src/rawVault.ts | 11 +- packages/hdwallet-native/package.json | 4 +- packages/hdwallet-portis/package.json | 4 +- packages/hdwallet-tallyho/package.json | 4 +- packages/hdwallet-trezor-connect/package.json | 6 +- packages/hdwallet-trezor/package.json | 4 +- packages/hdwallet-walletconnect/package.json | 4 +- .../hdwallet-walletconnectV2/package.json | 4 +- packages/hdwallet-xdefi/package.json | 4 +- 31 files changed, 86 insertions(+), 313 deletions(-) delete mode 100644 packages/hdwallet-native-vault/src/argonBenchmark.test.ts delete mode 100644 packages/hdwallet-native-vault/src/argonBenchmark.ts diff --git a/examples/sandbox/package.json b/examples/sandbox/package.json index b9e755f29..cdf3a0d9b 100644 --- a/examples/sandbox/package.json +++ b/examples/sandbox/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-sandbox", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "private": true, "browserslist": "> 0.5%, last 2 versions, not dead", @@ -11,21 +11,21 @@ }, "dependencies": { "@metamask/eth-sig-util": "^7.0.0", - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey-tcp": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey-webusb": "1.52.12", - "@shapeshiftoss/hdwallet-keplr": "1.52.12", - "@shapeshiftoss/hdwallet-ledger": "1.52.12", - "@shapeshiftoss/hdwallet-ledger-webhid": "1.52.12", - "@shapeshiftoss/hdwallet-ledger-webusb": "1.52.12", - "@shapeshiftoss/hdwallet-metamask": "1.52.12", - "@shapeshiftoss/hdwallet-native": "1.52.12", - "@shapeshiftoss/hdwallet-portis": "1.52.12", - "@shapeshiftoss/hdwallet-tallyho": "1.52.12", - "@shapeshiftoss/hdwallet-trezor": "1.52.12", - "@shapeshiftoss/hdwallet-trezor-connect": "1.52.12", - "@shapeshiftoss/hdwallet-walletconnect": "1.52.12", - "@shapeshiftoss/hdwallet-xdefi": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey-tcp": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey-webusb": "1.52.13", + "@shapeshiftoss/hdwallet-keplr": "1.52.13", + "@shapeshiftoss/hdwallet-ledger": "1.52.13", + "@shapeshiftoss/hdwallet-ledger-webhid": "1.52.13", + "@shapeshiftoss/hdwallet-ledger-webusb": "1.52.13", + "@shapeshiftoss/hdwallet-metamask": "1.52.13", + "@shapeshiftoss/hdwallet-native": "1.52.13", + "@shapeshiftoss/hdwallet-portis": "1.52.13", + "@shapeshiftoss/hdwallet-tallyho": "1.52.13", + "@shapeshiftoss/hdwallet-trezor": "1.52.13", + "@shapeshiftoss/hdwallet-trezor-connect": "1.52.13", + "@shapeshiftoss/hdwallet-walletconnect": "1.52.13", + "@shapeshiftoss/hdwallet-xdefi": "1.52.13", "bip32": "^2.0.4", "eip-712": "^1.0.0", "jquery": "^3.7.1", diff --git a/integration/package.json b/integration/package.json index 73995f485..28d16f0aa 100644 --- a/integration/package.json +++ b/integration/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/integration", - "version": "1.52.12", + "version": "1.52.13", "main": "index.js", "license": "MIT", "private": true, @@ -10,15 +10,15 @@ "dev": "lerna run test --scope integration --parallel --include-filtered-dependencies" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey-nodewebusb": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey-tcp": "1.52.12", - "@shapeshiftoss/hdwallet-ledger": "1.52.12", - "@shapeshiftoss/hdwallet-native": "1.52.12", - "@shapeshiftoss/hdwallet-portis": "1.52.12", - "@shapeshiftoss/hdwallet-trezor": "1.52.12", - "@shapeshiftoss/hdwallet-xdefi": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey-nodewebusb": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey-tcp": "1.52.13", + "@shapeshiftoss/hdwallet-ledger": "1.52.13", + "@shapeshiftoss/hdwallet-native": "1.52.13", + "@shapeshiftoss/hdwallet-portis": "1.52.13", + "@shapeshiftoss/hdwallet-trezor": "1.52.13", + "@shapeshiftoss/hdwallet-xdefi": "1.52.13", "fast-json-stable-stringify": "^2.1.0", "msw": "^0.27.1", "whatwg-fetch": "^3.6.2" diff --git a/lerna.json b/lerna.json index 0e24a401d..855bb7f82 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "lerna": "5.2.0", - "version": "1.52.12", + "version": "1.52.13", "npmClient": "yarn", "useWorkspaces": true, "command": { diff --git a/packages/hdwallet-coinbase/package.json b/packages/hdwallet-coinbase/package.json index 41dea027f..916ca85db 100644 --- a/packages/hdwallet-coinbase/package.json +++ b/packages/hdwallet-coinbase/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-coinbase", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -15,7 +15,7 @@ }, "dependencies": { "@coinbase/wallet-sdk": "^3.6.6", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "eth-rpc-errors": "^4.0.3", "lodash": "^4.17.21" }, diff --git a/packages/hdwallet-core/package.json b/packages/hdwallet-core/package.json index 9856f7911..f3c7e7219 100644 --- a/packages/hdwallet-core/package.json +++ b/packages/hdwallet-core/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-core", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" diff --git a/packages/hdwallet-keepkey-chromeusb/package.json b/packages/hdwallet-keepkey-chromeusb/package.json index 058927cfd..a77fe85e0 100644 --- a/packages/hdwallet-keepkey-chromeusb/package.json +++ b/packages/hdwallet-keepkey-chromeusb/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-chromeusb", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey": "1.52.12" + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13" } } diff --git a/packages/hdwallet-keepkey-electron/package.json b/packages/hdwallet-keepkey-electron/package.json index 61ba7c24b..d21356e0f 100644 --- a/packages/hdwallet-keepkey-electron/package.json +++ b/packages/hdwallet-keepkey-electron/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-electron", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-keepkey": "1.52.12", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13", "uuid": "^8.3.2" }, "peerDependencies": { diff --git a/packages/hdwallet-keepkey-nodehid/package.json b/packages/hdwallet-keepkey-nodehid/package.json index e83850936..96dce47b3 100644 --- a/packages/hdwallet-keepkey-nodehid/package.json +++ b/packages/hdwallet-keepkey-nodehid/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-nodehid", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-keepkey": "1.52.12" + "@shapeshiftoss/hdwallet-keepkey": "1.52.13" }, "peerDependencies": { "node-hid": "^2.1.1" diff --git a/packages/hdwallet-keepkey-nodewebusb/package.json b/packages/hdwallet-keepkey-nodewebusb/package.json index b2b47f5f3..bd390a66e 100644 --- a/packages/hdwallet-keepkey-nodewebusb/package.json +++ b/packages/hdwallet-keepkey-nodewebusb/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-nodewebusb", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,8 +14,8 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey": "1.52.12" + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13" }, "peerDependencies": { "usb": "^2.3.1" diff --git a/packages/hdwallet-keepkey-tcp/package.json b/packages/hdwallet-keepkey-tcp/package.json index 4a2642140..c2014b8de 100644 --- a/packages/hdwallet-keepkey-tcp/package.json +++ b/packages/hdwallet-keepkey-tcp/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-tcp", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,8 +14,8 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13", "axios": "^0.21.1" } } diff --git a/packages/hdwallet-keepkey-webusb/package.json b/packages/hdwallet-keepkey-webusb/package.json index 64fd4bb78..a99362e2b 100644 --- a/packages/hdwallet-keepkey-webusb/package.json +++ b/packages/hdwallet-keepkey-webusb/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey-webusb", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,8 +14,8 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-keepkey": "1.52.12" + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-keepkey": "1.52.13" }, "devDependencies": { "@types/w3c-web-usb": "^1.0.4" diff --git a/packages/hdwallet-keepkey/package.json b/packages/hdwallet-keepkey/package.json index e5c16e89b..90ca0398b 100644 --- a/packages/hdwallet-keepkey/package.json +++ b/packages/hdwallet-keepkey/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keepkey", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -20,7 +20,7 @@ "@keepkey/device-protocol": "^7.12.2", "@metamask/eth-sig-util": "^7.0.0", "@shapeshiftoss/bitcoinjs-lib": "5.2.0-shapeshift.2", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@shapeshiftoss/proto-tx-builder": "^0.8.0", "bignumber.js": "^9.0.1", "bnb-javascript-sdk-nobroadcast": "^2.16.14", diff --git a/packages/hdwallet-keplr/package.json b/packages/hdwallet-keplr/package.json index 736b96ceb..314b489a4 100644 --- a/packages/hdwallet-keplr/package.json +++ b/packages/hdwallet-keplr/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-keplr", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -15,7 +15,7 @@ }, "dependencies": { "@shapeshiftoss/caip": "8.15.0", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@shapeshiftoss/proto-tx-builder": "^0.8.0", "@shapeshiftoss/types": "3.1.3", "base64-js": "^1.5.1", diff --git a/packages/hdwallet-ledger-webhid/package.json b/packages/hdwallet-ledger-webhid/package.json index 216d7bb63..8a432dd6b 100644 --- a/packages/hdwallet-ledger-webhid/package.json +++ b/packages/hdwallet-ledger-webhid/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-ledger-webhid", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -19,8 +19,8 @@ "@ledgerhq/hw-transport": "^6.7.0", "@ledgerhq/hw-transport-webhid": "^6.7.0", "@ledgerhq/live-common": "^21.8.2", - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-ledger": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-ledger": "1.52.13", "@types/w3c-web-hid": "^1.0.2" }, "devDependencies": { diff --git a/packages/hdwallet-ledger-webusb/package.json b/packages/hdwallet-ledger-webusb/package.json index 4a720b15d..5e3fc4de9 100644 --- a/packages/hdwallet-ledger-webusb/package.json +++ b/packages/hdwallet-ledger-webusb/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-ledger-webusb", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -20,8 +20,8 @@ "@ledgerhq/hw-transport-webusb": "^6.7.0", "@ledgerhq/live-common": "^21.8.2", "@ledgerhq/logs": "^6.10.1", - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-ledger": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-ledger": "1.52.13", "@types/w3c-web-usb": "^1.0.4", "p-queue": "^7.4.1" }, diff --git a/packages/hdwallet-ledger/package.json b/packages/hdwallet-ledger/package.json index 3e383c98a..81624b639 100644 --- a/packages/hdwallet-ledger/package.json +++ b/packages/hdwallet-ledger/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-ledger", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -18,7 +18,7 @@ "@ethereumjs/common": "^2.4.0", "@ethereumjs/tx": "^3.3.0", "@shapeshiftoss/bitcoinjs-lib": "5.2.0-shapeshift.2", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "base64-js": "^1.5.1", "bchaddrjs": "^0.4.4", "bitcoinjs-message": "^2.0.0", diff --git a/packages/hdwallet-metamask-shapeshift-multichain/package.json b/packages/hdwallet-metamask-shapeshift-multichain/package.json index 2e57090d5..310eff763 100644 --- a/packages/hdwallet-metamask-shapeshift-multichain/package.json +++ b/packages/hdwallet-metamask-shapeshift-multichain/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-shapeshift-multichain", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -17,7 +17,7 @@ "@metamask/detect-provider": "^1.2.0", "@metamask/onboarding": "^1.0.1", "@shapeshiftoss/common-api": "^9.3.0", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@shapeshiftoss/metamask-snaps-adapter": "^1.0.8", "@shapeshiftoss/metamask-snaps-types": "^1.0.8", "eth-rpc-errors": "^4.0.3", diff --git a/packages/hdwallet-metamask/package.json b/packages/hdwallet-metamask/package.json index a55e4f088..ffc01d436 100644 --- a/packages/hdwallet-metamask/package.json +++ b/packages/hdwallet-metamask/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-metamask", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -16,7 +16,7 @@ "dependencies": { "@metamask/detect-provider": "^1.2.0", "@metamask/onboarding": "^1.0.1", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "eth-rpc-errors": "^4.0.3", "lodash": "^4.17.21" }, diff --git a/packages/hdwallet-native-vault/package.json b/packages/hdwallet-native-vault/package.json index d94b84f4f..e8f13064d 100644 --- a/packages/hdwallet-native-vault/package.json +++ b/packages/hdwallet-native-vault/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-native-vault", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-native": "1.52.12", + "@shapeshiftoss/hdwallet-native": "1.52.13", "bip39": "^3.0.4", "hash-wasm": "^4.9.0", "idb-keyval": "^6.0.3", diff --git a/packages/hdwallet-native-vault/src/argonBenchmark.test.ts b/packages/hdwallet-native-vault/src/argonBenchmark.test.ts deleted file mode 100644 index a14193cc3..000000000 --- a/packages/hdwallet-native-vault/src/argonBenchmark.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { argonBenchmark, customNow } from "./argonBenchmark"; -import { deterministicGetRandomValues } from "./deterministicGetRandomValues.test"; -import { setCrypto, setPerformance } from "./util"; - -const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); - -const performance = require("perf_hooks").performance; - -jest.setTimeout(30 * 1000); - -describe("argonBenchmark", () => { - beforeAll(async () => { - const realCrypto = require("crypto").webcrypto as Crypto; - setCrypto({ - randomUUID: realCrypto.randomUUID, - subtle: realCrypto.subtle, - getRandomValues: await deterministicGetRandomValues(realCrypto), - }); - setPerformance(performance); - }); - - describe("customNow", () => { - it("should work", async () => { - const now = customNow(performance.now.bind(performance), 10, 0.05); - const start = now(); - await delay(100); - const duration = now() - start; - expect(duration).toBeGreaterThan(50); - // This is performance-dependent - // expect(duration).toBeLessThan(200) - }); - }); - - describe("argonBenchmark", () => { - it("should run correctly", async () => { - const results = await argonBenchmark(32 * 1024, 1000, { - measureError: true, - // jitter: 0.10, - // roundMs: 100, - now: performance.now.bind(performance), - }); - console.info(results); - expect(results).toBeTruthy(); - expect(Number.isSafeInteger(results.iterations)).toBeTruthy(); - expect(results.iterations).toBeGreaterThanOrEqual(1); - expect(results.error).toBeGreaterThanOrEqual(0); - expect(results.bits).toBeGreaterThanOrEqual(0); - // These are performance-dependent - // expect(results.bits).toBeGreaterThan(5) - // expect(results.bits).toBeLessThan(15) - expect(results.durations.overall).toBeGreaterThanOrEqual(0); - expect(results.durations.setup).toBeGreaterThanOrEqual(0); - expect(results.durations.warmup).toBeGreaterThanOrEqual(0); - expect(results.durations.msPerIteration).toBeGreaterThan(0); - }); - }); -}); diff --git a/packages/hdwallet-native-vault/src/argonBenchmark.ts b/packages/hdwallet-native-vault/src/argonBenchmark.ts deleted file mode 100644 index bb5492565..000000000 --- a/packages/hdwallet-native-vault/src/argonBenchmark.ts +++ /dev/null @@ -1,168 +0,0 @@ -import { argon2id } from "hash-wasm"; - -import { crypto, performance } from "./util"; - -async function argonBenchInner(memorySize: number, iterations: number, now: () => number) { - const password = await (await crypto).getRandomValues(new Uint8Array(32)); - const salt = await (await crypto).getRandomValues(new Uint8Array(32)); - const start = now(); - await argon2id({ - password, - salt, - parallelism: 1, - memorySize, - iterations, - hashLength: 32, - outputType: "binary", - }); - return now() - start; -} - -export function customNow(now: () => number, roundMs: number, jitter: number): () => number { - if (jitter) { - let lastRealNow = 0; - now = ((thisNow) => () => { - const realNow = thisNow(); - const nextNow = - lastRealNow === 0 - ? realNow - : (() => { - const elapsed = (realNow - lastRealNow) * (1 + (Math.random() - 0.5) * jitter); - return lastRealNow + elapsed; - })(); - lastRealNow = realNow; - return nextNow; - })(now); - } - if (roundMs) { - now = ( - (thisNow) => () => - Math.round(thisNow() / roundMs) * roundMs - )(now); - } - return now; -} - -/** - * This function benchmarks the current machine to determine how many iterations of single-theaded argon2id can be afforded in a given time budget. - * The time budget is considered to include only the time spent actually iterating; setup time is proportional to the memory allocation and excluded, - * though it usually only is a few tens of milliseconds extra. - * - * Benchmarks in Node and the browser show that the error in the final value is typically less than 5%, with - * @param memorySizeKib The amount of memory used by argon2id; must be at least 8. A higher value provides more defense against ASIC and GPU attacks - * @param targetTimeMs Desired duration of the key derivation operation - * @param options Optional parameters primarily useful for testing the benchmark implementation - * @param options.fineTuning Number of fine-tuning rounds permitted; defaults to 0. Allowing more fine-tuning rounds reduces error. - * @param options.measureError Calculate the error of the final result by measuring the precise time the recommended number of iterations takes. - * @param options.now Overrides the timestamp function used to measure durations. Must return a monotonically-increasing number of milliseconds. - * @param options.roundMs Rounds timestamps to simulate results when a browser rounds performance.now() to prevent fingerprinting. - * @param options.jitter Introduces random timestamp errors of the specified magnitude. Range 0-1, default 0. - * @returns - */ -export async function argonBenchmark( - memorySizeKib: number, - targetTimeMs: number, - options: Partial<{ - fineTuning: number; - measureError: boolean; - now: () => number; - roundMs: number; - jitter: number; - }> = {} -) { - const preciseNow = options.now ?? (await performance).now.bind(await performance); - const overallStart = preciseNow(); - - let fineTuning = options.fineTuning ?? 0; - const measureError = options.measureError ?? false; - const roundedNow = customNow(preciseNow, options.roundMs ?? 0, options.jitter ?? 0); - - // warm-up wasm module with the minimum-possible parameter values to ensure the module is actually loaded; should take - // no more than 100ms in the absolute worst case - const warmupDuration = await argonBenchInner(8, 1, roundedNow); - - // Calculate the minimum time a single iteration of the shortest kind takes; this is later used to calculate the bits - // of added security. The low memory use ensures that the setup time is negligible. - const minMsPerIter = await (async () => { - let minDuration = 0; - let out = 0; - for (let i = 1; minDuration === 0; i *= 2) { - minDuration = await argonBenchInner(8, 1, roundedNow); - out = minDuration / i; - } - return out; - })(); - - let firstDuration = 0; - let firstI = 0; - let duration = 0; - let msPerIteration = 0; - let setupDuration = 0; - let i = 0.5; // after the initial i *= 2, this will always be an integer - while (duration < targetTimeMs + setupDuration) { - if (msPerIteration === 0) { - // This ensures that a non-increasing performance.now() implementation won't loop infinitely - if (i * memorySizeKib > 2 ** 20) break; - // This happen on the first loop, but also can happen with rounded timestamps - i *= 2; - } else { - const iStep = Math.round((targetTimeMs + setupDuration - duration) / msPerIteration); - // We don't need fine-tuning if we're dead on - if (iStep == 0) break; - // Ensuring each step is at least i large sacrifices accuracy, but limits the benchmark time to no more than 4 - // times the target duration. Additional fine-tuning steps can increase accuracy, but each one adds a possible - // 2 * targetTimeMs to the worst-case. - if (Math.abs(iStep) < 0) { - if (fineTuning <= 0) break; - fineTuning--; - } - i = i + iStep; - } - - duration = await argonBenchInner(memorySizeKib, i, roundedNow); - msPerIteration = (duration - setupDuration) / i; - // We want the the duration of the first *measurable* run (i.e. duration > 0). (Duration can be 0 if timestamps are - // rounded.) - if (!firstDuration) { - firstDuration = duration; - firstI = i; - } - // As we test higher iteration counts, we get a more accurate msPerIteration calculation, leading to - setupDuration = Math.max(0, firstDuration - msPerIteration * firstI); - } - if (msPerIteration === 0) throw new Error("benchmark runs look instantaneous -- is performance.now() working?"); - - // Allow no more than i/2 to be shaved off the final time; this limits the damage a significantly outlying final - // benchmark run can do - i -= Math.min((duration - (targetTimeMs + setupDuration)) / msPerIteration, i / 2); - i = Math.ceil(i); - - let error: number | undefined = undefined; - if (measureError) { - // bench with recommended iterations to determine precise error; this will obviously take extra time - const finalDuration = await argonBenchInner(memorySizeKib, i, preciseNow); - error = Math.abs(finalDuration - (targetTimeMs + setupDuration)) / (targetTimeMs + setupDuration); - } - - const bits = Math.log2((i * msPerIteration - setupDuration) / minMsPerIter); - const overallDuration = preciseNow() - overallStart; - return { - iterations: i, - bits, - error, - durations: { - warmup: warmupDuration, - setup: setupDuration, - overall: overallDuration, - msPerIteration, - }, - options: { - ...{ - ...options, - now: undefined, - }, - memorySizeKib, - targetTimeMs, - }, - }; -} diff --git a/packages/hdwallet-native-vault/src/index.ts b/packages/hdwallet-native-vault/src/index.ts index 1f150a6ac..317ff0ba6 100644 --- a/packages/hdwallet-native-vault/src/index.ts +++ b/packages/hdwallet-native-vault/src/index.ts @@ -1,7 +1,6 @@ import { createMnemonic, crypto, entropyToMnemonic, GENERATE_MNEMONIC } from "./util"; import { Vault } from "./vault"; -export { argonBenchmark } from "./argonBenchmark"; export type { ISealableVaultFactory, IVault, IVaultFactory } from "./types"; export { GENERATE_MNEMONIC } from "./util"; export { Vault } from "./vault"; diff --git a/packages/hdwallet-native-vault/src/rawVault.ts b/packages/hdwallet-native-vault/src/rawVault.ts index 550b5ed55..bfb83432b 100644 --- a/packages/hdwallet-native-vault/src/rawVault.ts +++ b/packages/hdwallet-native-vault/src/rawVault.ts @@ -5,7 +5,6 @@ import * as jose from "jose"; import * as ta from "type-assertions"; import * as uuid from "uuid"; -import { argonBenchmark } from "./argonBenchmark"; import { ArgonParams, IVaultBackedBy, IVaultFactory, VaultPrepareParams } from "./types"; import { crypto, encoder, keyStoreUUID, Revocable, revocable, setCrypto, setPerformance, vaultStoreUUID } from "./util"; @@ -69,16 +68,16 @@ export class RawVault extends Revocable(Object.freeze(class {})) implements IVau (await idb.get("defaultArgonParams", await RawVault.#keyStore)) ?? { then: (onfulfilled, onrejected) => { return (async () => { + // For more details on parameter selection, see: + // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-argon2-04#section-4 const out: ArgonParams = { parallelism: 1, memorySize: 32 * 1024, - iterations: 26, + iterations: 16, }; - const argonBenchmarkResults = await argonBenchmark(out.memorySize, 1000, { measureError: true }); - console.debug("argonBenchmarkResults:", argonBenchmarkResults); - await idb.set("argonBenchmarkResults", argonBenchmarkResults, await RawVault.#keyStore); - out.iterations = argonBenchmarkResults.iterations; + await idb.set("defaultArgonParams", out, await RawVault.#keyStore); + return out; })().then(onfulfilled, onrejected); }, diff --git a/packages/hdwallet-native/package.json b/packages/hdwallet-native/package.json index 8882421d4..f9c252d98 100644 --- a/packages/hdwallet-native/package.json +++ b/packages/hdwallet-native/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-native", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -16,7 +16,7 @@ "dependencies": { "@shapeshiftoss/bitcoinjs-lib": "5.2.0-shapeshift.2", "@shapeshiftoss/fiosdk": "1.2.1-shapeshift.6", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@shapeshiftoss/proto-tx-builder": "^0.8.0", "@zxing/text-encoding": "^0.9.0", "bchaddrjs": "^0.4.9", diff --git a/packages/hdwallet-portis/package.json b/packages/hdwallet-portis/package.json index 0a54af899..a119741b9 100644 --- a/packages/hdwallet-portis/package.json +++ b/packages/hdwallet-portis/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-portis", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -15,7 +15,7 @@ }, "dependencies": { "@portis/web3": "3.0.10", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "base64-js": "^1.5.1", "bip32": "^2.0.4", "bitcoinjs-lib": "^5.1.6", diff --git a/packages/hdwallet-tallyho/package.json b/packages/hdwallet-tallyho/package.json index 699022220..94d4cfc3c 100644 --- a/packages/hdwallet-tallyho/package.json +++ b/packages/hdwallet-tallyho/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-tallyho", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "lodash": "^4.17.21", "tallyho-onboarding": "^1.0.2" }, diff --git a/packages/hdwallet-trezor-connect/package.json b/packages/hdwallet-trezor-connect/package.json index 6968f3c20..873d46f8e 100644 --- a/packages/hdwallet-trezor-connect/package.json +++ b/packages/hdwallet-trezor-connect/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-trezor-connect", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,8 +14,8 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", - "@shapeshiftoss/hdwallet-trezor": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", + "@shapeshiftoss/hdwallet-trezor": "1.52.13", "@trezor/rollout": "^1.2.0", "trezor-connect": "^8.2.1" } diff --git a/packages/hdwallet-trezor/package.json b/packages/hdwallet-trezor/package.json index b4a821398..92f664368 100644 --- a/packages/hdwallet-trezor/package.json +++ b/packages/hdwallet-trezor/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-trezor", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -17,7 +17,7 @@ "dependencies": { "@ethereumjs/common": "^2.4.0", "@ethereumjs/tx": "^3.3.0", - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "base64-js": "^1.5.1", "lodash": "^4.17.21" }, diff --git a/packages/hdwallet-walletconnect/package.json b/packages/hdwallet-walletconnect/package.json index 9e654eec5..e5afa9e42 100644 --- a/packages/hdwallet-walletconnect/package.json +++ b/packages/hdwallet-walletconnect/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-walletconnect", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -15,7 +15,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@walletconnect/qrcode-modal": "^1.7.8", "@walletconnect/web3-provider": "^1.7.8", "ethers": "^5.6.5" diff --git a/packages/hdwallet-walletconnectV2/package.json b/packages/hdwallet-walletconnectV2/package.json index f80b3a2be..9ed0c517c 100644 --- a/packages/hdwallet-walletconnectV2/package.json +++ b/packages/hdwallet-walletconnectV2/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-walletconnectv2", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -15,7 +15,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "@walletconnect/ethereum-provider": "^2.10.1", "@walletconnect/modal": "^2.6.2", "ethers": "^5.6.5" diff --git a/packages/hdwallet-xdefi/package.json b/packages/hdwallet-xdefi/package.json index 5542cc96a..10bafe958 100644 --- a/packages/hdwallet-xdefi/package.json +++ b/packages/hdwallet-xdefi/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/hdwallet-xdefi", - "version": "1.52.12", + "version": "1.52.13", "license": "MIT", "publishConfig": { "access": "public" @@ -14,7 +14,7 @@ "prepublishOnly": "yarn clean && yarn build" }, "dependencies": { - "@shapeshiftoss/hdwallet-core": "1.52.12", + "@shapeshiftoss/hdwallet-core": "1.52.13", "lodash": "^4.17.21" }, "devDependencies": {