Skip to content

Commit 392a83f

Browse files
use platform
1 parent 7ab751f commit 392a83f

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

src/Web3Auth.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from "@toruslabs/openlogin-utils";
1313
import log from "loglevel";
1414

15-
import { InitializationError, LoginError } from "./errors";
15+
import { InitializationError, LoginError, RequestError } from "./errors";
1616
import KeyStore from "./session/KeyStore";
1717
import { EncryptedStorage } from "./types/IEncryptedStorage";
1818
import { SecureStore } from "./types/IExpoSecureStore";
@@ -284,7 +284,7 @@ class Web3Auth implements IWeb3Auth {
284284
const configParams: WalletLoginParams = {
285285
loginId,
286286
sessionId,
287-
isReactNative: true,
287+
platform: "react-native",
288288
};
289289

290290
const loginUrl = constructURL({
@@ -295,6 +295,52 @@ class Web3Auth implements IWeb3Auth {
295295
this.webBrowser.openAuthSessionAsync(loginUrl, dataObject.params.redirectUrl);
296296
}
297297

298+
async request(chainConfig: ChainConfig, method: string, params: unknown[], path: string | null = "wallet/request"): Promise<string> {
299+
if (!this.ready) throw InitializationError.notInitialized("Please call init first.");
300+
if (!this.sessionManager.sessionId) {
301+
throw LoginError.userNotLoggedIn();
302+
}
303+
304+
const dataObject: Omit<OpenloginSessionConfig, "options"> & { options: SdkInitParams & { chainConfig: ChainConfig } } = {
305+
actionType: OPENLOGIN_ACTIONS.LOGIN,
306+
options: { ...this.options, chainConfig },
307+
params: {},
308+
};
309+
310+
const url = `${this.walletSdkUrl}/${path}`;
311+
const loginId = OpenloginSessionManager.generateRandomSessionKey();
312+
await this.createLoginSession(loginId, dataObject);
313+
314+
const { sessionId } = this.sessionManager;
315+
const configParams: WalletLoginParams = {
316+
loginId,
317+
sessionId,
318+
request: {
319+
method,
320+
params,
321+
},
322+
platform: "react-native",
323+
};
324+
325+
const loginUrl = constructURL({
326+
baseURL: url,
327+
hash: { b64Params: jsonToBase64(configParams) },
328+
});
329+
330+
const result = await this.webBrowser.openAuthSessionAsync(loginUrl, dataObject.params.redirectUrl);
331+
332+
if (result.type !== "success" || !result.url) {
333+
log.error(`[Web3Auth] login flow failed with error type ${result.type}`);
334+
throw LoginError.loginFailed(`login flow failed with error type ${result.type}`);
335+
}
336+
337+
const { success, result: requestResult, error } = getHashQueryParams(result.url);
338+
if (error || success === "false") {
339+
throw RequestError.fromCode(5000, error);
340+
}
341+
return requestResult;
342+
}
343+
298344
async enableMFA(): Promise<boolean> {
299345
if (!this.ready) throw InitializationError.notInitialized("Please call init first.");
300346
if (!this.sessionManager.sessionId) {

src/errors.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,21 @@ export class LoginError extends Web3authRNError {
118118
return LoginError.fromCode(5117, extraMessage);
119119
}
120120
}
121+
122+
export class RequestError extends Web3authRNError {
123+
protected static messages: ErrorCodes = {
124+
5000: "Custom",
125+
};
126+
127+
public constructor(code: number, message?: string) {
128+
// takes care of stack and proto
129+
super(code, message);
130+
131+
// Set name explicitly as minification can mangle class names
132+
Object.defineProperty(this, "name", { value: "LoginError" });
133+
}
134+
135+
public static fromCode(code: number, extraMessage = ""): Web3authRNError {
136+
return new RequestError(code, `${RequestError.messages[code]}, ${extraMessage}`);
137+
}
138+
}

src/types/interface.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ export interface IWeb3Auth {
6161
export type WalletLoginParams = {
6262
loginId?: string;
6363
sessionId?: string;
64-
isReactNative: boolean;
64+
request?: {
65+
method: string;
66+
params: unknown[];
67+
};
68+
platform: string;
6569
};
6670

6771
export enum ChainNamespace {

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export type HashQueryParamResult = {
2323
sessionNamespace?: string;
2424
error?: string;
2525
state?: string;
26+
// Used only for request method
27+
success?: string;
28+
result?: string;
2629
};
2730

2831
export function getHashQueryParams(url: string): HashQueryParamResult {

0 commit comments

Comments
 (0)