Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: internal documentation #66

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
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
36 changes: 30 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" | "mock";

export class MockStorage 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 @@ -12,7 +34,7 @@ export class BrowserStorage {

private _storeKey: string;

private constructor(storeKey: string, storage: IStorage) {
constructor(storeKey: string, storage: IStorage) {
this.storage = storage;
this._storeKey = storeKey;
try {
Expand All @@ -24,16 +46,18 @@ 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")) {
storage = sessionStorage;
}

if (storageKey === "mock") {
storage = new MockStorage();
}
if (!storage) {
throw new Error("No valid storage available");
}
Expand Down Expand Up @@ -76,7 +100,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 +113,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
20 changes: 13 additions & 7 deletions src/helper/securityQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { keccak256 } from "@toruslabs/torus.js";
import BN from "bn.js";

import { FactorKeyTypeShareDescription, TssShareType, VALID_SHARE_INDICES } from "../constants";
import type { Web3AuthMPCCoreKit } from "../mpcCoreKit";
import { ICoreKit } from "../interfaces";
// import type { Web3AuthMPCCoreKit } from "../mpcCoreKit";
import { Point } from "../point";

export class TssSecurityQuestionStore {
Expand Down Expand Up @@ -34,7 +35,7 @@ export class TssSecurityQuestionStore {
}

export interface setSecurityQuestionParams {
mpcCoreKit: Web3AuthMPCCoreKit;
mpcCoreKit: ICoreKit;
question: string;
answer: string;
shareType?: TssShareType;
Expand All @@ -43,12 +44,16 @@ export interface setSecurityQuestionParams {
}

export interface changeSecurityQuestionParams {
mpcCoreKit: Web3AuthMPCCoreKit;
mpcCoreKit: ICoreKit;
newQuestion: string;
newAnswer: string;
answer: string;
}

// Idea using hash of answer + pubKey as security question factor key factor key
// opposed to use the hash to encrypt random factor key as both are equally secure
// but this way we can use the hash to recover the factor key and reduce the api call and storage

export class TssSecurityQuestion {
storeDomainName = "tssSecurityQuestion";

Expand Down Expand Up @@ -160,8 +165,9 @@ export class TssSecurityQuestion {
if (!tkey.manualSync) await tkey._syncShareMetadata();
}

// Should we check with answer before deleting?
async deleteSecurityQuestion(mpcCoreKit: Web3AuthMPCCoreKit, deleteFactorKey = true) {
// provide option to delete security question incase user forgot the password
// option to not delete the factor key if user only want to remove the question but not the factor key
async deleteSecurityQuestion(mpcCoreKit: ICoreKit, deleteFactorKey = true) {
if (!mpcCoreKit.tKey) {
throw new Error("Tkey not initialized, call init first.");
}
Expand All @@ -184,7 +190,7 @@ export class TssSecurityQuestion {
if (!tkey.manualSync) await tkey._syncShareMetadata();
}

async recoverFactor(mpcCoreKit: Web3AuthMPCCoreKit, answer: string): Promise<string> {
async recoverFactor(mpcCoreKit: ICoreKit, answer: string): Promise<string> {
if (!mpcCoreKit.tKey) {
throw new Error("Tkey not initialized, call init first.");
}
Expand Down Expand Up @@ -217,7 +223,7 @@ export class TssSecurityQuestion {
return hash;
}

getQuestion(mpcCoreKit: Web3AuthMPCCoreKit): string {
getQuestion(mpcCoreKit: ICoreKit): string {
if (!mpcCoreKit.tKey) {
throw new Error("Tkey not initialized, call init first.");
}
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
UX_MODE_TYPE,
WebAuthnExtraParams,
} from "@toruslabs/customauth";
import { BNString } from "@toruslabs/torus.js";
import { CustomChainConfig, SafeEventEmitterProvider } from "@web3auth/base";
import BN from "bn.js";

Expand Down Expand Up @@ -214,7 +215,7 @@ export interface ICoreKit {
* associated metadata.
* @param factorPub - The public key of the factor to delete.
*/
deleteFactor(factorPub: TkeyPoint): Promise<void>;
deleteFactor(factorPub: TkeyPoint, factorKey?: BNString): Promise<void>;

/**
* Logs out the user, terminating the session.
Expand Down Expand Up @@ -277,7 +278,7 @@ export interface Web3AuthOptions {
*
* @defaultValue `'local'`
*/
storageKey?: "session" | "local";
storageKey?: "session" | "local" | "mock";

/**
* @defaultValue 86400
Expand Down
Loading