damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 85262e1a4e5884cc790911d7e8424cec3da54687
parent 64a224135aa98376d3fd8a2a8cd3ab8867ab251a
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 13 May 2023 23:54:25 -0700

nwc: fix response parsing

Diffstat:
Mdamus/Models/HomeModel.swift | 5++++-
Mdamus/Util/WalletConnect.swift | 13++++++++++---
2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -138,7 +138,10 @@ class HomeModel: ObservableObject { func handle_nwc_response(_ ev: NostrEvent) { Task { @MainActor in - guard let resp = await FullWalletResponse(from: ev) else { + // TODO: Adapt KeychainStorage to StringCodable and instead of parsing to WalletConnectURL every time + guard let nwc_str = damus_state.settings.nostr_wallet_connect, + let nwc = WalletConnectURL(str: nwc_str), + let resp = await FullWalletResponse(from: ev, nwc: nwc) else { return } diff --git a/damus/Util/WalletConnect.swift b/damus/Util/WalletConnect.swift @@ -88,7 +88,7 @@ struct FullWalletResponse { let req_id: String let response: WalletResponse - init?(from: NostrEvent) async { + init?(from: NostrEvent, nwc: WalletConnectURL) async { guard let req_id = from.referenced_ids.first else { return nil } @@ -96,7 +96,9 @@ struct FullWalletResponse { self.req_id = req_id.ref_id let ares = Task { - guard let resp: WalletResponse = decode_json(from.content) else { + guard let json = decrypt_dm(nwc.keypair.privkey, pubkey: nwc.pubkey, content: from.content, encoding: .base64), + let resp: WalletResponse = decode_json(json) + else { let resp: WalletResponse? = nil return resp } @@ -116,7 +118,7 @@ struct FullWalletResponse { struct WalletResponse: Decodable { let result_type: WalletResponseResultType let error: WalletResponseErr? - let result: WalletResponseResult + let result: WalletResponseResult? private enum CodingKeys: CodingKey { case result_type, error, result @@ -133,6 +135,11 @@ struct WalletResponse: Decodable { self.result_type = result_type self.error = try container.decodeIfPresent(WalletResponseErr.self, forKey: .error) + guard self.error == nil else { + self.result = nil + return + } + switch result_type { case .pay_invoice: let res = try container.decode(PayInvoiceResponse.self, forKey: .result)