Skip to content

Commit fe79734

Browse files
authored
Merge branch 'v3.1-dev' into fix/dpns-case-insensitive-dash-v3.1-dev
2 parents 11666c6 + 85eba06 commit fe79734

3 files changed

Lines changed: 172 additions & 14 deletions

File tree

‎packages/rs-platform-wallet/src/wallet/identity/network/update.rs‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,24 @@ impl IdentityWallet {
233233
}
234234

235235
if !disabled_ids_for_local_apply.is_empty() {
236-
// TODO(disable-keys): wire a
237-
// `ManagedIdentity::disable_keys` counterpart to
238-
// `add_key` so the disable side of an update
239-
// transition stamps a `disabled_at` on the
240-
// matching `IdentityKeyEntry` rows. For now we
241-
// log + leave the cache stale on the disable
242-
// path; callers that need it should refresh
243-
// from Platform after the broadcast.
244-
tracing::warn!(
245-
identity = %identity_id,
246-
disabled_count = disabled_ids_for_local_apply.len(),
247-
"Disable-keys post-broadcast apply not yet implemented; \
248-
in-memory cache will not reflect disabled key flags \
249-
until the next refresh"
236+
// Disable-side counterpart to the `add_key` loop
237+
// above: stamp `disabled_at` on the matching cached
238+
// keys and fire the persister so the Swift
239+
// `PersistentPublicKey.disabledAt` rows flip without
240+
// a network re-fetch. The local wall-clock timestamp
241+
// is a placeholder — the next Platform refresh
242+
// reconciles it to the authoritative on-chain block
243+
// time. `disable_keys` reuses the same derivation
244+
// breadcrumb `add_key` carries, so the disabled key
245+
// keeps its private-key linkage.
246+
let disabled_at = std::time::SystemTime::now()
247+
.duration_since(std::time::UNIX_EPOCH)
248+
.map(|d| d.as_millis() as u64)
249+
.unwrap_or_default();
250+
managed.disable_keys(
251+
&disabled_ids_for_local_apply,
252+
disabled_at,
253+
&self.persister,
250254
);
251255
}
252256
}

‎packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/identity_ops.rs‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::wallet::persister::WalletPersister;
77
use dpp::identity::accessors::IdentityGettersV0;
88
use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
99
use dpp::identity::Identity;
10+
use dpp::identity::{KeyID, TimestampMillis};
1011
use dpp::prelude::Identifier;
1112
use dpp::util::hash::ripemd160_sha256;
1213
use 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
}

‎packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,52 @@ mod tests {
356356
assert_eq!(managed.incoming_contact_requests.len(), 1);
357357
assert_eq!(managed.established_contacts.len(), 0);
358358
}
359+
360+
#[test]
361+
fn test_disable_keys_stamps_disabled_at() {
362+
use dpp::identity::accessors::{IdentityGettersV0, IdentitySettersV0};
363+
use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
364+
use dpp::identity::identity_public_key::v0::IdentityPublicKeyV0;
365+
use dpp::identity::{IdentityPublicKey, KeyType, Purpose, SecurityLevel};
366+
use dpp::platform_value::BinaryData;
367+
368+
let make_key = |id: u32| {
369+
IdentityPublicKey::V0(IdentityPublicKeyV0 {
370+
id,
371+
key_type: KeyType::ECDSA_SECP256K1,
372+
purpose: Purpose::AUTHENTICATION,
373+
security_level: SecurityLevel::HIGH,
374+
contract_bounds: None,
375+
read_only: false,
376+
data: BinaryData::new(vec![0x02; 33]),
377+
disabled_at: None,
378+
})
379+
};
380+
381+
let mut identity = create_test_identity();
382+
let mut keys = BTreeMap::new();
383+
keys.insert(0, make_key(0));
384+
keys.insert(1, make_key(1));
385+
identity.set_public_keys(keys);
386+
387+
let mut managed = ManagedIdentity::new(identity, 0);
388+
let p = noop_persister();
389+
390+
// Disable key 1 plus a non-existent key 9 (must be skipped).
391+
managed.disable_keys(&[1, 9], 1_700_000_000, &p);
392+
393+
let pk0 = managed.identity.get_public_key_by_id(0).unwrap();
394+
let pk1 = managed.identity.get_public_key_by_id(1).unwrap();
395+
assert_eq!(pk0.disabled_at(), None, "untouched key must stay enabled");
396+
assert_eq!(
397+
pk1.disabled_at(),
398+
Some(1_700_000_000),
399+
"targeted key must be stamped disabled"
400+
);
401+
// The skipped key id must not have materialized a phantom row.
402+
assert!(
403+
managed.identity.get_public_key_by_id(9).is_none(),
404+
"non-existent key id must not be created"
405+
);
406+
}
359407
}

0 commit comments

Comments
 (0)