Skip to content
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

236 changes: 187 additions & 49 deletions DashWallet/Sources/Models/DashConnect/AppConnectProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,61 +267,38 @@ extension AppConnect {
/// Every connection the wallet can show for these identities. An app
/// with no intent appears when one of its contracts carries a live auth
/// key no intent names.
static func project(_ snapshots: [IdentitySnapshot], intents: [ConnectionIntent]) -> Projection {
static func project(_ snapshots: [IdentitySnapshot], intents: [ConnectionIntent], owners: [Data: Data] = [:]) -> Projection {
let byIdentity = Dictionary(snapshots.map { ($0.identityId, $0) }, uniquingKeysWith: { a, b in
b.revision >= a.revision ? b : a
})
// An id an intent names is that intent's only while the key under
// it is the one the intent submits: one held by another key is that
// key's.
let named = Set(intents.flatMap { intent in
intent.keys.filter { key in
byIdentity[intent.identityId].map { presence(of: key, in: $0) != .conflicting } ?? true
}.map { IdentityKey(intent.identityId, $0.id) }
})

// The contracts each app's intents on each identity bind keys to.
var contractsOfApp: [Data: [Data: Set<Data>]] = [:]
for intent in intents {
contractsOfApp[intent.identityId, default: [:]][intent.appId, default: [intent.appId]]
.formUnion(intent.authKeys.compactMap(\.bound).filter { !isProfileBound($0) }.map(contractId(of:)))
}
let placing = UnrecordedPlacing(snapshots: Array(byIdentity.values), intents: intents)

var unrecorded: [Data: [Data: [UInt32]]] = [:]
var profile: [Data: [UInt32]] = [:]
var unplaced: [Data: [UInt32]] = [:]
for snapshot in snapshots {
for key in snapshot.keys where isAppConnectAuthKey(key)
&& !named.contains(IdentityKey(snapshot.identityId, key.id)) {
guard let bound = key.bound else {
// The store does not hold the key's bound yet: whose it
// is cannot be told, and it is reported as such.
if !key.boundKnown { unplaced[snapshot.identityId, default: []].append(key.id) }
for key in snapshot.keys {
switch placing.placement(of: key, on: snapshot.identityId) {
case nil, .system:
continue
}
if isProfileBound(bound) {
// No app can be told to hold it. It is offered with any
// app's disconnect, and stands as a connection of its
// own, so it can be turned off whatever other rows there
// are.
case .unplaced:
unplaced[snapshot.identityId, default: []].append(key.id)
case .profile:
profile[snapshot.identityId, default: []].append(key.id)
if let dashPay = dashPayContractIdData {
unrecorded[dashPay, default: [:]][snapshot.identityId, default: []].append(key.id)
}
continue
}
// A key bound to a system contract is no app's. One bound to
// a contract the intents of several apps bind (on this
// identity first) cannot be told to be any one of theirs:
// each of them shows it, and a disconnect of any of them
// turns it off with the user's confirm.
let contract = contractId(of: bound)
guard !systemContractIds.contains(contract.toBase58String()) else { continue }
func binding(_ contracts: [Data: Set<Data>]) -> [Data] { contracts.filter { $0.value.contains(contract) }.map(\.key) }
var apps = binding(contractsOfApp[snapshot.identityId] ?? [:])
if apps.isEmpty { apps = Array(Set(contractsOfApp.values.flatMap(binding))) }
for app in apps.isEmpty ? [contract] : apps {
unrecorded[app, default: [:]][snapshot.identityId, default: []].append(key.id)
case .apps(let apps):
for app in apps {
unrecorded[app, default: [:]][snapshot.identityId, default: []].append(key.id)
}
case .noApp(let contract):
// It joins an app here only when attributed to it by
// owner; otherwise it stands on its own contract's row.
let apps = placing.apps(on: snapshot.identityId)
let attributed = apps.filter { ownerOutcome(of: contract, for: $0, among: apps, owners: owners) == .attributed }
let row = (attributed.count == 1 ? attributed.first : nil) ?? contract

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, for ADR-0032's Negative consequences.

Case: app X is connected on this device. App Z, same owner, was approved only on another device, so there is no intent for Z here. Z's key is bound to Z's own contract. Here that is .noApp(contract: Z), the owners match, no other app with an intent shares the owner, so the key is .attributed to X. Before this PR it stood on a row of its own.

Result: Z has no row, its key is counted under X, and X's question («keys of this app from another device…») offers to turn it off.

The wallet cannot tell this from X's own key on a sibling contract; it is the same shape as the AC-03 table case. The user's confirm is the safeguard, and AC-07 only covers two apps with an intent here. I would write the case down in the ADR so the next reader knows it was accepted, not missed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 968e24a. ADR-0032 now lists this case under Negative: the key shows under the one app here with an intent and is offered by its question, the user's confirm being the safeguard.

unrecorded[row, default: [:]][snapshot.identityId, default: []].append(key.id)
Comment on lines +298 to +301

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocking: Disclose same-developer key attribution in the disconnect question

Owner equality does not establish which app holds a key. With an intent for X here and another same-developer app Z approved only on another device, Z's contract-bound key takes this path and moves under X because Z has no local intent. X's disconnect then offers that key, but ConnectionsViewModel.swift:459–471 describes the offered keys as “keys of this app” or “the keys this app holds.” The same-developer warning is absent because this key is attributed rather than counted in keysNotAttributable. Confirming therefore disables Z's key without disclosing that another app's access is included. ADR-0032 documents this exact case and relies on confirmation as the safeguard, but the actual question does not disclose that scope. Preserve the intended owner-wide behavior, explicitly tell the user that owner-inferred keys may serve other apps by the same developer, and add an X/Z regression covering both the disclosure and the exact confirmed key set.

source: gpt-6.1-sol (phase2-reviewer: general)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred: the question naming its keys, including keys of other apps by the same developer, is tracked in #1386 together with the dialog design it needs; ADR-0032 records the case as accepted. Not changing this in this PR.

}
}
}
Expand Down Expand Up @@ -350,6 +327,119 @@ extension AppConnect {
unplacedKeyIds: unplaced)
}

/// The ids whose owner the projection needs for `snapshot`: the contract
/// of every app with an intent on its identity, and the bound (contract,
/// or contract group) of every unrecorded key no intent names by
/// contract. Empty when there is no such key, or no app has an intent on
/// the identity: then no owner decides anything.
static func ownerIdsNeeded(snapshot: IdentitySnapshot, intents: [ConnectionIntent]) -> Set<Data> {
let placing = UnrecordedPlacing(snapshots: [snapshot], intents: intents)
let apps = placing.apps(on: snapshot.identityId)
guard !apps.isEmpty else { return [] }
let bounds = snapshot.keys.compactMap { key -> Data? in
guard case .noApp(let contract) = placing.placement(of: key, on: snapshot.identityId) else { return nil }
return contract
}
guard !bounds.isEmpty else { return [] }
return Set(bounds).union(apps)
}

/// How an unrecorded key bound to a contract no intent names stands
/// against one app, by owner.
enum OwnerOutcome: Equatable {
/// The bound's known owner is the app's, and no other app here has
/// that owner or an unknown one.
case attributed
/// The bound's known owner is the app's, and another app here has it
/// too, or the bound is a contract group with admins, who may add
/// their own contracts to it: whose the key is cannot be told.
case notAttributable
/// It may be the app's, but an owner that decides it is unknown.
case notChecked
/// It is not the app's: a known other owner, or the system's.
case notThisApp
}

/// The owner rule, the only place it lives. `apps` are the apps with an
/// intent on the identity, `app` among them; `owners` holds the owners
/// read, and an id it lacks has an unknown owner, which is never a match
/// or a mismatch.
static func ownerOutcome(
of bound: Data, for app: Data, among apps: Set<Data>, owners: [Data: Data]
) -> OwnerOutcome {
let boundOwner = owners[bound]
if boundOwner == systemContractOwnerId { return .notThisApp }
if boundOwner == administeredGroupOwnerId { return .notAttributable }
guard let boundOwner, let appOwner = owners[app] else { return .notChecked }
guard boundOwner == appOwner else { return .notThisApp }
let others = apps.subtracting([app]).map { owners[$0] }
if others.contains(appOwner) { return .notAttributable }
return others.contains(nil) ? .notChecked : .attributed
}

/// Where an unrecorded key goes, before any owner is asked.
private enum UnrecordedPlacement: Equatable {
/// The store does not hold the key's bound yet: whose it is cannot be
/// told, and it is reported as such.
case unplaced
/// Bound to the DashPay profile. No app can be told to hold it. It is
/// offered with any app's disconnect, and stands as a connection of
/// its own, so it can be turned off whatever other rows there are.
case profile
/// Bound to a system contract: no app's.
case system
/// Bound to a contract the intents of these apps bind (on this
/// identity first). With several, it cannot be told to be any one
/// of theirs: each of them shows it, and a disconnect of any of them
/// turns it off with the user's confirm.
case apps([Data])
/// Bound to a contract no intent binds; `contract` is the bound's
/// contract, or contract group.
case noApp(contract: Data)
}

/// Which keys intents name, and the contracts their apps bind, for one
/// set of reads.
private struct UnrecordedPlacing {
private let named: Set<IdentityKey>
/// The contracts each app's intents on each identity bind keys to.
private let contractsOfApp: [Data: [Data: Set<Data>]]

init(snapshots: [IdentitySnapshot], intents: [ConnectionIntent]) {
let byIdentity = Dictionary(snapshots.map { ($0.identityId, $0) }, uniquingKeysWith: { a, _ in a })
// An id an intent names is that intent's only while the key
// under it is the one the intent submits: one held by another
// key is that key's.
named = Set(intents.flatMap { intent in
intent.keys.filter { key in
byIdentity[intent.identityId].map { presence(of: key, in: $0) != .conflicting } ?? true
}.map { IdentityKey(intent.identityId, $0.id) }
})
var contractsOfApp: [Data: [Data: Set<Data>]] = [:]
for intent in intents {
contractsOfApp[intent.identityId, default: [:]][intent.appId, default: [intent.appId]]
.formUnion(intent.authKeys.compactMap(\.bound).filter { !isProfileBound($0) }.map(contractId(of:)))
}
self.contractsOfApp = contractsOfApp
}

/// The apps with an intent on `identityId`.
func apps(on identityId: Data) -> Set<Data> { Set(contractsOfApp[identityId].map { Array($0.keys) } ?? []) }

/// Nil for a key that is not an unrecorded App Connect key.
func placement(of key: PlatformKey, on identityId: Data) -> UnrecordedPlacement? {
guard isAppConnectAuthKey(key), !named.contains(IdentityKey(identityId, key.id)) else { return nil }
guard let bound = key.bound else { return key.boundKnown ? nil : .unplaced }
if isProfileBound(bound) { return .profile }
let contract = contractId(of: bound)
guard !systemContractIds.contains(contract.toBase58String()) else { return .system }
func binding(_ contracts: [Data: Set<Data>]) -> [Data] { contracts.filter { $0.value.contains(contract) }.map(\.key) }
var apps = binding(contractsOfApp[identityId] ?? [:])
if apps.isEmpty { apps = Array(Set(contractsOfApp.values.flatMap(binding))) }
return apps.isEmpty ? .noApp(contract: contract) : .apps(apps)
}
}

private struct IdentityKey: Hashable {
let identityId: Data
let keyId: UInt32
Expand Down Expand Up @@ -480,6 +570,20 @@ extension AppConnect {
var fence: [UInt32] = []
/// Intents whose response may be on Platform.
var deleteResponses: [UUID] = []
/// The live keys no intent names on the app's row, or, for an app
/// with no intent on the identity, every one placed on its contract:
/// those a disconnect asks the user to turn off with it.
var offeredUnrecorded: [UInt32] = []
/// Keys that may be the app's whose owner, or the app's or another
/// app's owner, is unknown, and keys whose bound the store does not
/// hold: they stay on, and no access left cannot be claimed.
var notChecked: [UInt32] = []
/// Keys of the app's owner that another app here has too: they stay
/// on, since they may be the other app's.
var notAttributable: [UInt32] = []
/// Offered keys the user's question did not list: they stay on for a
/// later disconnect.
var leftAfterQuestion: [UInt32] = []

var isEmpty: Bool { disable.isEmpty && fence.isEmpty && deleteResponses.isEmpty }
}
Expand All @@ -504,10 +608,14 @@ extension AppConnect {
return Set(otherKeyIds).subtracting(app).sorted() + app.sorted()
}

/// `includeUnrecorded`: also turn off the app's live keys no intent
/// names; the user confirms that separately.
/// `owners`: the owners read, an absent id unknown. `listedUnrecordedKeyIds`:
/// the unrecorded keys the user's question listed and confirmed, nil
/// before the question. Of those, only the ones still offered
/// (`DisconnectPlan.offeredUnrecorded`) are turned off: never a key the
/// question did not count.
static func disconnectPlan(
appId: Data, snapshot: IdentitySnapshot, intents: [ConnectionIntent], includeUnrecorded: Bool
appId: Data, snapshot: IdentitySnapshot, intents: [ConnectionIntent], owners: [Data: Data],
listedUnrecordedKeyIds: Set<UInt32>?
) -> DisconnectPlan {
var plan = DisconnectPlan()
let own = intents.filter { $0.appId == appId && $0.identityId == snapshot.identityId }
Expand All @@ -519,11 +627,41 @@ extension AppConnect {
}
if !intent.responseRemoved { plan.deleteResponses.append(intent.id) }
}
if includeUnrecorded {
let projection = project([snapshot], intents: intents)
let others = projection.connections.first { $0.appId == appId }?.unrecordedKeyIds[snapshot.identityId] ?? []
plan.disable += others
let placing = UnrecordedPlacing(snapshots: [snapshot], intents: intents)
let apps = placing.apps(on: snapshot.identityId)
var offered = Set(project([snapshot], intents: intents, owners: owners).connections
.first { $0.appId == appId }?.unrecordedKeyIds[snapshot.identityId] ?? [])
if !apps.contains(appId) {
// A row with no intent here is the contract the user named: its
// keys stay its own even when an owner read since attributes
// them to an app here.
offered.formUnion(snapshot.keys.filter {
placing.placement(of: $0, on: snapshot.identityId) == .noApp(contract: appId)
}.map(\.id))
Comment on lines +634 to +640

@thepastaclaw thepastaclaw Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Resolved at e661639b; see the replies below.

🔴 Blocking: Resolve the retry target from the surviving Connections rows

The direct-contract fallback preserves consent for a no-intent row, but settlement can move its remaining keys onto another app's row while the result still directs the user back to the original connection. For example, Y initially has its own row because its owner is unreadable, while X has a local intent. Disconnecting Y can resolve ownership during settlement, move Y's keys under X, and still return a confirmation question tied to Y through this fallback. If another key on Y arrives after that question, confirmation disables only the listed keys and reports the new key as leftAfterQuestion. The published list contains X rather than Y, but ConnectionsViewModel says to disconnect the original app again. Preserve direct-contract consent for disabling keys, but derive the remaining keys' retry destination from the settled projection and surface the surviving connection instead. Add coverage combining owner recovery, disappearance of Y's row, and a key arriving after the question.

Raised on code unchanged since the last review, and confirmed by an independent pass: With an intent for X and an unrecorded key on same-owner contract Y, an initially unreadable owner gives Y its own row; disconnecting Y can recover ownership during settlement and remove Y's row while retaining a confirmation tied to Y. If another Y-bound key arrives after that question, confirmation leaves it enabled under X but tells the user to disconnect Y again, an action no longer available in Connections.

source: gpt-6.1-sol (phase2-reviewer: general)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e661639. Settlement returns the app whose row shows the keys left on; when the disconnected row is gone, the result carries that row's name and the message points there instead of asking for a second disconnect, while direct-contract consent still decides what is turned off. Covered by testKeysLeftOnARowThatIsGoneAreReportedUnderTheRowThatShowsThem and testKeysLeftUnderAnotherRowNameThatRow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved (re-reviewed at e661639b): Your holder lookup and keysLeftUnder result now direct the reported single-holder owner-recovery case to the surviving app row while preserving direct-contract consent. The added projection and message regressions cover that transition; the new multi-row coverage issue is reported separately.

}
plan.offeredUnrecorded = offered.sorted()
if let listed = listedUnrecordedKeyIds {
plan.disable += offered.intersection(listed)
plan.leftAfterQuestion = offered.subtracting(listed).sorted()
}
// The keys that may be the app's but stay on, by the same owner rule
// that places them on rows.
for key in snapshot.keys {
switch placing.placement(of: key, on: snapshot.identityId) {
case .unplaced:
plan.notChecked.append(key.id)
case .noApp(let contract) where apps.contains(appId):
switch ownerOutcome(of: contract, for: appId, among: apps, owners: owners) {
case .notChecked: plan.notChecked.append(key.id)
case .notAttributable: plan.notAttributable.append(key.id)
case .attributed, .notThisApp: break
}
default:
break
}
}
plan.notChecked.sort()
plan.notAttributable.sort()
plan.disable = Array(Set(plan.disable)).sorted()
plan.fence = Array(Set(plan.fence)).sorted()
return plan
Expand Down
4 changes: 4 additions & 0 deletions DashWallet/Sources/Models/DashConnect/AppConnectV2Login.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ extension AppConnect {
]
/// The owner of every system contract. No identity has this id.
static let systemContractOwnerId = Data(count: 32)
/// What a contract group with admins is remembered as owned by: its
/// admins may add their own contracts to it, so no single owner holds
/// what is bound to it. No identity has this id.
static let administeredGroupOwnerId = Data(repeating: 0xFF, count: 32)

// MARK: What an approval grants

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ protocol DashConnectDataSource {
/// responses; it throws when a key could not be shown off, and the
/// connection then stays. Keys no approval of this wallet names (the
/// app's from another device or from before a restore, DashPay profile
/// keys no app can be told to hold) are turned off only with
/// `includingUnrecorded`; the result says how many are left. The
/// keys no app can be told to hold) are turned off only when listed in
/// `unrecordedKeys`, what the result of a disconnect without them
/// offered (nil: none listed); only those still the app's go, and none
/// when the active identity is not the one they were offered on. The
/// connection also stays, and that is thrown, while an approval of the
/// app on another identity of the wallet is not done.
///
/// `scope` is the `DAppConnection.scope` of the row that was picked:
/// both calls act only while it still names the wallet they would act
/// on.
@discardableResult
func disconnect(id: String, scope: String?, includingUnrecorded: Bool) async throws -> DashConnectDisconnectResult
func disconnect(id: String, scope: String?, unrecordedKeys: DashConnectOfferedKeys?) async throws -> DashConnectDisconnectResult
func remove(id: String, scope: String?) async
/// Puts the App Connect rows in step with the active identity's keys on
/// Platform. Best effort: a failed read leaves the rows as they were.
Expand Down Expand Up @@ -277,7 +279,7 @@ final class MockDashConnectDataSource: DashConnectDataSource {
}

@discardableResult
func disconnect(id: String, scope: String?, includingUnrecorded: Bool) async throws -> DashConnectDisconnectResult {
func disconnect(id: String, scope: String?, unrecordedKeys: DashConnectOfferedKeys?) async throws -> DashConnectDisconnectResult {
persistAndSend(subject.value.filter { $0.id != id })
return DashConnectDisconnectResult()
}
Expand Down
Loading
Loading