Skip to content

Commit b7565f0

Browse files
committed
fix: TypeGuard
1 parent 5cd320d commit b7565f0

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

packages/sdk/src/providers/gno-wallet/gno-social-wallet.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CommonPrivateKeyProvider } from '@web3auth/base-provider';
44
import { Web3AuthNoModal } from '@web3auth/no-modal';
55

66
import {
7-
ExtraLoginOptions,
7+
SocialExtraLoginOptions,
88
NetworkInfo,
99
SocialEmailPasswordlessConfigure,
1010
SocialGoogleConfigure,
@@ -23,7 +23,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
2323
private config: SocialGoogleConfigure | SocialTwitterConfigure | SocialEmailPasswordlessConfigure;
2424
private chainConfig: CustomChainConfig;
2525
private loginConfig: LoginConfig;
26-
private extraLoginOptions: ExtraLoginOptions;
26+
private extraLoginOptions: SocialExtraLoginOptions;
2727

2828
constructor(
2929
socialType: SocialType,
@@ -50,47 +50,55 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
5050
}
5151
}
5252

53-
public setExtraLoginOptions(extraLoginOptions: ExtraLoginOptions): void {
53+
public setExtraLoginOptions(extraLoginOptions: SocialExtraLoginOptions): void {
5454
this.extraLoginOptions = extraLoginOptions;
5555
}
5656

5757
private createLoginConfig(): LoginConfig {
5858
switch (this.socialType) {
5959
case SocialType.GOOGLE:
60-
const googleConfig = this.config as SocialGoogleConfigure;
60+
if (!isSocialGoogleConfigure(this.config)) {
61+
throw new Error('Invalid Google configuration');
62+
}
63+
6164
return {
6265
[this.socialType]: {
6366
typeOfLogin: 'google',
64-
name: googleConfig.name,
65-
clientId: googleConfig.googleClientId,
66-
verifier: googleConfig.verifier,
67+
name: this.config.name,
68+
clientId: this.config.googleClientId,
69+
verifier: this.config.verifier,
6770
},
6871
};
6972

7073
case SocialType.TWITTER:
71-
const twitterConfig = this.config as SocialTwitterConfigure;
74+
if (!isSocialTwitterConfigure(this.config)) {
75+
throw new Error('Invalid Twitter configuration');
76+
}
7277
return {
7378
[this.socialType]: {
7479
typeOfLogin: 'twitter',
75-
name: twitterConfig.name,
76-
verifier: twitterConfig.verifier,
77-
clientId: twitterConfig.authClientId,
80+
name: this.config.name,
81+
verifier: this.config.verifier,
82+
clientId: this.config.authClientId,
7883
jwtParameters: {
7984
connection: 'twitter',
8085
verifyerIdField: 'sub',
81-
domain: twitterConfig.domain,
86+
domain: this.config.domain,
8287
},
8388
},
8489
};
8590

8691
case SocialType.EMAIL:
87-
const emailConfig = this.config as SocialEmailPasswordlessConfigure;
92+
if (!isSocialEmailPasswordlessConfigure(this.config)) {
93+
throw new Error('Invalid Email configuration');
94+
}
95+
8896
return {
8997
[this.socialType]: {
9098
typeOfLogin: 'email_passwordless',
91-
name: emailConfig.name,
92-
verifier: emailConfig.verifier,
93-
clientId: emailConfig.clientId,
99+
name: this.config.name,
100+
verifier: this.config.verifier,
101+
clientId: this.config.clientId,
94102
},
95103
};
96104

@@ -142,7 +150,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
142150
.catch(() => false);
143151
};
144152

145-
private async requestPrivateKey() {
153+
private async requestPrivateKey(): Promise<string> {
146154
if (!this.web3auth.provider) {
147155
throw new Error('Not initialized web3 provider.');
148156
}
@@ -176,7 +184,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
176184
return web3auth;
177185
}
178186

179-
public static async createGoogle(config: SocialGoogleConfigure) {
187+
public static async createGoogle(config: SocialGoogleConfigure): Promise<GnoSocialWalletProvider> {
180188
const networkConfig: NetworkInfo = {
181189
chainId: config.chainId,
182190
addressPrefix: config.addressPrefix || GNO_ADDRESS_PREFIX,
@@ -188,7 +196,7 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
188196
return new GnoSocialWalletProvider(SocialType.GOOGLE, config, [networkConfig]);
189197
}
190198

191-
public static async createTwitter(config: SocialTwitterConfigure) {
199+
public static async createTwitter(config: SocialTwitterConfigure): Promise<GnoSocialWalletProvider> {
192200
const networkConfig: NetworkInfo = {
193201
chainId: config.chainId,
194202
addressPrefix: config.addressPrefix || GNO_ADDRESS_PREFIX,
@@ -200,7 +208,9 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
200208
return new GnoSocialWalletProvider(SocialType.TWITTER, config, [networkConfig]);
201209
}
202210

203-
public static async createEmailPasswordless(config: SocialEmailPasswordlessConfigure) {
211+
public static async createEmailPasswordless(
212+
config: SocialEmailPasswordlessConfigure
213+
): Promise<GnoSocialWalletProvider> {
204214
const networkConfig: NetworkInfo = {
205215
chainId: config.chainId,
206216
addressPrefix: config.addressPrefix || GNO_ADDRESS_PREFIX,
@@ -221,12 +231,29 @@ export class GnoSocialWalletProvider extends GnoWalletProvider {
221231
}
222232
}
223233

234+
function isSocialGoogleConfigure(
235+
config: SocialGoogleConfigure | SocialTwitterConfigure | SocialEmailPasswordlessConfigure
236+
): config is SocialGoogleConfigure {
237+
if (config === null || typeof config !== 'object') {
238+
return false;
239+
}
240+
return 'authClientId' in config && 'googleClientId' in config && 'verifier' in config;
241+
}
242+
243+
function isSocialTwitterConfigure(
244+
config: SocialGoogleConfigure | SocialTwitterConfigure | SocialEmailPasswordlessConfigure
245+
): config is SocialTwitterConfigure {
246+
if (config === null || typeof config !== 'object') {
247+
return false;
248+
}
249+
return 'verifier' in config && 'domain' in config;
250+
}
251+
224252
function isSocialEmailPasswordlessConfigure(
225253
config: SocialGoogleConfigure | SocialTwitterConfigure | SocialEmailPasswordlessConfigure
226254
): config is SocialEmailPasswordlessConfigure {
227255
if (config === null || typeof config !== 'object') {
228256
return false;
229257
}
230-
231-
return 'email' in config;
258+
return 'email' in config && 'verifier' in config && 'domain' in config;
232259
}

0 commit comments

Comments
 (0)