Skip to content

Commit 776a758

Browse files
author
himanshu
committed
Merge remote-tracking branch 'origin' into feat/importRecoverTssKeyfix example
2 parents 33569b8 + d88f49b commit 776a758

File tree

10 files changed

+310
-36
lines changed

10 files changed

+310
-36
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"eslint.format.enable": true,
77
"eslint.lintTask.enable": true,
88
"editor.codeActionsOnSave": {
9-
"source.fixAll": true
9+
"source.fixAll": "explicit"
1010
}
1111
}

demo/firebase-idtoken-flow-example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@types/node": "^16.18.48",
1111
"@types/react": "^18.2.21",
1212
"@types/react-dom": "^18.2.7",
13+
"@web3auth/mpc-core-kit": "file://../../",
1314
"browserify-zlib": "^0.2.0",
1415
"copy-webpack-plugin": "^11.0.0",
1516
"firebase": "^10.1.0",

package-lock.json

Lines changed: 138 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3auth/mpc-core-kit",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "MPC CoreKit SDK for web3Auth",
55
"keywords": [
66
"web3Auth/mpc-core-kit",
@@ -50,7 +50,7 @@
5050
"@toruslabs/metadata-helpers": "^5.x",
5151
"@toruslabs/openlogin-session-manager": "^3.0.0",
5252
"@toruslabs/torus.js": "^11.0.6",
53-
"@toruslabs/tss-client": "^2.0.0",
53+
"@toruslabs/tss-client": "^2.1.0",
5454
"@toruslabs/tss-lib": "^2.0.0",
5555
"@web3auth-mpc/ethereum-provider": "^2.3.0",
5656
"@web3auth/base": "^7.0.1",
@@ -67,6 +67,7 @@
6767
"@toruslabs/tss-lib-node": "^1.1.3",
6868
"@types/chai": "^4.3.6",
6969
"@types/elliptic": "^6.4.14",
70+
"@types/jsonwebtoken": "^9.0.5",
7071
"@types/node": "^20.6.3",
7172
"@typescript-eslint/eslint-plugin": "^6.7.0",
7273
"chai": "^4.3.8",
@@ -75,6 +76,7 @@
7576
"esbuild-register": "^3.5.0",
7677
"eslint": "^8.49.0",
7778
"husky": "^8.0.3",
79+
"jsonwebtoken": "^9.0.2",
7880
"lint-staged": "^14.0.1",
7981
"mocha": "^10.2.0",
8082
"node-fetch": "^3.3.2",

src/helper/browserStorage.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import BN from "bn.js";
22

33
import { FIELD_ELEMENT_HEX_LEN } from "../constants";
4-
import { ICoreKit, IStorage, SupportedStorageType, TkeyLocalStoreData } from "../interfaces";
4+
import { IAsyncStorage, ICoreKit, IStorage, SupportedStorageType, TkeyLocalStoreData } from "../interfaces";
55
import { storageAvailable } from "../utils";
66

77
export class MemoryStorage implements IStorage {
@@ -99,6 +99,87 @@ export class BrowserStorage {
9999
}
100100
}
101101

102+
export class AsyncStorage {
103+
// eslint-disable-next-line no-use-before-define
104+
private static instance: AsyncStorage;
105+
106+
public storage: IAsyncStorage;
107+
108+
private _storeKey: string;
109+
110+
private constructor(storeKey: string, storage: IAsyncStorage) {
111+
this.storage = storage;
112+
this._storeKey = storeKey;
113+
}
114+
115+
static getInstance(key: string, storageKey: IAsyncStorage): AsyncStorage {
116+
if (!this.instance) {
117+
const storage: IAsyncStorage = storageKey;
118+
if (!storage) {
119+
throw new Error("No valid storage available");
120+
}
121+
this.instance = new this(key, storage);
122+
}
123+
return this.instance;
124+
}
125+
126+
async toJSON(): Promise<string> {
127+
const result = await this.storage.getItem(this._storeKey);
128+
if (!result) throw new Error(`storage ${this._storeKey} is null`);
129+
return result;
130+
}
131+
132+
async resetStore(): Promise<Record<string, unknown>> {
133+
const currStore = await this.getStore();
134+
await this.storage.setItem(this._storeKey, JSON.stringify({}));
135+
return currStore;
136+
}
137+
138+
async getStore(): Promise<Record<string, unknown>> {
139+
return JSON.parse((await this.storage.getItem(this._storeKey)) || "{}");
140+
}
141+
142+
async get<T>(key: string): Promise<T> {
143+
const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}");
144+
return store[key];
145+
}
146+
147+
async set<T>(key: string, value: T): Promise<void> {
148+
const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}");
149+
store[key] = value;
150+
await this.storage.setItem(this._storeKey, JSON.stringify(store));
151+
}
152+
153+
async remove(key: string): Promise<void> {
154+
const store = JSON.parse((await this.storage.getItem(this._storeKey)) || "{}");
155+
delete store[key];
156+
await this.storage.setItem(this._storeKey, JSON.stringify(store));
157+
}
158+
}
159+
160+
export async function asyncStoreFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: IAsyncStorage): Promise<void> {
161+
const metadata = mpcCoreKit.tKey.getMetadata();
162+
const currentStorage = AsyncStorage.getInstance("mpc_corekit_store", storageKey);
163+
164+
const tkeyPubX = metadata.pubKey.x.toString(16, FIELD_ELEMENT_HEX_LEN);
165+
await currentStorage.set(
166+
tkeyPubX,
167+
JSON.stringify({
168+
factorKey: factorKey.toString("hex").padStart(64, "0"),
169+
} as TkeyLocalStoreData)
170+
);
171+
}
172+
173+
export async function asyncGetFactor(mpcCoreKit: ICoreKit, storageKey: IAsyncStorage): Promise<string | undefined> {
174+
const metadata = mpcCoreKit.tKey.getMetadata();
175+
const currentStorage = AsyncStorage.getInstance("mpc_corekit_store", storageKey);
176+
177+
const tkeyPubX = metadata.pubKey.x.toString(16, FIELD_ELEMENT_HEX_LEN);
178+
const tKeyLocalStoreString = await currentStorage.get<string>(tkeyPubX);
179+
const tKeyLocalStore = JSON.parse(tKeyLocalStoreString || "{}") as TkeyLocalStoreData;
180+
return tKeyLocalStore.factorKey;
181+
}
182+
102183
export async function storeWebBrowserFactor(factorKey: BN, mpcCoreKit: ICoreKit, storageKey: SupportedStorageType = "local"): Promise<void> {
103184
const metadata = mpcCoreKit.tKey.getMetadata();
104185
const currentStorage = BrowserStorage.getInstance("mpc_corekit_store", storageKey);

0 commit comments

Comments
 (0)