@@ -283,6 +283,47 @@ describe('DKLS Dkg 2x3', function () {
283283 assert . deepEqual ( DklsTypes . getCommonKeychain ( backupKeyShare ) , DklsTypes . getCommonKeychain ( bitgoKeyShare ) ) ;
284284 } ) ;
285285
286+ it ( 'restoreSession() should ignore tampered dkgState and re-derive from WASM bytes' , async function ( ) {
287+ const user = new DklsDkg . Dkg ( 3 , 2 , 0 ) ;
288+
289+ // After initDkg() the WASM session encodes WaitMsg1 → DkgState.Round1
290+ await user . initDkg ( ) ;
291+
292+ const legitimateSessionData = user . getSessionData ( ) ;
293+
294+ // Tamper: claim the session is at Round4 when WASM bytes still say Round1
295+ const tamperedSessionData = {
296+ ...legitimateSessionData ,
297+ dkgState : DklsTypes . DkgState . Round4 ,
298+ } ;
299+
300+ const restoredUser = await DklsDkg . Dkg . restoreSession ( 3 , 2 , 0 , tamperedSessionData ) ;
301+
302+ // Must reflect the actual WASM state (Round1), not the tampered Round4
303+ assert . strictEqual (
304+ restoredUser [ 'dkgState' ] ,
305+ DklsTypes . DkgState . Round1 ,
306+ 'restoreSession() must re-derive dkgState from WASM bytes and ignore caller-supplied value'
307+ ) ;
308+ } ) ;
309+
310+ it ( 'restoreSession() should restore a completed DKG session as DkgState.Complete' , async function ( ) {
311+ const [ user ] = await generateDKGKeyShares ( ) ;
312+ const completedSessionData = user . getSessionData ( ) ;
313+
314+ // dkgSessionBytes holds { round: 'Ended' }; restoreSession() must decode it as Complete
315+ // without reconstructing the (already freed) WASM session
316+ const restoredUser = await DklsDkg . Dkg . restoreSession ( 3 , 2 , 0 , completedSessionData ) ;
317+
318+ assert . strictEqual (
319+ restoredUser [ 'dkgState' ] ,
320+ DklsTypes . DkgState . Complete ,
321+ 'restoreSession() must decode "Ended" round marker as DkgState.Complete'
322+ ) ;
323+ // Key share must still be accessible on the restored instance
324+ assert . ok ( restoredUser . getKeyShare ( ) , 'Key share should be accessible after restoring completed session' ) ;
325+ } ) ;
326+
286327 it ( 'should successfully finish DKG using restored sessions' , async function ( ) {
287328 const user = new DklsDkg . Dkg ( 3 , 2 , 0 ) ;
288329 const backup = new DklsDkg . Dkg ( 3 , 2 , 1 ) ;
0 commit comments