-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
2,268 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
2,038 changes: 2,038 additions & 0 deletions
2,038
packages/hooks/modal-react-hooks/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{ | ||
"name": "@web3auth/modal-react-hooks", | ||
"version": "8.0.1", | ||
"description": "Wallet services for which can be used with Web3Auth or private key providers used inside Web3Auth", | ||
"keywords": [ | ||
"web3Auth/wallet-services-plugin", | ||
"web3Auth", | ||
"blockchain", | ||
"ethereum", | ||
"metamask" | ||
], | ||
"main": "dist/modalReactHooks.cjs.js", | ||
"module": "dist/modalReactHooks.esm.js", | ||
"unpkg": "dist/modalReactHooks.umd.min.js", | ||
"jsdelivr": "dist/modalReactHooks.umd.min.js", | ||
"types": "dist/types/index.d.ts", | ||
"author": "Torus Labs", | ||
"homepage": "https://github.com/Web3Auth/Web3Auth/tree/master/packages/plugins/wallet-services-plugin#readme", | ||
"license": "ISC", | ||
"scripts": { | ||
"test": "mocha --config ../../../.mocharc.json test/**.ts", | ||
"test-debugger": "mocha --config ../../../.mocharc.json --inspect-brk test/**.ts", | ||
"dev": "torus-scripts start", | ||
"build": "torus-scripts build", | ||
"lint": "eslint --fix 'src/**/*.ts'", | ||
"prepack": "npm run build", | ||
"pre-commit": "lint-staged --cwd ." | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"devDependencies": { | ||
"@metamask/rpc-errors": "^6.2.1", | ||
"@web3auth/openlogin-adapter": "^8.0.1" | ||
}, | ||
"dependencies": { | ||
"@toruslabs/ethereum-controllers": "^5.5.3", | ||
"@toruslabs/openlogin-jrpc": "^8.0.0", | ||
"@web3auth/base": "^8.0.0", | ||
"@web3auth/base-plugin": "^8.0.1", | ||
"@web3auth/no-modal": "^8.0.1", | ||
"@web3auth/ws-embed": "^1.1.1", | ||
"loglevel": "^1.9.1" | ||
}, | ||
"peerDependencies": { | ||
"@babel/runtime": "^7.x", | ||
"@web3auth/base-plugin": "^8.x", | ||
"@web3auth/modal": "^8.x", | ||
"@web3auth/openlogin-adapter": "^8.x", | ||
"react": "^18.x" | ||
}, | ||
"lint-staged": { | ||
"!(*d).ts": [ | ||
"eslint --cache --fix", | ||
"prettier --write" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Web3Auth/Web3Auth.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Web3Auth/Web3Auth/issues" | ||
}, | ||
"engines": { | ||
"node": ">=18.x", | ||
"npm": ">=9.x" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IAdapter } from "@web3auth/base"; | ||
import { IPlugin } from "@web3auth/base-plugin"; | ||
import { Web3Auth, Web3AuthOptions } from "@web3auth/modal"; | ||
import React, { createContext, createElement, useEffect } from "react"; | ||
|
||
export type Web3AuthContextConfig = { | ||
web3AuthOptions: Web3AuthOptions; | ||
adapters?: IAdapter<unknown>[]; | ||
plugins?: IPlugin[]; | ||
}; | ||
|
||
export const Web3AuthContext = createContext<Web3Auth | null>(null); | ||
|
||
interface Web3AuthProviderProps { | ||
config: Web3AuthContextConfig; | ||
} | ||
|
||
export function Web3AuthProvider(params: React.PropsWithChildren<Web3AuthProviderProps>) { | ||
const { children, config } = params; | ||
const [web3Auth, setWeb3Auth] = React.useState<Web3Auth | null>(null); | ||
|
||
useEffect(() => { | ||
const { web3AuthOptions, adapters = [], plugins = [] } = config; | ||
const web3Instance = new Web3Auth(web3AuthOptions); | ||
if (adapters.length) adapters.map((adapter) => web3Instance.configureAdapter(adapter)); | ||
if (plugins.length) plugins.map((plugin) => web3Instance.addPlugin(plugin)); | ||
setWeb3Auth(web3Instance); | ||
}, []); | ||
|
||
const props = { value: web3Auth }; | ||
return createElement(Web3AuthContext.Provider, props, children); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { useWeb3Auth } from "./useWeb3auth"; | ||
export { Web3AuthProvider } from "./Web3AuthProvider"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { ADAPTER_EVENTS, IProvider, WALLET_ADAPTERS } from "@web3auth/base"; | ||
import { ModalConfig } from "@web3auth/modal"; | ||
import { LoginParams, OpenloginAdapter, type OpenloginUserInfo } from "@web3auth/openlogin-adapter"; | ||
import { useContext, useEffect, useState } from "react"; | ||
|
||
import { Web3AuthContext } from "./Web3AuthProvider"; | ||
|
||
const WAIT_FOR_INIT_MSG = "Wait for web3auth to be ready first"; | ||
|
||
export const useWeb3Auth = () => { | ||
const web3auth = useContext(Web3AuthContext); | ||
|
||
const [isConnected, setConnected] = useState<boolean>(false); | ||
const [provider, setProvider] = useState<IProvider | null>(null); | ||
const [userInfo, setUserInfo] = useState<Partial<OpenloginUserInfo> | null>(null); | ||
const [isMFAEnabled, setIsMFAEnabled] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const addState = async () => { | ||
setProvider(web3auth.provider); | ||
const userState = await web3auth.getUserInfo(); | ||
setUserInfo(userState); | ||
setIsMFAEnabled(userState?.isMfaEnabled || false); | ||
}; | ||
|
||
const resetState = () => { | ||
setProvider(null); | ||
setUserInfo(null); | ||
setIsMFAEnabled(false); | ||
}; | ||
|
||
if (web3auth) { | ||
if (isConnected) addState(); | ||
else resetState(); | ||
} | ||
}, [web3auth, isConnected]); | ||
|
||
const initModal = async (params: { modalConfig?: Record<string, ModalConfig> } = {}) => { | ||
if (!web3auth) throw new Error(WAIT_FOR_INIT_MSG); | ||
|
||
await web3auth.initModal(params); | ||
}; | ||
|
||
useEffect(() => { | ||
if (web3auth) { | ||
web3auth.on(ADAPTER_EVENTS.CONNECTED, () => setConnected(true)); | ||
web3auth.on(ADAPTER_EVENTS.DISCONNECTED, () => setConnected(false)); | ||
} | ||
}, [web3auth]); | ||
|
||
async function enableMFA(params: Partial<LoginParams> = {}) { | ||
if (!web3auth) throw new Error(WAIT_FOR_INIT_MSG); | ||
if (!isConnected) throw new Error("Connect to a wallet first"); | ||
if (web3auth.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN) throw new Error("Enable MFA is only supported for OpenLogin."); | ||
|
||
if (web3auth.connectedAdapterName === WALLET_ADAPTERS.OPENLOGIN) { | ||
await (web3auth.walletAdapters[WALLET_ADAPTERS.OPENLOGIN] as OpenloginAdapter).openloginInstance?.enableMFA(params); | ||
setIsMFAEnabled(userInfo?.isMfaEnabled || false); | ||
} | ||
} | ||
|
||
async function logout(params: { cleanup: boolean } = { cleanup: false }) { | ||
if (!web3auth) throw new Error(WAIT_FOR_INIT_MSG); | ||
if (!isConnected) throw new Error("Connect to a wallet first"); | ||
|
||
await web3auth.logout(params); | ||
} | ||
|
||
async function connect(): Promise<IProvider | null> { | ||
if (!web3auth) throw new Error(WAIT_FOR_INIT_MSG); | ||
|
||
const localProvider = await web3auth.connect(); | ||
return localProvider; | ||
} | ||
|
||
return { | ||
web3auth, | ||
isConnected, | ||
provider, | ||
userInfo, | ||
isMFAEnabled, | ||
initModal, | ||
connect, | ||
enableMFA, | ||
logout, | ||
addChain: web3auth?.addChain, | ||
addPlugin: web3auth?.addPlugin, | ||
authenticateUser: web3auth?.authenticateUser, | ||
switchChain: web3auth?.switchChain, | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("../../../torus.config"); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"include": ["src"] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"include": ["src","test"], | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"strict": true, | ||
"jsx": "react", | ||
"noEmit": true, | ||
"allowImportingTsExtensions": true | ||
}, | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const path = require("path"); | ||
const generateWebpackConfig = require("../../../webpack.config"); | ||
|
||
const pkg = require("./package.json"); | ||
|
||
const currentPath = path.resolve("."); | ||
|
||
const config = generateWebpackConfig({ | ||
currentPath, | ||
pkg, | ||
alias: {}, | ||
}); | ||
|
||
module.exports = config; |
This file contains 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