@@ -1486,7 +1486,7 @@ impl<
14861486 threshold_fhe_keys,
14871487 & compressed_keyset,
14881488 & compact_pk,
1489- meta_store,
1489+ Arc :: clone ( & meta_store) ,
14901490 )
14911491 . await
14921492 {
@@ -1495,6 +1495,50 @@ impl<
14951495 ) ;
14961496 return ;
14971497 }
1498+
1499+ // If requested, copy the compressed key to the original key ID.
1500+ //
1501+ // Note: at this point the *new* keygen has already been committed
1502+ // and its meta_store entry is Done — that part of the request
1503+ // succeeded. The copy is a follow-up on a *different* key id
1504+ // (old_key_id), so a failure here does not invalidate the
1505+ // new_key_id material. We log loudly so operators can detect
1506+ // partial success and retry the migration, but we do not try
1507+ // to mark the new keygen itself as failed.
1508+ //
1509+ // TODO if copying fails, do we still call `update_backup_vault`?
1510+ if matches ! (
1511+ keyset_config. secret_key_config,
1512+ ddec_keyset_config:: KeyGenSecretKeyConfig :: UseExisting
1513+ ) && internal_keyset_config. copy_compressed_key_to_original ( )
1514+ {
1515+ let old_key_id = internal_keyset_config
1516+ . get_existing_keyset_id ( )
1517+ . expect ( "validated" ) ;
1518+ // UseExisting reads the old private shares at the current
1519+ // epoch_id (see key_gen_from_existing_private_keyset), so
1520+ // the copy targets the same (old_key_id, epoch_id) pair.
1521+ if let Err ( e) = crypto_storage
1522+ . copy_compressed_key_to_original (
1523+ req_id,
1524+ epoch_id,
1525+ & old_key_id,
1526+ epoch_id,
1527+ & sk,
1528+ & eip712_domain,
1529+ Arc :: clone ( & meta_store) ,
1530+ )
1531+ . await
1532+ {
1533+ tracing:: error!(
1534+ "Compressed keygen for {req_id} committed successfully, but the \
1535+ follow-up copy to original key id {old_key_id} failed: {e}. \
1536+ The new keys at {req_id} are valid; \
1537+ the migration to {old_key_id} must be retried."
1538+ ) ;
1539+ return ;
1540+ }
1541+ }
14981542 }
14991543 }
15001544 // Update the backup and handle potential failures by incrementing backup errors in the metrics
0 commit comments