Skip to content

Commit 9f759da

Browse files
committed
feat: toJson, fromJson
1 parent d82f4c1 commit 9f759da

File tree

4 files changed

+99
-9
lines changed

4 files changed

+99
-9
lines changed

src/mpcCoreKit.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BNString, KeyType, Point, secp256k1, SHARE_DELETED, ShareStore, StringifiedType } from "@tkey/common-types";
22
import { CoreError } from "@tkey/core";
3-
import { ShareSerializationModule } from "@tkey/share-serialization";
43
import { TorusStorageLayer } from "@tkey/storage-layer-torus";
54
import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss";
65
import { KEY_TYPE, SIGNER_MAP } from "@toruslabs/constants";
@@ -111,7 +110,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
111110
if (options.enableLogging) {
112111
log.enableAll();
113112
this.enableLogging = true;
114-
} else log.setLevel("error");
113+
} else {
114+
log.setLevel("error");
115+
options.enableLogging = false;
116+
}
115117
if (typeof options.manualSync !== "boolean") options.manualSync = false;
116118
if (!options.web3AuthNetwork) options.web3AuthNetwork = WEB3AUTH_NETWORK.MAINNET;
117119
// if sessionTime is not provided, it is defaulted to 86400
@@ -196,6 +198,57 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
196198
return this.keyType === KeyType.ed25519 && this.options.useClientGeneratedTSSKey === undefined ? true : !!this.options.useClientGeneratedTSSKey;
197199
}
198200

201+
static async fromJSON(value: StringifiedType, options: Web3AuthOptions): Promise<Web3AuthMPCCoreKit> {
202+
const coreKit = new Web3AuthMPCCoreKit(options);
203+
const { state, serviceProvider, storageLayer, keyType, atomicCallStackCounter, ready, sessionId, tkey } = value;
204+
coreKit.torusSp = TSSTorusServiceProvider.fromJSON(serviceProvider);
205+
coreKit.storageLayer = TorusStorageLayer.fromJSON(storageLayer);
206+
207+
coreKit.tkey = await TKeyTSS.fromJSON(tkey, {
208+
serviceProvider: coreKit.torusSp,
209+
storageLayer: coreKit.storageLayer,
210+
enableLogging: options.enableLogging,
211+
tssKeyType: options.tssLib.keyType as KeyType,
212+
});
213+
await coreKit.tkey.reconstructKey();
214+
215+
if (state.factorKey) state.factorKey = new BN(state.factorKey, "hex");
216+
if (state.tssPubKey) state.tssPubKey = Buffer.from(state.tssPubKey, "hex");
217+
coreKit.state = state;
218+
coreKit.sessionManager.sessionId = sessionId;
219+
coreKit.atomicCallStackCounter = atomicCallStackCounter;
220+
coreKit.ready = ready;
221+
222+
if (coreKit._keyType !== keyType) {
223+
console.log("keyType mismatch", coreKit._keyType, keyType);
224+
throw CoreKitError.invalidConfig();
225+
}
226+
227+
// will be derived from option during constructor
228+
// sessionManager
229+
// enableLogging
230+
// private _tssLib: TssLibType;
231+
// private wasmLib: DKLSWasmLib | FrostWasmLib;
232+
// private _keyType: KeyType;
233+
return coreKit;
234+
}
235+
236+
public toJSON(): StringifiedType {
237+
const factorKey = this.state.factorKey ? this.state.factorKey.toString("hex") : undefined;
238+
const tssPubKey = this.state.tssPubKey ? this.state.tssPubKey.toString("hex") : undefined;
239+
return {
240+
state: { ...this.state, factorKey, tssPubKey },
241+
options: this.options,
242+
serviceProvider: this.torusSp.toJSON(),
243+
storageLayer: this.storageLayer.toJSON(),
244+
tkey: this.tKey.toJSON(),
245+
keyType: this.keyType,
246+
sessionId: this.sessionManager.sessionId,
247+
atomicCallStackCounter: this.atomicCallStackCounter,
248+
ready: this.ready,
249+
};
250+
}
251+
199252
// RecoverTssKey only valid for user that enable MFA where user has 2 type shares :
200253
// TssShareType.DEVICE and TssShareType.RECOVERY
201254
// if the factors key provided is the same type recovery will not works
@@ -255,16 +308,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
255308
enableLogging: this.enableLogging,
256309
});
257310

258-
const shareSerializationModule = new ShareSerializationModule();
259-
260311
this.tkey = new TKeyTSS({
261312
enableLogging: this.enableLogging,
262313
serviceProvider: this.torusSp,
263314
storageLayer: this.storageLayer,
264315
manualSync: this.options.manualSync,
265-
modules: {
266-
shareSerialization: shareSerializationModule,
267-
},
268316
tssKeyType: this.keyType,
269317
});
270318

tests/ed25519.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ variable.forEach((testVariable) => {
144144
const msgBuffer = Buffer.from(msg);
145145

146146
const signature = ed25519().makeSignature((await coreKitInstance.sign(msgBuffer)).toString("hex"));
147+
msgBuffer.slice(1,2);
147148
const valid = ed25519().verify(msgBuffer, signature, coreKitInstance.getPubKeyEd25519());
148149
assert(valid);
149150
});

tests/serialize.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, it } from "node:test";
2+
import { defaultTestOptions, newCoreKitLogInInstance } from "./setup";
3+
import { AsyncStorage, MemoryStorage, WEB3AUTH_NETWORK, Web3AuthMPCCoreKit } from "src";
4+
import { deepEqual, deepStrictEqual, equal, strictEqual } from "node:assert";
5+
6+
7+
describe('serialize deserialze mpc corekit', () => {
8+
it('serialize', async () => {
9+
const localMemoryStorage = new MemoryStorage();
10+
const instance = await newCoreKitLogInInstance({
11+
network: WEB3AUTH_NETWORK.DEVNET,
12+
manualSync: true,
13+
14+
storageInstance: localMemoryStorage,
15+
})
16+
const options = defaultTestOptions({ network: WEB3AUTH_NETWORK.DEVNET, manualSync: true, storageInstance: localMemoryStorage });
17+
const serialized = JSON.stringify(instance);
18+
console.log(serialized)
19+
const deserialized = await Web3AuthMPCCoreKit.fromJSON(JSON.parse(serialized), options);
20+
21+
22+
23+
});
24+
});

tests/setup.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import BN from "bn.js";
44
import jwt, { Algorithm } from "jsonwebtoken";
55
import { tssLib as tssLibDKLS } from "@toruslabs/tss-dkls-lib";
66

7-
import { IAsyncStorage, IStorage, parseToken, TssLib, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src";
7+
import { IAsyncStorage, IStorage, parseToken, TssLibType, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, Web3AuthOptions } from "../src";
88

99
export const mockLogin2 = async (email: string) => {
1010
const req = new Request("https://li6lnimoyrwgn2iuqtgdwlrwvq0upwtr.lambda-url.eu-west-1.on.aws/", {
@@ -84,6 +84,23 @@ export const mockLogin = async (email?: string) => {
8484
};
8585

8686
export type LoginFunc = (email: string) => Promise<{ idToken: string, parsedToken: any }>;
87+
export const defaultTestOptions = (params: {
88+
network: WEB3AUTH_NETWORK_TYPE;
89+
manualSync: boolean;
90+
storageInstance: IStorage | IAsyncStorage;
91+
tssLib?: TssLibType;
92+
}) : Web3AuthOptions => {
93+
const { network, manualSync, storageInstance, tssLib } = params;
94+
return {
95+
web3AuthClientId: "torus-key-test",
96+
web3AuthNetwork: network,
97+
baseUrl: "http://localhost:3000",
98+
uxMode: "nodejs",
99+
tssLib: tssLib || tssLibDKLS,
100+
storage: storageInstance,
101+
manualSync,
102+
}
103+
}
87104

88105
export const newCoreKitLogInInstance = async ({
89106
network,
@@ -97,7 +114,7 @@ export const newCoreKitLogInInstance = async ({
97114
manualSync: boolean;
98115
email: string;
99116
storageInstance: IStorage | IAsyncStorage;
100-
tssLib?: TssLib;
117+
tssLib?: TssLibType;
101118
importTssKey?: string;
102119
login?: LoginFunc;
103120
}) => {

0 commit comments

Comments
 (0)