Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 130 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"homepage": "https://github.com/Web3Auth/mpc-core-kit/tree/master#readme",
"license": "ISC",
"scripts": {
"test": "echo no tests available",
"test": "node --test -r esbuild-register tests/*.spec.ts ",
"dev": "torus-scripts start",
"build": "torus-scripts build",
"release": "torus-scripts release",
Expand Down Expand Up @@ -74,6 +74,7 @@
"esbuild-register": "^3.5.0",
"eslint": "^8.49.0",
"husky": "^8.0.3",
"jsonwebtoken": "^9.0.2",
"lint-staged": "^14.0.1",
"mocha": "^10.2.0",
"node-fetch": "^3.3.2",
Expand Down
37 changes: 31 additions & 6 deletions src/helper/browserStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ import { FIELD_ELEMENT_HEX_LEN } from "../constants";
import { ICoreKit, IStorage, TkeyLocalStoreData } from "../interfaces";
import { storageAvailable } from "../utils";

export type SupportedStorageType = "local" | "session" | "memory" | IStorage;

export class MemoryStorage implements IStorage {
private _store: Record<string, string> = {};

getItem(key: string): string | null {
return this._store[key] || null;
}

setItem(key: string, value: string): void {
this._store[key] = value;
}

removeItem(key: string): void {
delete this._store[key];
}

clear(): void {
this._store = {};
}
}

export class BrowserStorage {
// eslint-disable-next-line no-use-before-define
private static instance: BrowserStorage;
Expand All @@ -24,14 +46,17 @@ export class BrowserStorage {
}
}

static getInstance(key: string, storageKey: "session" | "local" = "local"): BrowserStorage {
static getInstance(key: string, storageKey: SupportedStorageType = "local"): BrowserStorage {
if (!this.instance) {
let storage: Storage | undefined;
let storage: IStorage | undefined;
if (storageKey === "local" && storageAvailable("localStorage")) {
storage = localStorage;
}
if (storageKey === "session" && storageAvailable("sessionStorage")) {
} else if (storageKey === "session" && storageAvailable("sessionStorage")) {
storage = sessionStorage;
} else if (storageKey === "memory") {
storage = new MemoryStorage();
} else if (typeof storageKey === "object") {
storage = storageKey;
}

if (!storage) {
Expand Down Expand Up @@ -76,7 +101,7 @@ export class BrowserStorage {
}
}

export async function storeWebBrowserFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: "local" | "session" = "local"): Promise<void> {
export async function storeWebBrowserFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: SupportedStorageType = "local"): Promise<void> {
const metadata = mpcCoreKit.tKey.getMetadata();
const currentStorage = BrowserStorage.getInstance("mpc_corekit_store", storageKey);

Expand All @@ -89,7 +114,7 @@ export async function storeWebBrowserFactor(factorKey: BN, mpcCoreKit: ICoreKit,
);
}

export async function getWebBrowserFactor(mpcCoreKit: ICoreKit, storageKey: "local" | "session" = "local"): Promise<string | undefined> {
export async function getWebBrowserFactor(mpcCoreKit: ICoreKit, storageKey: SupportedStorageType = "local"): Promise<string | undefined> {
const metadata = mpcCoreKit.tKey.getMetadata();
const currentStorage = BrowserStorage.getInstance("mpc_corekit_store", storageKey);

Expand Down
6 changes: 4 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { CustomChainConfig, SafeEventEmitterProvider } from "@web3auth/base";
import BN from "bn.js";

import { FactorKeyTypeShareDescription, TssShareType, USER_PATH, WEB3AUTH_NETWORK } from "./constants";

export type CoreKitMode = UX_MODE_TYPE | "nodejs";
export interface IStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
Expand Down Expand Up @@ -277,7 +279,7 @@ export interface Web3AuthOptions {
*
* @defaultValue `'local'`
*/
storageKey?: "session" | "local";
storageKey?: "session" | "local" | "memory" | IStorage;

/**
* @defaultValue 86400
Expand All @@ -287,7 +289,7 @@ export interface Web3AuthOptions {
/**
* @defaultValue `'POPUP'`
*/
uxMode?: UX_MODE_TYPE;
uxMode?: CoreKitMode;

/**
* @defaultValue `false`
Expand Down
Loading