@@ -7,6 +7,7 @@ use crate::wallet::persister::WalletPersister;
77use dpp:: identity:: accessors:: IdentityGettersV0 ;
88use dpp:: identity:: identity_public_key:: accessors:: v0:: IdentityPublicKeyGettersV0 ;
99use dpp:: identity:: Identity ;
10+ use dpp:: identity:: { KeyID , TimestampMillis } ;
1011use dpp:: prelude:: Identifier ;
1112use dpp:: util:: hash:: ripemd160_sha256;
1213use std:: collections:: BTreeMap ;
@@ -294,4 +295,109 @@ impl ManagedIdentity {
294295 tracing:: error!( "Failed to persist changeset: {}" , e) ;
295296 }
296297 }
298+
299+ /// Stamp `disabled_at` on the public keys named by `key_ids` and
300+ /// emit a single-key [`IdentityKeysChangeSet`] upsert per affected
301+ /// key — the disable-side counterpart to [`Self::add_key`].
302+ ///
303+ /// Mirrors `add_key`'s persistence shape: the matching
304+ /// `IdentityPublicKey` records are mutated in place on the DPP
305+ /// `Identity` (so every signing / introspection path sees the
306+ /// disabled flag immediately) and a combined changeset is persisted
307+ /// so the client's `PersistentPublicKey.disabledAt` rows flip
308+ /// without a network re-fetch. Each emitted entry reuses the same
309+ /// `(wallet_id, identity_index, key_index)` derivation breadcrumb
310+ /// `add_key` carries, so the client re-derives idempotently and
311+ /// keeps the key's existing private-key linkage rather than
312+ /// dropping it on the upsert. Out-of-wallet identities (no
313+ /// derivation context) emit a breadcrumb-less entry, matching
314+ /// their watch-only state.
315+ ///
316+ /// `key_ids` not present on the identity are skipped (logged) and
317+ /// no changeset is emitted when nothing matched. `disabled_at` is
318+ /// the timestamp stamped on every matching key — callers pass the
319+ /// local broadcast time; the next Platform refresh reconciles it to
320+ /// the authoritative on-chain block time.
321+ ///
322+ /// Does **not** touch the identity revision — the caller bumps it
323+ /// alongside the update transition.
324+ pub fn disable_keys (
325+ & mut self ,
326+ key_ids : & [ KeyID ] ,
327+ disabled_at : TimestampMillis ,
328+ persister : & WalletPersister ,
329+ ) {
330+ use dpp:: identity:: accessors:: IdentitySettersV0 ;
331+ use dpp:: identity:: identity_public_key:: accessors:: v0:: IdentityPublicKeySettersV0 ;
332+
333+ let identity_id = self . id ( ) ;
334+
335+ // Reconstruct the derivation breadcrumb from the cached
336+ // wallet/identity slot. Wallet-owned identities carry both
337+ // halves; out-of-wallet ones have neither (and no private key
338+ // to preserve), so they fall through to a watch-only entry.
339+ let breadcrumb = match ( self . wallet_id , self . identity_index ) {
340+ ( Some ( wallet_id) , Some ( identity_index) ) => Some ( ( wallet_id, identity_index) ) ,
341+ _ => None ,
342+ } ;
343+
344+ // Operate on a clone of the key map, mirroring `add_key`, then
345+ // write it back once all matching keys have been stamped.
346+ let mut keys = self . identity . public_keys ( ) . clone ( ) ;
347+ let mut keys_cs = IdentityKeysChangeSet :: default ( ) ;
348+
349+ for & key_id in key_ids {
350+ let Some ( public_key) = keys. get_mut ( & key_id) else {
351+ tracing:: warn!(
352+ identity = %identity_id,
353+ key_id,
354+ "disable_keys: key id not present on identity; skipping" ,
355+ ) ;
356+ continue ;
357+ } ;
358+
359+ public_key. set_disabled_at ( disabled_at) ;
360+ let public_key_hash = pubkey_hash_of ( public_key) ;
361+
362+ let ( wallet_id, derivation_indices) = match breadcrumb {
363+ Some ( ( wallet_id, identity_index) ) => (
364+ Some ( wallet_id) ,
365+ Some ( crate :: changeset:: IdentityKeyDerivationIndices {
366+ identity_index,
367+ key_index : key_id,
368+ } ) ,
369+ ) ,
370+ None => ( None , None ) ,
371+ } ;
372+
373+ keys_cs. upserts . insert (
374+ ( identity_id, key_id) ,
375+ IdentityKeyEntry {
376+ identity_id,
377+ key_id,
378+ public_key : public_key. clone ( ) ,
379+ public_key_hash,
380+ wallet_id,
381+ derivation_indices,
382+ } ,
383+ ) ;
384+ }
385+
386+ // Nothing matched — leave state untouched rather than churning a
387+ // no-op changeset.
388+ if keys_cs. upserts . is_empty ( ) {
389+ return ;
390+ }
391+
392+ self . identity . set_public_keys ( keys) ;
393+
394+ let cs = crate :: changeset:: PlatformWalletChangeSet {
395+ identities : Some ( self . snapshot_changeset ( ) ) ,
396+ identity_keys : Some ( keys_cs) ,
397+ ..Default :: default ( )
398+ } ;
399+ if let Err ( e) = persister. store ( cs) {
400+ tracing:: error!( "Failed to persist changeset: {}" , e) ;
401+ }
402+ }
297403}
0 commit comments