Last active
September 30, 2022 18:26
-
-
Save himanshuchawla009/110a4d7cd775e536e58b11b048e33b78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Web3AuthCore } from "@web3auth/core" | |
import Torus from "@toruslabs/torus.js"; | |
import { subkey } from "@toruslabs/openlogin-subkey"; | |
import NodeDetailManager, { ETHEREUM_NETWORK } from "@toruslabs/fetch-node-details"; | |
// Web3Auth clientId | |
const clientId = | |
"BKjpD5DNAFDbX9Ty9RSBAXdQP8YDY1rldKqKCgbxxa8JZODZ8zxVRzlT74qRIHsor5aIwZ55dQVlcmrwJu37PI8"; // get from https://dashboard.web3auth.io | |
const TORUS_NETWORK = { | |
TESTNET: "testnet", | |
MAINNET: "mainnet", | |
CYAN: "cyan", | |
} | |
export const CONTRACT_MAP = { | |
[TORUS_NETWORK.MAINNET]: NodeDetailManager.PROXY_ADDRESS_MAINNET, | |
[TORUS_NETWORK.TESTNET]: NodeDetailManager.PROXY_ADDRESS_ROPSTEN, | |
[TORUS_NETWORK.CYAN]: NodeDetailManager.PROXY_ADDRESS_POLYGON, | |
}; | |
export const NETWORK_MAP = { | |
[TORUS_NETWORK.MAINNET]: ETHEREUM_NETWORK.MAINNET, | |
[TORUS_NETWORK.TESTNET]: ETHEREUM_NETWORK.ROPSTEN, | |
[TORUS_NETWORK.CYAN]: ETHEREUM_NETWORK.POLYGON, | |
}; | |
const ethNetwork = NETWORK_MAP[TORUS_NETWORK.TESTNET]; | |
const torus = new Torus({ | |
enableOneKey: true, | |
network: TORUS_NETWORK.TESTNET, | |
}); | |
const nodeDetailManager = new NodeDetailManager({ network: ethNetwork, proxyAddress: CONTRACT_MAP[TORUS_NETWORK.TESTNET] }); | |
async function login(verifier, verifierId, idToken) { | |
const { torusNodeEndpoints, torusNodePub, torusIndexes } = await nodeDetailManager.getNodeDetails({ verifier, verifierId }); | |
const userDetails = await torus.getUserTypeAndAddress(torusNodeEndpoints, torusNodePub, { verifier, verifierId }, true); | |
// new user and hasn't enabled mfa | |
if (userDetails.typeOfUser === "v2" && !userDetails.upgraded) { | |
const keyDetails = await torus.retrieveShares(torusNodeEndpoints, torusIndexes, verifier, { verifier_id: verifierId }, idToken, {}); | |
const finalPrivKey = subkey(keyDetails.privKey.padStart(64, "0"), Buffer.from(clientId, "base64")); | |
// final priv key of user, in example use this key to generate a provider from @web3auth/ethereum-provider package | |
return finalPrivKey | |
} else { | |
// do the web3auth login here using web3auth core by passing idToken | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment