Skip to content

Commit

Permalink
undo interface changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed May 7, 2024
1 parent b7495b3 commit a717bc8
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 125 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ const fetchNodeDetails = new FetchNodeDetails();
const torus = new TorusUtils({ network: "mainnet", clientId: "YOUR_CLIENT_ID" }); // get your Client ID from Web3Auth Dashboard
const verifier = "google";
const verifierId = "[email protected]";
const { torusNodeEndpoints } = await fetchNodeDetails.getNodeDetails();
const publicAddress = await torus.getPublicAddress(torusNodeEndpoints, { verifier, verifierId });
const { torusNodeEndpoints, torusNodePub, torusIndexes } = await fetchNodeDetails.getNodeDetails();
const publicAddress = await torus.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId });

const idToken = "YOUR_ID_TOKEN";
const keyData = await torus.retrieveShares(torusNodeEndpoints, verifier, { verifier_id: verifierId }, idToken);
const keyData = await torus.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, { verifier_id: verifierId }, idToken);
```

```js
Expand All @@ -91,13 +91,15 @@ const verifier = "google"; // any verifier
const verifierId = "[email protected]"; // any verifier id
fetchNodeDetails
.getNodeDetails()
.then(({ torusNodeEndpoints }) => torus.getPublicAddress(torusNodeEndpoints, { verifier, verifierId }))
.then(({ torusNodeEndpoints, torusNodePub }) => torus.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }))
.then((publicAddress) => console.log(publicAddress));

const idToken = "YOUR_ID_TOKEN";
fetchNodeDetails
.getNodeDetails()
.then(({ torusNodeEndpoints }) => torus.retrieveShares(torusNodeEndpoints, verifier, { verifier_id: verifierId }, idToken))
.then(({ torusNodeEndpoints, torusIndexes }) =>
torus.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, { verifier_id: verifierId }, idToken)
)
.then((keyData) => console.log(keyData));
```

Expand All @@ -112,11 +114,11 @@ const verifier = "google"; // any verifier
const verifierId = "[email protected]"; // any verifier id
fetchNodeDetails
.getNodeDetails()
.then(({ torusNodeEndpoints }) => torus.getPublicAddress(torusNodeEndpoints, { verifier, verifierId }))
.then(({ torusNodeEndpoints, torusNodePub }) => torus.getPublicAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }))
.then((publicAddress) => console.log(publicAddress));
```

## Requirements

- This package requires a peer dependency of `@babel/runtime`
- Node 20+
- Node 16+
24 changes: 13 additions & 11 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const GetPubKeyOrKeyAssign = async (params: {
extendedVerifierId?: string;
}): Promise<KeyLookupResult> => {
const { endpoints, network, verifier, verifierId, extendedVerifierId } = params;
const minThreshold = ~~(endpoints.length / 2) + 1;
const lookupPromises = endpoints.map((x) =>
post<JRPCResponse<VerifierLookupResponse>>(
x,
Expand Down Expand Up @@ -65,12 +66,12 @@ export const GetPubKeyOrKeyAssign = async (params: {

const errorResult = thresholdSame(
lookupPubKeys.map((x2) => x2 && x2.error),
~~(endpoints.length / 2) + 1
minThreshold
);

const keyResult = thresholdSame(
lookupPubKeys.map((x3) => x3 && normalizeKeysResult(x3.result)),
~~(endpoints.length / 2) + 1
minThreshold
);

// check for nonce result in response if not a extendedVerifierId and not a legacy network
Expand Down Expand Up @@ -138,6 +139,7 @@ export async function retrieveOrImportShare(params: {
idToken: string;
importedShares?: ImportedShare[];
extraParams: Record<string, unknown>;
indexes: number[];
}): Promise<TorusKey> {
const {
legacyMetadataHost,
Expand All @@ -154,6 +156,7 @@ export async function retrieveOrImportShare(params: {
extraParams,
serverTimeOffset,
} = params;
const minThreshold = ~~(endpoints.length / 2) + 1;
await get<void>(
allowHost,
{
Expand Down Expand Up @@ -348,7 +351,7 @@ export async function retrieveOrImportShare(params: {
return undefined;
});

const thresholdPublicKey = thresholdSame(pubkeys, ~~(endpoints.length / 2) + 1);
const thresholdPublicKey = thresholdSame(pubkeys, minThreshold);

if (!thresholdPublicKey) {
throw new Error("invalid result from nodes, threshold number of public key results are not matching");
Expand All @@ -373,7 +376,7 @@ export async function retrieveOrImportShare(params: {
);
}

const thresholdReqCount = importedShares.length > 0 ? endpoints.length : ~~(endpoints.length / 2) + 1;
const thresholdReqCount = importedShares.length > 0 ? endpoints.length : minThreshold;
// optimistically run lagrange interpolation once threshold number of shares have been received
// this is matched against the user public key to ensure that shares are consistent
// Note: no need of thresholdMetadataNonce for extended_verifier_id key
Expand Down Expand Up @@ -464,9 +467,8 @@ export async function retrieveOrImportShare(params: {
return false;
});

const minThresholdRequired = ~~(endpoints.length / 2) + 1;
if (!verifierParams.extended_verifier_id && validSigs.length < minThresholdRequired) {
throw new Error(`Insufficient number of signatures from nodes, required: ${minThresholdRequired}, found: ${validSigs.length}`);
if (!verifierParams.extended_verifier_id && validSigs.length < minThreshold) {
throw new Error(`Insufficient number of signatures from nodes, required: ${minThreshold}, found: ${validSigs.length}`);
}

const validTokens = sessionTokensResolved.filter((token) => {
Expand All @@ -476,8 +478,8 @@ export async function retrieveOrImportShare(params: {
return false;
});

if (!verifierParams.extended_verifier_id && validTokens.length < minThresholdRequired) {
throw new Error(`Insufficient number of session tokens from nodes, required: ${minThresholdRequired}, found: ${validTokens.length}`);
if (!verifierParams.extended_verifier_id && validTokens.length < minThreshold) {
throw new Error(`Insufficient number of session tokens from nodes, required: ${minThreshold}, found: ${validTokens.length}`);
}
sessionTokensResolved.forEach((x, index) => {
if (!x) sessionTokenData.push(undefined);
Expand All @@ -500,7 +502,7 @@ export async function retrieveOrImportShare(params: {
[] as { index: BN; value: BN }[]
);
// run lagrange interpolation on all subsets, faster in the optimistic scenario than berlekamp-welch due to early exit
const allCombis = kCombinations(decryptedShares.length, ~~(endpoints.length / 2) + 1);
const allCombis = kCombinations(decryptedShares.length, minThreshold);

let privateKey: BN | null = null;
for (let j = 0; j < allCombis.length; j += 1) {
Expand All @@ -525,7 +527,7 @@ export async function retrieveOrImportShare(params: {
if (privateKey === undefined || privateKey === null) {
throw new Error("could not derive private key");
}
const thresholdIsNewKey = thresholdSame(isNewKeyResponses, ~~(endpoints.length / 2) + 1);
const thresholdIsNewKey = thresholdSame(isNewKeyResponses, minThreshold);

// Convert each string timestamp to a number
const serverOffsetTimes = serverTimeOffsetResponses.map((timestamp) => Number.parseInt(timestamp, 10));
Expand Down
5 changes: 5 additions & 0 deletions src/torus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Torus {

async retrieveShares(
endpoints: string[],
indexes: number[],
verifier: string,
verifierParams: VerifierParams,
idToken: string,
Expand All @@ -111,6 +112,7 @@ class Torus {
verifier,
verifierParams,
idToken,
indexes,
importedShares: [],
extraParams: {
...extraParams,
Expand All @@ -121,6 +123,7 @@ class Torus {

async getPublicAddress(
endpoints: string[],
torusNodePubs: INodePub[],
{ verifier, verifierId, extendedVerifierId }: { verifier: string; verifierId: string; extendedVerifierId?: string }
): Promise<TorusPublicKey> {
return this.getNewPublicAddress(endpoints, { verifier, verifierId, extendedVerifierId }, this.enableOneKey);
Expand Down Expand Up @@ -196,6 +199,7 @@ class Torus {
verifier,
verifierParams,
idToken,
indexes: nodeIndexes,
importedShares: sharesData,
extraParams: {
...extraParams,
Expand All @@ -210,6 +214,7 @@ class Torus {
*/
async getUserTypeAndAddress(
endpoints: string[],
torusNodePubs: INodePub[],
{ verifier, verifierId, extendedVerifierId }: { verifier: string; verifierId: string; extendedVerifierId?: string }
): Promise<TorusPublicKey> {
return this.getNewPublicAddress(endpoints, { verifier, verifierId, extendedVerifierId }, true) as Promise<TorusPublicKey>;
Expand Down
23 changes: 12 additions & 11 deletions test/aqua.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe("torus utils aqua", function () {
it("should fetch public address", async function () {
const verifier = "tkey-google-aqua"; // any verifier
const verifierDetails = { verifier, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.getPublicAddress(torusNodeEndpoints, verifierDetails);
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.getPublicAddress(torusNodeEndpoints, torusNodePub, verifierDetails);
expect(result.finalKeyData.evmAddress).to.equal("0xDfA967285AC699A70DA340F60d00DB19A272639d");
expect(result.metadata.serverTimeOffset).lessThan(20);
delete result.metadata.serverTimeOffset;
Expand Down Expand Up @@ -57,8 +57,8 @@ describe("torus utils aqua", function () {
it("should fetch user type and public address", async function () {
const verifier = "tkey-google-aqua"; // any verifier
const verifierDetails = { verifier, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result1 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, verifierDetails)) as TorusPublicKey;
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result1 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, verifierDetails)) as TorusPublicKey;
expect(result1.metadata.typeOfUser).to.equal("v2");
expect(result1.metadata.serverTimeOffset).lessThan(20);
delete result1.metadata.serverTimeOffset;
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("torus utils aqua", function () {
const v2Verifier = "tkey-google-aqua";
// 1/1 user
const v2TestEmail = "[email protected]";
const result2 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, {
const result2 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, {
verifier: v2Verifier,
verifierId: v2TestEmail,
})) as TorusPublicKey;
Expand Down Expand Up @@ -122,7 +122,7 @@ describe("torus utils aqua", function () {

// 2/n user
const v2nTestEmail = "[email protected]";
const result3 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, {
const result3 = (await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, {
verifier: v2Verifier,
verifierId: v2nTestEmail,
})) as TorusPublicKey;
Expand Down Expand Up @@ -158,8 +158,8 @@ describe("torus utils aqua", function () {
const verifier = "tkey-google-aqua"; // any verifier
const email = faker.internet.email();
const verifierDetails = { verifier, verifierId: email };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { finalKeyData, oAuthKeyData, metadata } = await torus.getPublicAddress(torusNodeEndpoints, verifierDetails);
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { finalKeyData, oAuthKeyData, metadata } = await torus.getPublicAddress(torusNodeEndpoints, torusNodePub, verifierDetails);
expect(finalKeyData.evmAddress).to.not.equal("");
expect(finalKeyData.evmAddress).to.not.equal(null);
expect(oAuthKeyData.evmAddress).to.not.equal("");
Expand All @@ -171,8 +171,8 @@ describe("torus utils aqua", function () {
it("should be able to login", async function () {
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
const verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(torusNodeEndpoints, TORUS_TEST_VERIFIER, { verifier_id: TORUS_TEST_EMAIL }, token);
const { torusNodeEndpoints, torusIndexes } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(torusNodeEndpoints, torusIndexes, TORUS_TEST_VERIFIER, { verifier_id: TORUS_TEST_EMAIL }, token);
expect(result.finalKeyData.privKey).to.be.equal("f726ce4ac79ae4475d72633c94769a8817aff35eebe2d4790aed7b5d8a84aa1d");
expect(result.metadata.serverTimeOffset).lessThan(20);
delete result.metadata.serverTimeOffset;
Expand Down Expand Up @@ -200,9 +200,10 @@ describe("torus utils aqua", function () {
const idToken = generateIdToken(TORUS_TEST_EMAIL, "ES256");
const hashedIdToken = keccak256(Buffer.from(idToken, "utf8"));
const verifierDetails = { verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { torusNodeEndpoints, torusIndexes } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(
torusNodeEndpoints,
torusIndexes,
TORUS_TEST_AGGREGATE_VERIFIER,
{
verify_params: [{ verifier_id: TORUS_TEST_EMAIL, idtoken: idToken }],
Expand Down
23 changes: 12 additions & 11 deletions test/celeste.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe("torus utils celeste", function () {
it("should fetch public address", async function () {
const verifier = "tkey-google-celeste"; // any verifier
const verifierDetails = { verifier, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.getPublicAddress(torusNodeEndpoints, verifierDetails);
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.getPublicAddress(torusNodeEndpoints, torusNodePub, verifierDetails);
expect(result.finalKeyData.evmAddress).to.equal("0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113");
expect(result.metadata.serverTimeOffset).lessThan(20);
delete result.metadata.serverTimeOffset;
Expand Down Expand Up @@ -57,8 +57,8 @@ describe("torus utils celeste", function () {
it("should fetch user type and public address", async function () {
const verifier = "tkey-google-celeste"; // any verifier
const verifierDetails = { verifier, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result1 = await torus.getUserTypeAndAddress(torusNodeEndpoints, verifierDetails);
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result1 = await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, verifierDetails);
expect(result1.finalKeyData.evmAddress).to.equal("0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113");
expect(result1.metadata.typeOfUser).to.equal("v1");
expect(result1.metadata.serverTimeOffset).lessThan(20);
Expand Down Expand Up @@ -87,7 +87,7 @@ describe("torus utils celeste", function () {
const v2Verifier = "tkey-google-celeste";
// 1/1 user
const v2TestEmail = "[email protected]";
const result2 = await torus.getUserTypeAndAddress(torusNodeEndpoints, {
const result2 = await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, {
verifier: v2Verifier,
verifierId: v2TestEmail,
});
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("torus utils celeste", function () {

// 2/n user
const v2nTestEmail = "[email protected]";
const result3 = await torus.getUserTypeAndAddress(torusNodeEndpoints, {
const result3 = await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, {
verifier: v2Verifier,
verifierId: v2nTestEmail,
});
Expand Down Expand Up @@ -156,8 +156,8 @@ describe("torus utils celeste", function () {
const verifier = "tkey-google-celeste"; // any verifier
const email = faker.internet.email();
const verifierDetails = { verifier, verifierId: email };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { finalKeyData, metadata, oAuthKeyData } = await torus.getPublicAddress(torusNodeEndpoints, verifierDetails);
const { torusNodeEndpoints, torusNodePub } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { finalKeyData, metadata, oAuthKeyData } = await torus.getPublicAddress(torusNodeEndpoints, torusNodePub, verifierDetails);
expect(finalKeyData.evmAddress).to.not.equal("");
expect(finalKeyData.evmAddress).to.not.equal(null);
expect(oAuthKeyData.evmAddress).to.not.equal("");
Expand All @@ -169,8 +169,8 @@ describe("torus utils celeste", function () {
it("should be able to login", async function () {
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
const verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(torusNodeEndpoints, TORUS_TEST_VERIFIER, { verifier_id: TORUS_TEST_EMAIL }, token);
const { torusNodeEndpoints, torusIndexes } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(torusNodeEndpoints, torusIndexes, TORUS_TEST_VERIFIER, { verifier_id: TORUS_TEST_EMAIL }, token);
expect(result.finalKeyData.privKey).to.be.equal("0ae056aa938080c9e8bf6641261619e09fd510c91bb5aad14b0de9742085a914");
expect(result.metadata.serverTimeOffset).lessThan(20);

Expand Down Expand Up @@ -199,9 +199,10 @@ describe("torus utils celeste", function () {
const idToken = generateIdToken(TORUS_TEST_EMAIL, "ES256");
const hashedIdToken = keccak256(Buffer.from(idToken, "utf8"));
const verifierDetails = { verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: TORUS_TEST_EMAIL };
const { torusNodeEndpoints } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const { torusNodeEndpoints, torusIndexes } = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const result = await torus.retrieveShares(
torusNodeEndpoints,
torusIndexes,
TORUS_TEST_AGGREGATE_VERIFIER,
{
verify_params: [{ verifier_id: TORUS_TEST_EMAIL, idtoken: idToken }],
Expand Down
Loading

0 comments on commit a717bc8

Please sign in to comment.