|
1 | 1 | import { BNString, KeyType, Point, secp256k1, SHARE_DELETED, ShareStore, StringifiedType } from "@tkey/common-types"; |
2 | 2 | import { CoreError } from "@tkey/core"; |
3 | | -import { ShareSerializationModule } from "@tkey/share-serialization"; |
4 | 3 | import { TorusStorageLayer } from "@tkey/storage-layer-torus"; |
5 | 4 | import { factorKeyCurve, getPubKeyPoint, lagrangeInterpolation, TKeyTSS, TSSTorusServiceProvider } from "@tkey/tss"; |
6 | 5 | import { KEY_TYPE, SIGNER_MAP } from "@toruslabs/constants"; |
@@ -111,7 +110,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit { |
111 | 110 | if (options.enableLogging) { |
112 | 111 | log.enableAll(); |
113 | 112 | this.enableLogging = true; |
114 | | - } else log.setLevel("error"); |
| 113 | + } else { |
| 114 | + log.setLevel("error"); |
| 115 | + options.enableLogging = false; |
| 116 | + } |
115 | 117 | if (typeof options.manualSync !== "boolean") options.manualSync = false; |
116 | 118 | if (!options.web3AuthNetwork) options.web3AuthNetwork = WEB3AUTH_NETWORK.MAINNET; |
117 | 119 | // if sessionTime is not provided, it is defaulted to 86400 |
@@ -196,6 +198,57 @@ export class Web3AuthMPCCoreKit implements ICoreKit { |
196 | 198 | return this.keyType === KeyType.ed25519 && this.options.useClientGeneratedTSSKey === undefined ? true : !!this.options.useClientGeneratedTSSKey; |
197 | 199 | } |
198 | 200 |
|
| 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 | + |
199 | 252 | // RecoverTssKey only valid for user that enable MFA where user has 2 type shares : |
200 | 253 | // TssShareType.DEVICE and TssShareType.RECOVERY |
201 | 254 | // if the factors key provided is the same type recovery will not works |
@@ -255,16 +308,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit { |
255 | 308 | enableLogging: this.enableLogging, |
256 | 309 | }); |
257 | 310 |
|
258 | | - const shareSerializationModule = new ShareSerializationModule(); |
259 | | - |
260 | 311 | this.tkey = new TKeyTSS({ |
261 | 312 | enableLogging: this.enableLogging, |
262 | 313 | serviceProvider: this.torusSp, |
263 | 314 | storageLayer: this.storageLayer, |
264 | 315 | manualSync: this.options.manualSync, |
265 | | - modules: { |
266 | | - shareSerialization: shareSerializationModule, |
267 | | - }, |
268 | 316 | tssKeyType: this.keyType, |
269 | 317 | }); |
270 | 318 |
|
|
0 commit comments