Skip to content

Commit 840a285

Browse files
Merge pull request #81 from Web3Auth/feat/add-more-userinfo
Feat/add more userinfo
2 parents 81cfced + cf3e13f commit 840a285

File tree

3 files changed

+84
-19
lines changed

3 files changed

+84
-19
lines changed

examples/react-app/src/components/Account.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Divider from "./Divider";
88

99
function Account() {
1010
const [addressToShow, setAddressToShow] = useState<string>("");
11-
const { address, userInfo, getUserInfo } = usePlayground();
11+
const { address, userInfo, getUserInfo, getExternalIdToken } = usePlayground();
1212

1313
useEffect(() => {
1414
setAddressToShow(address || "");
@@ -27,9 +27,12 @@ function Account() {
2727
<div>
2828
<h3 className="font-bold text-app-gray-800 mb-2">{userInfo?.name || ""}</h3>
2929
<p className="text-xs text-app-gray-400 mb-1">{userInfo?.email ? userInfo?.email : userInfo?.name}</p>
30-
<button type="button" className="leading-none text-xs text-app-primary-600 hover:underline" onClick={getUserInfo}>
30+
<p><button type="button" className="leading-none text-xs text-app-primary-600 hover:underline" onClick={getUserInfo}>
3131
View User Info
32-
</button>
32+
</button></p>
33+
<p><button type="button" className="leading-none text-xs text-app-primary-600 hover:underline" onClick={getExternalIdToken}>
34+
Get External Id Token
35+
</button></p>
3336
</div>
3437
<Divider />
3538
<Button

examples/react-app/src/services/playground.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CHAIN_NAMESPACES, IProvider, log, WEB3AUTH_NETWORK, IPlugin } from "@we
44
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
55
import { PasskeysPlugin } from "@web3auth/passkeys-sfa-plugin";
66
// Import Single Factor Auth SDK for no redirect flow
7-
import { ADAPTER_EVENTS, decodeToken, Web3Auth } from "@web3auth/single-factor-auth";
7+
import { ADAPTER_EVENTS, decodeToken, UserAuthInfo, Web3Auth } from "@web3auth/single-factor-auth";
88
import { WalletServicesPlugin } from "@web3auth/wallet-services-plugin";
99
import { createContext, ReactNode, useCallback, useContext, useEffect, useMemo, useState } from "react";
1010

@@ -62,6 +62,7 @@ export interface IPlaygroundContext {
6262
unlinkPasskey: (id: number) => void;
6363
logout: () => void;
6464
getUserInfo: () => Promise<AuthUserInfo | null>;
65+
getExternalIdToken: () => Promise<UserAuthInfo | null>;
6566
showCheckout: () => void;
6667
showWalletUI: () => void;
6768
showWalletScanner: () => void;
@@ -94,6 +95,7 @@ export const PlaygroundContext = createContext<IPlaygroundContext>({
9495
unlinkPasskey: async () => null,
9596
logout: async () => null,
9697
getUserInfo: async () => null,
98+
getExternalIdToken: async () => null,
9799
showCheckout: async () => null,
98100
showWalletUI: async () => null,
99101
showWalletScanner: async () => null,
@@ -326,6 +328,17 @@ export function Playground({ children }: IPlaygroundProps) {
326328
return null;
327329
}, [web3authSFAuth]);
328330

331+
const getExternalIdToken = useCallback(async (): Promise<UserAuthInfo | null> => {
332+
if (web3authSFAuth && web3authSFAuth?.connected) {
333+
const data = await web3authSFAuth?.authenticateUser();
334+
setPlaygroundConsoleTitle("Id Token");
335+
setPlaygroundConsoleData(JSON.stringify(data, null, 2));
336+
uiConsole(data);
337+
return data;
338+
}
339+
return null;
340+
}, [web3authSFAuth]);
341+
329342
const showCheckout = useCallback(async () => {
330343
if (!wsPlugin) {
331344
uiConsole("wallet services plugin not initialized yet");
@@ -523,6 +536,7 @@ export function Playground({ children }: IPlaygroundProps) {
523536
unlinkPasskey,
524537
logout,
525538
getUserInfo,
539+
getExternalIdToken,
526540
showCheckout,
527541
showWalletUI,
528542
showWalletScanner,
@@ -554,6 +568,7 @@ export function Playground({ children }: IPlaygroundProps) {
554568
unlinkPasskey,
555569
logout,
556570
getUserInfo,
571+
getExternalIdToken,
557572
showCheckout,
558573
showWalletUI,
559574
showWalletScanner,

src/Web3Auth.ts

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
102102
}
103103

104104
get connected(): boolean {
105-
return Boolean(this.sessionManager?.sessionId);
105+
return this.status === ADAPTER_STATUS.CONNECTED;
106106
}
107107

108108
get provider(): IProvider | null {
@@ -182,9 +182,16 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
182182

183183
const { chainNamespace, chainId } = this.coreOptions.chainConfig || {};
184184
if (!this.authInstance || !this.privKeyProvider) throw WalletInitializationError.notReady();
185-
const accounts = await this.privKeyProvider.provider.request<unknown, string[]>({
186-
method: chainNamespace === CHAIN_NAMESPACES.EIP155 ? "eth_accounts" : "getAccounts",
187-
});
185+
const [accounts, publicKey] = await Promise.all([
186+
this.privKeyProvider.provider.request<unknown, string[]>({
187+
method: chainNamespace === CHAIN_NAMESPACES.EIP155 ? "eth_accounts" : "getAccounts",
188+
}),
189+
this.privKeyProvider.provider.request<unknown, string[]>({
190+
method: "public_key",
191+
}),
192+
]);
193+
// const thresholdPrivKey = this._getBasePrivKey();
194+
188195
if (accounts && accounts.length > 0) {
189196
const existingToken = getSavedToken(accounts[0] as string, "SFA");
190197
if (existingToken) {
@@ -204,6 +211,35 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
204211
issuedAt: new Date().toISOString(),
205212
};
206213

214+
const additionalMetadata = {
215+
email: userInfo.email,
216+
name: userInfo.name,
217+
profileImage: userInfo.profileImage,
218+
aggregateVerifier: userInfo.aggregateVerifier,
219+
verifier: userInfo.verifier,
220+
verifierId: userInfo.verifierId,
221+
typeOfLogin: userInfo.typeOfLogin || "jwt",
222+
oAuthIdToken: userInfo.oAuthIdToken,
223+
oAuthAccessToken: userInfo.oAuthAccessToken,
224+
wallets: [] as unknown[],
225+
signatures: this.state.signatures,
226+
network: this.coreOptions.web3AuthNetwork,
227+
};
228+
229+
if (this.coreOptions.usePnPKey) {
230+
additionalMetadata.wallets.push({
231+
public_key: publicKey,
232+
type: "web3auth_app_key",
233+
curve: chainNamespace === CHAIN_NAMESPACES.EIP155 ? KEY_TYPE.SECP256K1 : KEY_TYPE.ED25519,
234+
});
235+
} else {
236+
additionalMetadata.wallets.push({
237+
public_key: publicKey,
238+
type: "web3auth_threshold_key",
239+
curve: chainNamespace === CHAIN_NAMESPACES.EIP155 ? KEY_TYPE.SECP256K1 : KEY_TYPE.ED25519,
240+
});
241+
}
242+
207243
const challenge = await signChallenge(payload, chainNamespace);
208244
const signedMessage = await this._getSignedMessage(challenge, accounts, chainNamespace);
209245
const idToken = await verifySignedChallenge(
@@ -213,7 +249,9 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
213249
this.SFA_ISSUER,
214250
this.coreOptions.sessionTime,
215251
this.coreOptions.clientId,
216-
this.coreOptions.web3AuthNetwork as WEB3AUTH_NETWORK_TYPE
252+
this.coreOptions.web3AuthNetwork as WEB3AUTH_NETWORK_TYPE,
253+
undefined,
254+
additionalMetadata
217255
);
218256
saveToken(accounts[0] as string, "SFA", idToken);
219257
return {
@@ -318,11 +356,12 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
318356

319357
async logout(): Promise<void> {
320358
if (!this.connected) throw WalletLoginError.notConnectedError("Not logged in");
321-
const sessionId = this.currentStorage.get<string>("sessionId");
322-
if (!sessionId) throw WalletLoginError.fromCode(5000, "User not logged in");
323359

324-
await this.sessionManager.invalidateSession();
325-
this.currentStorage.set("sessionId", "");
360+
if (this.coreOptions.mode !== SDK_MODE.NODE) {
361+
await this.sessionManager.invalidateSession();
362+
this.currentStorage.set("sessionId", "");
363+
}
364+
326365
this.updateState({
327366
privKey: "",
328367
userInfo: {
@@ -415,18 +454,26 @@ export class Web3Auth extends SafeEventEmitter<Web3AuthSfaEvents> implements IWe
415454
let finalPrivKey = privKey.padStart(64, "0");
416455
// get app scoped keys.
417456
if (this.coreOptions.usePnPKey) {
418-
const pnpPrivKey = subkey(finalPrivKey, Buffer.from(this.coreOptions.clientId, "base64"));
419-
finalPrivKey = pnpPrivKey.padStart(64, "0");
457+
finalPrivKey = this.getSubKey(finalPrivKey);
420458
}
421459
if (this.coreOptions.chainConfig.chainNamespace === CHAIN_NAMESPACES.SOLANA) {
422-
if (!this.privKeyProvider.getEd25519Key) {
423-
throw WalletLoginError.fromCode(5000, "Private key provider is not valid, Missing getEd25519Key function");
424-
}
425-
finalPrivKey = this.privKeyProvider.getEd25519Key(finalPrivKey);
460+
finalPrivKey = this.getEd25519Key(finalPrivKey);
426461
}
427462
return finalPrivKey;
428463
}
429464

465+
private getSubKey(privKey: string) {
466+
const pnpPrivKey = subkey(privKey, Buffer.from(this.coreOptions.clientId, "base64"));
467+
return pnpPrivKey.padStart(64, "0");
468+
}
469+
470+
private getEd25519Key(privKey: string) {
471+
if (!this.privKeyProvider.getEd25519Key) {
472+
throw WalletLoginError.fromCode(5000, "Private key provider is not valid, Missing getEd25519Key function");
473+
}
474+
return this.privKeyProvider.getEd25519Key(privKey);
475+
}
476+
430477
private async getTorusKey(loginParams: LoginParams): Promise<string> {
431478
const { verifier, verifierId, idToken, subVerifierInfoArray } = loginParams;
432479

0 commit comments

Comments
 (0)