damus

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

commit b1e0a6210987da97f0b3c66ce58ef85b6cb3c3d1
parent 1fc5ceff3be93e0b8774c363f367f34dbcf9d151
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Wed, 13 Sep 2023 18:44:42 +0000

nwc: fix parsing issue with NIP-47 compliant NWC urls without double-slashes

Closes: https://github.com/damus-io/damus/issues/1547
Changelog-Fixed: Fix parsing issue with NIP-47 compliant NWC urls without double-slashes
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Util/WalletConnect.swift | 10+++++-----
MdamusTests/WalletConnectTests.swift | 26++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/damus/Util/WalletConnect.swift b/damus/Util/WalletConnect.swift @@ -36,11 +36,11 @@ struct WalletConnectURL: Equatable { } init?(str: String) { - guard let url = URL(string: str), - url.scheme == "nostrwalletconnect" || url.scheme == "nostr+walletconnect", - let pkhost = url.host, - let pubkey = hex_decode_pubkey(pkhost), - let components = URLComponents(url: url, resolvingAgainstBaseURL: true), + guard let components = URLComponents(string: str), + components.scheme == "nostrwalletconnect" || components.scheme == "nostr+walletconnect", + // The line below provides flexibility for both `nostrwalletconnect://` (non-compliant, but commonly used) and `nostrwalletconnect:` (NIP-47 compliant) formats + let encoded_pubkey = components.path == "" ? components.host : components.path, + let pubkey = hex_decode_pubkey(encoded_pubkey), let items = components.queryItems, let relay = items.first(where: { qi in qi.name == "relay" })?.value, let relay_url = RelayURL(relay), diff --git a/damusTests/WalletConnectTests.swift b/damusTests/WalletConnectTests.swift @@ -36,6 +36,12 @@ final class WalletConnectTests: XCTestCase { } func testDoesNWCParse() { + // Test an NWC url format which is not technically NIP-47 and RFC 3986 compliant, but still commonly used (by Alby, for example) + // See Github issue #1547 for details on why this URL is non-compliant + // This test URL also features: + // - `nostrwalletconnect` scheme + // - A non-url-encoded relay parameter + // - lud16 parameter let pk = Pubkey(hex: "9d088f4760422443d4699b485e2ac66e565a2f5da1198c55ddc5679458e3f67a")! let sec = Privkey(hex: "ff2eefd57196d42089e1b42acc39916d7ecac52e0625bd70597bbd5be14aff18")! let relay = "wss://relay.getalby.com/v1" @@ -51,6 +57,26 @@ final class WalletConnectTests: XCTestCase { XCTAssertEqual(url.keypair.pubkey, privkey_to_pubkey(privkey: sec)) XCTAssertEqual(url.relay.id, relay) XCTAssertEqual(url.lud16, "jb55@jb55.com") + + // Test an NWC url format which is NIP-47 and RFC 3986 compliant + // This test URL also features: + // - `nostr+walletconnect` scheme + // - A url-encoded relay parameter + // - No lud16 parameter + let pk_2 = Pubkey(hex: "b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4")! + let sec_2 = Privkey(hex: "71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c")! + let relay_2 = "wss://relay.damus.io" + let str_2 = "nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c" + + let url_2 = WalletConnectURL(str: str_2) + XCTAssertNotNil(url_2) + guard let url_2 else { + return + } + XCTAssertEqual(url_2.pubkey, pk_2) + XCTAssertEqual(url_2.keypair.privkey, sec_2) + XCTAssertEqual(url_2.keypair.pubkey, privkey_to_pubkey(privkey: sec_2)) + XCTAssertEqual(url_2.relay.id, relay_2) } func testNWCEphemeralRelay() {