Skip to content
12 changes: 12 additions & 0 deletions DashWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,10 @@
RC00A0012FA0000000000001 /* TopIntro.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00A0032FA0000000000001 /* TopIntro.swift */; };
RC00A0022FA0000000000001 /* TopIntro.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00A0032FA0000000000001 /* TopIntro.swift */; };
RC00A0042FA0000000000001 /* ExploreMenuScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00A0062FA0000000000001 /* ExploreMenuScreen.swift */; };
UM00A0012FA0000000000001 /* UsernameMarketplaceScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = UM00A0032FA0000000000001 /* UsernameMarketplaceScreen.swift */; };
UM00A0022FA0000000000001 /* UsernameMarketplaceScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = UM00A0032FA0000000000001 /* UsernameMarketplaceScreen.swift */; };
UM00B0012FA0000000000001 /* UsernameMarketplaceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = UM00B0032FA0000000000001 /* UsernameMarketplaceService.swift */; };
UM00B0022FA0000000000001 /* UsernameMarketplaceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = UM00B0032FA0000000000001 /* UsernameMarketplaceService.swift */; };
RC00A0052FA0000000000001 /* ExploreMenuScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00A0062FA0000000000001 /* ExploreMenuScreen.swift */; };
RC00C0012FA0000000000001 /* CSVExportSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00C0032FA0000000000001 /* CSVExportSheet.swift */; };
RC00C0022FA0000000000001 /* CSVExportSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = RC00C0032FA0000000000001 /* CSVExportSheet.swift */; };
Expand Down Expand Up @@ -3635,6 +3639,8 @@
GG0000032DUMMYID001234567 /* GeoRestrictionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeoRestrictionService.swift; sourceTree = "<group>"; };
RC00A0032FA0000000000001 /* TopIntro.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopIntro.swift; sourceTree = "<group>"; };
RC00A0062FA0000000000001 /* ExploreMenuScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreMenuScreen.swift; sourceTree = "<group>"; };
UM00A0032FA0000000000001 /* UsernameMarketplaceScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UsernameMarketplaceScreen.swift; sourceTree = "<group>"; };
UM00B0032FA0000000000001 /* UsernameMarketplaceService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UsernameMarketplaceService.swift; sourceTree = "<group>"; };
RC00C0032FA0000000000001 /* CSVExportSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSVExportSheet.swift; sourceTree = "<group>"; };
FC1426BD13E794C788646055 /* ContestedNamesService.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = ContestedNamesService.swift; sourceTree = "<group>"; };
15D89828EC913714251D0B58 /* MasternodeVoterRegistry.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = MasternodeVoterRegistry.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6256,6 +6262,7 @@
47AE8BDF28C1305E00490F5E /* Views */,
7569D6822DE6DA6800768BFF /* ExploreViewController.swift */,
RC00A0062FA0000000000001 /* ExploreMenuScreen.swift */,
UM00A0032FA0000000000001 /* UsernameMarketplaceScreen.swift */,
);
path = "Explore Dash";
sourceTree = "<group>";
Expand Down Expand Up @@ -7109,6 +7116,7 @@
E181EB71C8803101EA789AA4 /* SwiftDashSDKHost.swift */,
35F37C177039C76661FA2DB2 /* SwiftDashSDKSPVCoordinator.swift */,
E6E56673B2BFED8EC0181A1F /* PlatformAddressSyncCoordinator.swift */,
UM00B0032FA0000000000001 /* UsernameMarketplaceService.swift */,
51AA000D2F97000D005A000D /* ShieldedSyncMonitor.swift */,
43C830DD6D63115525925F75 /* PlatformCreditsFormatter.swift */,
44DCDC3A2B92601BB1E61FD9 /* PlatformSendExecutor.swift */,
Expand Down Expand Up @@ -9820,6 +9828,8 @@
75EBAA0F2BB99036004488E3 /* TextIntro.swift in Sources */,
RC00A0012FA0000000000001 /* TopIntro.swift in Sources */,
RC00A0042FA0000000000001 /* ExploreMenuScreen.swift in Sources */,
UM00A0012FA0000000000001 /* UsernameMarketplaceScreen.swift in Sources */,
UM00B0012FA0000000000001 /* UsernameMarketplaceService.swift in Sources */,
0F71317728F436920072F454 /* ServiceOverviewTableCell.swift in Sources */,
47838B87290670630003E8AB /* IntegrationViewController.swift in Sources */,
758A8F172C4D35A80012FA41 /* NSLayoutConstraint+DashWallet.swift in Sources */,
Expand Down Expand Up @@ -10535,6 +10545,8 @@
75EBAA102BB99036004488E3 /* TextIntro.swift in Sources */,
RC00A0022FA0000000000001 /* TopIntro.swift in Sources */,
RC00A0052FA0000000000001 /* ExploreMenuScreen.swift in Sources */,
UM00A0022FA0000000000001 /* UsernameMarketplaceScreen.swift in Sources */,
UM00B0022FA0000000000001 /* UsernameMarketplaceService.swift in Sources */,
757514E52B1735370026AD8E /* JoinDashPayView.swift in Sources */,
C9D2C73C2A320AA000D15901 /* DWDecimalInputValidator.m in Sources */,
C9D2C73D2A320AA000D15901 /* PaymentMethods.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,21 @@ public final class PlatformAddressSyncCoordinator: NSObject, ObservableObject {
} catch {
Self.logger.error("👥 DASHPAY-SYNC :: startDashPaySync failed: \(String(describing: error), privacy: .public)")
}

// Start the recurring DPNS username-marketplace sync (tracks the
// wallet identities' names with sale state, detects sold/
// transferred departures, refreshes seller balances). Same
// best-effort contract as the blocks above; the marketplace
// screen's pull-to-refresh (`syncDpnsMarketplace`) still works
// without the loop.
do {
if try !manager.isDpnsSyncRunning() {
try manager.startDpnsSync()
}
Self.logger.info("🏷️ DPNS-SYNC :: started for \(network.rawValue, privacy: .public)")
} catch {
Self.logger.error("🏷️ DPNS-SYNC :: startDpnsSync failed: \(String(describing: error), privacy: .public)")
}
#endif

self.sdk = SwiftDashSDKHost.shared.sdk
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
//
// UsernameMarketplaceService.swift
// DashWallet
//
// DPNS username marketplace over the wallet-level SDK surface that
// landed with platform #4348 (`DpnsMarketplace.swift`): typed search
// with sale state, locally persisted my-names rows (including retained
// sold/transferred departures), per-name trade history from the
// Document History contract, and orchestrated trade operations with
// typed errors — `notForSale`, `priceChanged` (a purchase never
// executes at a price the user didn't confirm; consensus is the
// backstop), `insufficientIdentityCredits`, and
// `contestedNameNotTradable`.
//
// This layer adds only what belongs to the app: the PIN/biometric gate
// before anything is signed, resolution of the wallet's main identity,
// friendlier wording for the errors users act on, and the contested
// guard for direct registration (the trade ops are typed by the SDK).
//
// dashpay target only.
//

import Foundation
import OSLog
import SwiftData
import SwiftDashSDK

// MARK: - Identifiable projections for SwiftUI

extension DpnsMarketplaceName: Identifiable {
public var id: Data { documentId }

var priceDuffs: UInt64? { priceCredits.map { $0 / 1_000 } }
var isForSale: Bool { priceCredits != nil }

func isOwned(by identityId: Data?) -> Bool {
guard let identityId else { return false }
return ownerId == identityId
}
}

extension DpnsNameStateRow: Identifiable {
public var id: Data { documentId }

var priceDuffs: UInt64? { priceCredits.map { $0 / 1_000 } }
var isForSale: Bool {
guard case .owned = status else { return false }
return priceCredits != nil
}
}

// MARK: - UsernameMarketplaceService

/// Stateless facade over the SDK marketplace surface. Instantiated per
/// view model (it holds only an authorizer); deliberately NOT a
/// singleton.
@MainActor
struct UsernameMarketplaceService {

private static let logger = Logger(
subsystem: "org.dashfoundation.dash",
category: "swift-sdk-migration.username-marketplace")

enum ServiceError: LocalizedError {
case noIdentity
case contestedName
case invalidRecipient

var errorDescription: String? {
switch self {
case .noIdentity:
return NSLocalizedString("No DashPay identity is registered", comment: "DashPay")
case .contestedName:
return NSLocalizedString("This name is short enough to be contested and must be requested through the username flow, where it goes to a network vote.", comment: "Username marketplace")
case .invalidRecipient:
return NSLocalizedString("The recipient identity couldn't be resolved.", comment: "Username marketplace")
}
}
}

private let authorizer = DWIdentityAuthorizer()

// MARK: Reads

/// Prefix search with live sale state — one network query.
func search(prefix: String, limit: UInt32 = 25) async throws -> [DpnsMarketplaceName] {
guard let wallet = SwiftDashSDKHost.shared.wallet else { return [] }
return try await wallet.searchDpnsMarketplace(prefix: prefix, limit: limit)
}

/// The main identity's tracked names from the wallet's local rows —
/// no network round-trip. Includes retained `.sold` / `.transferred`
/// departures so the UI can show what left and to whom.
func myNames() async throws -> [DpnsNameStateRow] {
guard let wallet = SwiftDashSDKHost.shared.wallet,
let identityId = DWCurrentUserIdentityInfo.shared.identityId else { return [] }
return try await wallet.myDpnsMarketplaceNames(identityId: identityId)
}

/// Authoritative live state of one exact name; nil = unregistered.
func nameState(_ name: String) async throws -> DpnsMarketplaceName? {
guard let wallet = SwiftDashSDKHost.shared.wallet else { return nil }
return try await wallet.dpnsMarketplaceNameState(name: name)
}

/// Full trade timeline (registration, listings, purchases with
/// counterparties, transfers), block-time ascending.
func history(_ name: String) async throws -> [DpnsNameHistoryEvent] {
guard let wallet = SwiftDashSDKHost.shared.wallet else { return [] }
return try await wallet.dpnsNameHistory(name: name)
}

/// Pull-to-refresh: one marketplace sync pass (tracked names, added/
/// departed labels, price changes, seller balance refreshes).
@discardableResult
func syncNow() async throws -> DpnsMarketplaceSyncSummary {
guard let wallet = SwiftDashSDKHost.shared.wallet else {
throw ServiceError.noIdentity
}
return try await wallet.syncDpnsMarketplace()
}

// MARK: Trade actions (all PIN-gated; SDK errors are typed)

func setPrice(name: String, priceDuffs: UInt64) async throws {
let (wallet, container, ownerId) = try requireOwnContext()
try await authorizer.authorize()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
_ = try await wallet.setDpnsNamePrice(
ownerIdentityId: ownerId,
name: name,
priceCredits: priceDuffs * 1_000,
signer: KeychainSigner(modelContainer: container))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Self.logger.info("🏷️ MARKET :: price set on \(name, privacy: .public)")
}

func removeFromSale(name: String) async throws {
let (wallet, container, ownerId) = try requireOwnContext()
try await authorizer.authorize()
_ = try await wallet.delistDpnsName(
ownerIdentityId: ownerId,
name: name,
signer: KeychainSigner(modelContainer: container))
Self.logger.info("🏷️ MARKET :: delisted \(name, privacy: .public)")
}

func transfer(name: String, toIdentityBase58 recipient: String) async throws {
let (wallet, container, ownerId) = try requireOwnContext()
guard let recipientId = Data.identifier(fromBase58: recipient), recipientId.count == 32 else {
throw ServiceError.invalidRecipient
}
try await authorizer.authorize()
_ = try await wallet.transferDpnsName(
ownerIdentityId: ownerId,
name: name,
recipientId: recipientId,
signer: KeychainSigner(modelContainer: container))
Self.logger.info("🏷️ MARKET :: transferred \(name, privacy: .public)")
DWCurrentUserIdentityInfo.shared.refreshFromSDK()
}

/// Buy a listed name at exactly `expectedPriceCredits` — the SDK
/// pre-flights the listing and the buyer's identity balance, pins the
/// confirmed price into the transition, and consensus rejects any
/// seller-side change (typed `.priceChanged`).
func purchase(name: String, expectedPriceCredits: UInt64) async throws {
let (wallet, container, buyerId) = try requireOwnContext()
try await authorizer.authorize()
_ = try await wallet.purchaseDpnsName(
purchaserIdentityId: buyerId,
name: name,
expectedPriceCredits: expectedPriceCredits,
signer: KeychainSigner(modelContainer: container))
Self.logger.info("🏷️ MARKET :: purchased \(name, privacy: .public) for \(expectedPriceCredits, privacy: .public) credits")
// The name now points at the buyer's identity; refresh the
// snapshots the rest of the app renders usernames from.
DWCurrentUserIdentityInfo.shared.refreshFromSDK()
}

/// Register a name nobody owns. Contested-eligible labels must go
/// through the voting flow instead — this path refuses them rather
/// than silently starting a vote.
func register(label: String) async throws {
let (wallet, container, identityId) = try requireOwnContext()
if let sdk = SwiftDashSDKHost.shared.sdk {
let normalized = (try? sdk.dpnsNormalizeLabel(label)) ?? label.lowercased()
if Self.isContestedEligible(normalizedLabel: normalized) {
throw ServiceError.contestedName
}
}
try await authorizer.authorize()
_ = try await wallet.registerDpnsName(
identityId: identityId,
name: label,
signer: KeychainSigner(modelContainer: container))
Self.logger.info("🏷️ MARKET :: registered \(label, privacy: .public)")
DWCurrentUserIdentityInfo.shared.refreshFromSDK()
}

// MARK: Helpers

/// DPNS contested-name eligibility per the platform rules the
/// username flow enforces: normalized labels of 3–19 characters
/// consisting only of letters and digits (no hyphen) go to a vote.
static func isContestedEligible(normalizedLabel: String) -> Bool {
let length = normalizedLabel.count
guard (3...19).contains(length) else { return false }
return normalizedLabel.allSatisfy { $0.isLetter || $0.isNumber }
&& !normalizedLabel.allSatisfy { $0.isNumber }
}

/// Buyer identity's credit balance from the persisted row — for the
/// affordability hint; the SDK re-checks authoritatively at purchase.
static func identityBalanceCredits(identityId: Data, container: ModelContainer) -> UInt64 {
var descriptor = FetchDescriptor<PersistentIdentity>(
predicate: #Predicate { $0.identityId == identityId })
descriptor.fetchLimit = 1
guard let identity = (try? container.mainContext.fetch(descriptor))?.first else { return 0 }
return UInt64(bitPattern: identity.balance)
}

/// Friendlier wording for the typed SDK failures a user acts on;
/// everything else keeps the SDK's own description.
static func userFacingMessage(for error: Error) -> String {
if let walletError = error as? PlatformWalletError {
switch walletError {
case .notForSale:
return NSLocalizedString("This username is not for sale.", comment: "Username marketplace")
case .priceChanged:
return NSLocalizedString("The seller changed the price before your purchase was accepted. Review the new price and try again.", comment: "Username marketplace")
case .insufficientIdentityCredits(_, let required, let available):
return String.localizedStringWithFormat(
NSLocalizedString("Your identity balance can't cover this purchase: %@ DASH needed (including a fee reserve), %@ DASH available. Top up your identity balance and try again.", comment: "Username marketplace"),
(required / 1000).dashAmount.formattedDashAmountWithoutCurrencySymbol,
(available / 1000).dashAmount.formattedDashAmountWithoutCurrencySymbol)
case .contestedNameNotTradable:
return NSLocalizedString("This name is in an active network vote and can't be traded until the contest ends.", comment: "Username marketplace")
default:
break
}
}
return error.localizedDescription
}

private func requireOwnContext() throws -> (ManagedPlatformWallet, ModelContainer, Data) {
guard let wallet = SwiftDashSDKHost.shared.wallet,
let container = SwiftDashSDKHost.shared.modelContainer,
let identityId = DWCurrentUserIdentityInfo.shared.identityId else {
throw ServiceError.noIdentity
}
return (wallet, container, identityId)
}
}
15 changes: 15 additions & 0 deletions DashWallet/Sources/UI/Explore Dash/ExploreMenuScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ struct ExploreMenuScreen: View {
)
.frame(minHeight: 56)

MenuItem(
title: NSLocalizedString("Username Marketplace", comment: "Username marketplace"),
subtitle: NSLocalizedString("Buy, sell and transfer DashPay usernames", comment: "Username marketplace: Explore row subtitle"),
icon: .system("at.circle.fill"),
action: { showUsernameMarketplace() }
)
.frame(minHeight: 56)

MenuItem(
title: NSLocalizedString("ATMs", comment: ""),
subtitle: NSLocalizedString("Find ATMs where you can buy or sell Dash", comment: ""),
Expand Down Expand Up @@ -219,6 +227,13 @@ struct ExploreMenuScreen: View {
}
}

private func showUsernameMarketplace() {
let controller = UIHostingController(
rootView: UsernameMarketplaceScreen(vc: vc))
controller.hidesBottomBarWhenPushed = true
vc.pushViewController(controller, animated: true)
}

private func showAtms() {
let atmVC = AtmListViewController()
atmVC.payWithDashHandler = { onShowReceivePayment() }
Expand Down
Loading
Loading