Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/backend_task/identity/load_identity_from_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,20 @@ impl AppContext {

let wallet_seed_hash = wallet_arc_ref.wallet.read()?.seed_hash();

// Derive alias from DPNS names if available (same logic as load_identity.rs)
let alias = if !maybe_owned_dpns_names.is_empty() {
Some(format!("{}.dash", maybe_owned_dpns_names[0].name))
} else {
None
};

let mut qualified_identity = QualifiedIdentity {
identity: identity.clone(),
associated_voter_identity: None,
associated_operator_identity: None,
associated_owner_key_id: None,
identity_type: IdentityType::User,
alias: None,
alias,
private_keys: Default::default(),
dpns_names: Vec::new(),
associated_wallets: BTreeMap::new(),
Expand Down
57 changes: 31 additions & 26 deletions src/database/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,38 @@ impl Database {

let status = qualified_identity.status.as_u8();

if let Some((wallet, wallet_index)) = wallet_and_identity_id_info {
// If wallet information is provided, insert with wallet and wallet_index
self.execute(
"INSERT OR REPLACE INTO identity
let (wallet, wallet_index) = match wallet_and_identity_id_info {
Some((w, idx)) => (Some(w.as_slice()), Some(*idx)),
None => (None, None),
};

// Use INSERT ... ON CONFLICT to merge with existing data rather than
// blindly overwriting. COALESCE preserves existing non-null values (e.g.
// alias, wallet, wallet_index) when the incoming value is NULL.
self.execute(
"INSERT INTO identity
(id, data, is_local, alias, identity_type, network, wallet, wallet_index, status)
VALUES (?, ?, 1, ?, ?, ?, ?, ?, ?)",
params![
id,
data,
alias,
identity_type,
network,
wallet,
wallet_index,
status,
],
)?;
} else {
tracing::warn!(identity_id=?id, alias, network, "saving identity without wallet; this needs investigating");
// If wallet information is not provided, insert without wallet and wallet_index
self.execute(
"INSERT OR REPLACE INTO identity
(id, data, is_local, alias, identity_type, network, status)
VALUES (?, ?, 1, ?, ?, ?, ?)",
params![id, data, alias, identity_type, network, status],
)?;
}
VALUES (?1, ?2, 1, ?3, ?4, ?5, ?6, ?7, ?8)
ON CONFLICT(id) DO UPDATE SET
data = excluded.data,
is_local = 1,
alias = COALESCE(excluded.alias, identity.alias),
identity_type = excluded.identity_type,
network = excluded.network,
wallet = COALESCE(excluded.wallet, identity.wallet),
wallet_index = COALESCE(excluded.wallet_index, identity.wallet_index),
status = excluded.status",
params![
id,
data,
alias,
identity_type,
network,
wallet,
wallet_index,
status
],
)?;

Ok(())
}
Expand Down