Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Wallet/WalletAPI.swift3// Description: The wallet's base endpoints: registry, accounts, portfolio, prices, activity, export, address book, settings,4// security overview, emergency halt. Reads only, except the address book, the settings and the one-way halt.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public struct WalletAPI: Sendable {11 let client: APIClient12 public init(_ client: APIClient) { self.client = client }1314 public func networks() async throws -> [Network] { try await client.get("/networks") }15 public func accounts() async throws -> Accounts { try await client.get("/accounts") }16 public func portfolio() async throws -> Portfolio { try await client.get("/portfolio") }1718 public func chart(symbol: String, days: Int, currency: FiatCurrency) async throws -> MarketChart {19 precondition([1, 7, 30, 365].contains(days), "the server serves 1, 7, 30 or 365 days")20 return try await client.get("/market/\(APIClient.segment(symbol))", query: ["days": String(days), "currency": currency.rawValue.lowercased()])21 }2223 public func activity(limit: Int = 100) async throws -> [ActivityItem] {24 try await client.get("/activity", query: ["limit": String(min(max(limit, 1), 500))], as: ActivityFeed.self).items25 }26 /** The server's own CSV of every proposal and deposit (formula-injection-safe quoting is the server's). */27 public func activityCSV() async throws -> Data { try await client.raw(.get, "/export/activity.csv", headers: ["Accept": "text/csv"]).0 }2829 public func addressBook() async throws -> [AddressBookEntry] { try await client.get("/addressbook") }30 public func saveAddress(network: String, address: String, label: String) async throws -> AddressBookEntry {31 struct Body: Encodable, Sendable { let network, address, label: String }32 return try await client.post("/addressbook", body: Body(network: network, address: address, label: label))33 }34 public func deleteAddress(id: String) async throws { let _: OKResponse = try await client.delete("/addressbook/\(APIClient.segment(id))") }3536 public func settings() async throws -> WalletSettings { try await client.get("/settings") }37 public func update(settings: WalletSettings) async throws -> WalletSettings { try await client.put("/settings", body: settings) }3839 public func securityOverview() async throws -> SecurityOverview { try await client.get("/security/overview") }40 public func audit(limit: Int = 100) async throws -> AuditJournal { try await client.get("/security/audit", query: ["limit": String(min(max(limit, 1), 500))]) }41 public func healthReady() async throws -> HealthReady { try await client.get("/health/ready") }4243 /**44 Emergency halt: the signer stops signing anything that can move funds out or add exposure. ONE-WAY from every app:45 lifting it requires the operator's command on the signer machine.46 */47 public func emergencyHalt() async throws {48 struct R: Decodable, Sendable { let ok: Bool; let halted: Bool }49 let r: R = try await client.post("/security/emergency-halt")50 guard r.halted else { throw APIError(status: 200, code: "halt_not_confirmed", message: "the signer did not confirm the halt") }51 }52}53