Skip to content

Commit

Permalink
merge with masteR
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Jul 11, 2024
2 parents 2b39412 + 09533e0 commit a981bc2
Show file tree
Hide file tree
Showing 7 changed files with 1,606 additions and 1,965 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ jobs:
name: run tests
strategy:
matrix:
node: ["18.x"]
node: ["20.x"]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: "npm"
Expand Down
3,388 changes: 1,509 additions & 1,879 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "@toruslabs/torus.js",
"version": "13.0.0-alpha.7",
"version": "14.0.0",
"description": "Handle communication with torus nodes",
"main": "dist/torusUtils.cjs.js",
"module": "dist/torusUtils.esm.js",
"main": "dist/lib.cjs/index.js",
"module": "dist/lib.esm/index.js",
"unpkg": "dist/torusUtils.umd.min.js",
"jsdelivr": "dist/torusUtils.umd.min.js",
"types": "dist/types/index.d.ts",
"sideEffects": false,
"scripts": {
"start": "torus-scripts start",
"build": "torus-scripts build",
Expand All @@ -23,29 +24,29 @@
"@babel/runtime": "7.x"
},
"dependencies": {
"@toruslabs/constants": "^13.4.0",
"@toruslabs/eccrypto": "^4.0.0",
"@toruslabs/http-helpers": "^6.1.1",
"@toruslabs/constants": "^14.0.0",
"@toruslabs/eccrypto": "^5.0.4",
"@toruslabs/http-helpers": "^7.0.0",
"bn.js": "^5.2.1",
"elliptic": "^6.5.5",
"bs58": "^5.0.0",
"ethereum-cryptography": "^2.1.3",
"ethereum-cryptography": "^2.2.1",
"json-stable-stringify": "^1.1.1",
"loglevel": "^1.9.1"
},
"devDependencies": {
"@babel/register": "^7.23.7",
"@babel/runtime": "^7.24.5",
"@toruslabs/config": "^2.0.2",
"@toruslabs/eslint-config-typescript": "^3.2.0",
"@toruslabs/fetch-node-details": "^13.4.0",
"@toruslabs/torus-scripts": "^5.3.1",
"@babel/register": "^7.24.6",
"@babel/runtime": "^7.24.7",
"@toruslabs/config": "^2.1.0",
"@toruslabs/eslint-config-typescript": "^3.3.1",
"@toruslabs/fetch-node-details": "^14.0.0",
"@toruslabs/torus-scripts": "^6.0.1",
"@types/chai": "^4.3.16",
"@types/elliptic": "^6.4.18",
"@types/faker": "^5.5.3",
"@types/json-stable-stringify": "^1.0.36",
"@types/jsonwebtoken": "^9.0.6",
"@types/mocha": "^10.0.6",
"@types/mocha": "^10.0.7",
"@types/sinon": "^17.0.3",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
Expand All @@ -54,13 +55,13 @@
"faker": "^5.5.3",
"husky": "^9.0.11",
"jsonwebtoken": "^9.0.2",
"lint-staged": "^15.2.2",
"mocha": "^10.4.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"sinon": "^17.0.2",
"lint-staged": "^15.2.7",
"mocha": "^10.6.0",
"prettier": "^3.3.2",
"rimraf": "^6.0.0",
"sinon": "^18.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.5.3"
},
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion src/helpers/metadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from "../interfaces";
import { encParamsHexToBuf, secp256k1Curve } from "./common";
import { getSecpKeyFromEd25519, keccak256 } from "./keyUtils";

export function convertMetadataToNonce(params: { message?: string }) {
if (!params || !params.message) {
return new BN(0);
Expand Down
11 changes: 6 additions & 5 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const GetPubKeyOrKeyAssign = async (params: {
fetch_node_index: true,
client_time: Math.floor(Date.now() / 1000).toString(),
}),
null,
{},
{ logTracingHeader: config.logRequestTracing }
).catch((err) => log.error(`${JRPC_METHODS.GET_OR_SET_KEY} request failed`, err))
);
Expand Down Expand Up @@ -256,7 +256,7 @@ export async function retrieveOrImportShare(params: {
extended_verifier_id: verifierParams.extended_verifier_id,
is_import_key_flow: true,
}),
null,
{},
{ logTracingHeader: config.logRequestTracing }
);
const r = retryCommitment(p, 4);
Expand Down Expand Up @@ -422,7 +422,7 @@ export async function retrieveOrImportShare(params: {
client_time: Math.floor(Date.now() / 1000).toString(),
one_key_flow: true,
}),
null,
{},
{ logTracingHeader: config.logRequestTracing }
);
promiseArrRequest.push(p);
Expand Down Expand Up @@ -664,7 +664,7 @@ export async function retrieveOrImportShare(params: {
});
})
.then(async (res) => {
const { privateKey, sessionTokenData, thresholdNonceData, nodeIndexes, isNewKey, serverTimeOffsetResponse, thresholdPubKey } = res;
const { privateKey, thresholdPubKey, sessionTokenData, nodeIndexes, thresholdNonceData, isNewKey, serverTimeOffsetResponse } = res;
let nonceResult = thresholdNonceData;
if (!privateKey) throw new Error("Invalid private key returned");

Expand All @@ -676,7 +676,8 @@ export async function retrieveOrImportShare(params: {
// if both thresholdNonceData and extended_verifier_id are not available
// then we need to throw other wise address would be incorrect.
if (!nonceResult && !verifierParams.extended_verifier_id && !LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) {
const metadataNonceResult = await getOrSetSapphireMetadataNonce(network, oAuthPubkeyX, oAuthPubkeyY, serverTimeOffset, oAuthKey);
// NOTE: dont use padded pub key anywhere in metadata apis, send pub keys as is received from nodes.
const metadataNonceResult = await getOrSetSapphireMetadataNonce(network, thresholdPubKey.X, thresholdPubKey.Y, serverTimeOffset, oAuthKey);
// rechecking nonceResult to avoid promise race condition.
if (metadataNonceResult && !thresholdNonceData) {
nonceResult = metadataNonceResult;
Expand Down
106 changes: 52 additions & 54 deletions src/some.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable promise/catch-or-return */
function capitalizeFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
Expand Down Expand Up @@ -43,62 +44,59 @@ export const Some = <K, T>(promises: Promise<K>[], predicate: (resultArr: K[], {
const errorArr: Error[] = new Array(promises.length).fill(undefined);
const resultArr: K[] = new Array(promises.length).fill(undefined);
let predicateError: Error | string;
// eslint-disable-next-line no-promise-executor-return
return promises.forEach((x, index) => {
return (
x
.then((resp: K) => {
resultArr[index] = resp;
return undefined;
})
.catch((error: Error) => {
errorArr[index] = error;
})
// eslint-disable-next-line promise/no-return-in-finally
.finally(() => {
if (sharedState.resolved) return;
return predicate(resultArr.slice(0), sharedState)
.then((data) => {
sharedState.resolved = true;
resolve(data);
return undefined;
})
.catch((error) => {
// log only the last predicate error
predicateError = error;
})
.finally(() => {
finishedCount += 1;
if (finishedCount === promises.length) {
const errors = Object.values(
resultArr.reduce((acc: Record<string, string>, z) => {
if (z) {
const { id, error } = z as { id?: string; error?: { data?: string } };
if (error?.data?.length > 0) {
if (error.data.startsWith("Error occurred while verifying params")) acc[id] = capitalizeFirstLetter(error.data);
else acc[id] = error.data;
}

promises.forEach((x, index) => {
x.then((resp: K) => {
resultArr[index] = resp;
return undefined;
})
.catch((error: Error) => {
errorArr[index] = error;
})
// eslint-disable-next-line promise/no-return-in-finally
.finally(() => {
if (sharedState.resolved) return;
return predicate(resultArr.slice(0), sharedState)
.then((data) => {
sharedState.resolved = true;
resolve(data);
return undefined;
})
.catch((error) => {
// log only the last predicate error
predicateError = error;
})
.finally(() => {
finishedCount += 1;
if (finishedCount === promises.length) {
const errors = Object.values(
resultArr.reduce((acc: Record<string, string>, z) => {
if (z) {
const { id, error } = z as { id?: string; error?: { data?: string } };
if (error?.data?.length > 0) {
if (error.data.startsWith("Error occurred while verifying params")) acc[id] = capitalizeFirstLetter(error.data);
else acc[id] = error.data;
}
return acc;
}, {})
);
}
return acc;
}, {})
);

if (errors.length > 0) {
// Format-able errors
const msg = errors.length > 1 ? `\n${errors.map((it) => `• ${it}`).join("\n")}` : errors[0];
reject(new Error(msg));
} else {
reject(
new SomeError({
errors: errorArr,
responses: resultArr,
predicate: (predicateError as Error)?.message || (predicateError as string),
})
);
}
if (errors.length > 0) {
// Format-able errors
const msg = errors.length > 1 ? `\n${errors.map((it) => `• ${it}`).join("\n")}` : errors[0];
reject(new Error(msg));
} else {
reject(
new SomeError({
errors: errorArr,
responses: resultArr,
predicate: (predicateError as Error)?.message || (predicateError as string),
})
);
}
});
})
);
}
});
});
});
});
18 changes: 15 additions & 3 deletions test/setup.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import Register from "@babel/register";
import register from "@babel/register";

Register({
import currentPkg from "../package.json" assert { type: "json" };

const runtimeVersion = currentPkg.peerDependencies["@babel/runtime"];

register({
presets: [["@babel/env", { bugfixes: true }], "@babel/typescript"],
plugins: [
"@babel/plugin-syntax-bigint",
"@babel/plugin-transform-object-rest-spread",
"@babel/plugin-transform-class-properties",
["@babel/transform-runtime", { version: runtimeVersion }],
"@babel/plugin-transform-numeric-separator",
],
sourceType: "unambiguous",
extensions: [".ts", ".js"],
rootMode: "upward",
});

0 comments on commit a981bc2

Please sign in to comment.