Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimises fnd call if node details passed in the constructor #371

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions examples/vue-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/vue-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@toruslabs/broadcast-channel": "10.0.1",
"@toruslabs/constants": "^14.0.0",
"@toruslabs/customauth": "file:../../",
"@toruslabs/fnd-base": "^14.0.0",
"@toruslabs/openlogin-starkkey": "^3.2.0",
"@toruslabs/torus.js": "^15.0.2",
"@toruslabs/vue-components": "^7.8.3",
Expand Down
13 changes: 10 additions & 3 deletions examples/vue-app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@
</template>

<script setup lang="ts">
import { TORUS_LEGACY_NETWORK, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
import { KEY_TYPE, TORUS_LEGACY_NETWORK, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
import { CustomAuth, LoginWindowResponse, TorusLoginResponse, TorusVerifierResponse, UX_MODE } from "@toruslabs/customauth";
import { fetchLocalConfig } from "@toruslabs/fnd-base";
import { getStarkHDAccount, pedersen, sign, STARKNET_NETWORKS, verify } from "@toruslabs/openlogin-starkkey";
import { TorusKey } from "@toruslabs/torus.js";
import { Button, Card, Select, TextArea, TextField } from "@toruslabs/vue-components";
Expand Down Expand Up @@ -305,28 +306,34 @@ const loadResponse = (privKeyInfo: TorusKey["finalKeyData"], localUserInfo: Toru
const initCustomAuth = async () => {
const { network, uxMode } = formData.value;
switch (uxMode) {
case UX_MODE.REDIRECT:
case UX_MODE.REDIRECT: {
const nodeDetails = fetchLocalConfig(network, KEY_TYPE.SECP256K1);
customAuthSdk.value = new CustomAuth({
baseUrl: `${window.location.origin}`,
redirectPathName: "auth",
enableLogging: true,
network,
uxMode,
web3AuthClientId: WEB3AUTH_CLIENT_ID,
nodeDetails,
});
await customAuthSdk.value.init({ skipSw: true });
break;
case UX_MODE.POPUP:
}
case UX_MODE.POPUP: {
const nodeDetails = fetchLocalConfig(network, KEY_TYPE.SECP256K1);
customAuthSdk.value = new CustomAuth({
uxMode,
baseUrl: `${window.location.origin}/serviceworker`,
enableLogging: true,
network,
popupFeatures: `titlebar=0,toolbar=0,status=0,location=0,menubar=0,height=500,width=500,top=100,left=100`,
web3AuthClientId: WEB3AUTH_CLIENT_ID,
nodeDetails,
});
await customAuthSdk.value.init();
break;
}
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/handlers/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TORUS_NETWORK_TYPE } from "@toruslabs/constants";
import { INodeDetails, TORUS_NETWORK_TYPE } from "@toruslabs/constants";
import { KeyType, TorusKey } from "@toruslabs/torus.js";

import { Sentry } from "../sentry";
Expand Down Expand Up @@ -241,6 +241,8 @@ export interface CustomAuthArgs {
* @defaultValue undefined
*/
useDkg?: boolean;

nodeDetails?: INodeDetails;
}

export interface InitParams {
Expand Down
12 changes: 10 additions & 2 deletions src/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TORUS_NETWORK_TYPE } from "@toruslabs/constants";
import { type INodeDetails, TORUS_NETWORK_TYPE } from "@toruslabs/constants";
import { NodeDetailManager } from "@toruslabs/fetch-node-details";
import { keccak256, Torus, TorusKey } from "@toruslabs/torus.js";
import { keccak256, type KeyType, Torus, TorusKey } from "@toruslabs/torus.js";

import createHandler from "./handlers/HandlerFactory";
import {
Expand Down Expand Up @@ -41,6 +41,8 @@ class CustomAuth {
useDkg?: boolean;
web3AuthClientId: string;
web3AuthNetwork: TORUS_NETWORK_TYPE;
keyType: KeyType;
nodeDetails: INodeDetails;
};

torus: Torus;
Expand Down Expand Up @@ -69,6 +71,7 @@ class CustomAuth {
metadataUrl = "https://metadata.tor.us",
keyType = "secp256k1",
serverTimeOffset = 0,
nodeDetails,
}: CustomAuthArgs) {
if (!web3AuthClientId) throw new Error("Please provide a valid web3AuthClientId in constructor");
if (!network) throw new Error("Please provide a valid network in constructor");
Expand All @@ -86,6 +89,8 @@ class CustomAuth {
useDkg,
web3AuthClientId,
web3AuthNetwork: network,
keyType,
nodeDetails,
};
const torus = new Torus({
network,
Expand Down Expand Up @@ -281,6 +286,9 @@ class CustomAuth {
name: SENTRY_TXNS.FETCH_NODE_DETAILS,
},
async () => {
if (this.config.nodeDetails) {
return this.config.nodeDetails;
}
return this.nodeDetailManager.getNodeDetails({ verifier, verifierId });
}
);
Expand Down