nip19: add bech32 TLV url parsing

Create shortened URLs for bech32 with TLV data strings. Additionally,
upon clicking on an nevent URL the user is directed to the note.

Lightning-url: LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G
Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-01-18 14:59:27 -05:00
committed by William Casarin
parent af75eed83a
commit d07ad67778
9 changed files with 285 additions and 88 deletions

View File

@@ -6,6 +6,7 @@
//
import XCTest
import SwiftUI
@testable import damus
class NoteContentViewTests: XCTestCase {
@@ -35,5 +36,101 @@ class NoteContentViewTests: XCTestCase {
XCTAssertTrue((parsed.blocks[0].asURL != nil), "NoteContentView does not correctly parse an image block when url in JSON content contains optional escaped slashes.")
}
func testMentionStr_Pubkey_ContainsAbbreviated() throws {
let compatibleText = createCompatibleText(test_pubkey.npub)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "17ldvg64:nq5mhr77")
}
func testMentionStr_Pubkey_ContainsFullBech32() {
let compatableText = createCompatibleText(test_pubkey.npub)
assertCompatibleTextHasExpectedString(compatibleText: compatableText, expected: test_pubkey.npub)
}
func testMentionStr_Nprofile_ContainsAbbreviated() throws {
let compatibleText = createCompatibleText("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p")
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "180cvv07:wsyjh6w6")
}
func testMentionStr_Nprofile_ContainsFullBech32() throws {
let bech = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech)
}
func testMentionStr_Note_ContainsAbbreviated() {
let compatibleText = createCompatibleText(test_note.id.bech32)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "note1qqq:qqn2l0z3")
}
func testMentionStr_Note_ContainsFullBech32() {
let compatableText = createCompatibleText(test_note.id.bech32)
assertCompatibleTextHasExpectedString(compatibleText: compatableText, expected: test_note.id.bech32)
}
func testMentionStr_Nevent_ContainsAbbreviated() {
let bech = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "nevent1q:t5nxnepm")
}
func testMentionStr_Nevent_ContainsFullBech32() throws {
let bech = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech)
}
func testMentionStr_Nrelay_ContainsAbbreviated() {
let bech = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "wss://relay.nostr.band")
}
func testMentionStr_Nrelay_ContainsFullBech32() {
let bech = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech)
}
func testMentionStr_Naddr_ContainsAbbreviated() {
let bech = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: "naddr1qq:3cnmhuld")
}
func testMentionStr_Naddr_ContainsFullBech32() {
let bech = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld"
let compatibleText = createCompatibleText(bech)
assertCompatibleTextHasExpectedString(compatibleText: compatibleText, expected: bech)
}
}
private func assertCompatibleTextHasExpectedString(compatibleText: CompatibleText, expected: String) {
guard let hasExpected = compatibleText.items.first?.attributed_string()?.description.contains(expected) else {
XCTFail()
return
}
XCTAssertTrue(hasExpected)
}
private func createCompatibleText(_ bechString: String) -> CompatibleText {
guard let mentionRef = Bech32Object.parse(bechString)?.toMentionRef() else {
XCTFail("Failed to create MentionRef from Bech32 string")
return CompatibleText()
}
return mention_str(.any(mentionRef), profiles: test_damus_state.profiles)
}

View File

@@ -228,5 +228,47 @@ class damusTests: XCTestCase {
XCTAssertEqual(txt, "there is no mention here")
}
func testTagGeneration_Nevent_ContainsETag() {
let ev = createEventFromContentString("nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm")
XCTAssertEqual(ev.tags.count, 1)
XCTAssertEqual(ev.tags[0][0].string(), "e")
XCTAssertEqual(ev.tags[0][1].string(), "b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696")
}
func testTagGeneration_Nprofile_ContainsPTag() {
let ev = createEventFromContentString("nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p")
XCTAssertEqual(ev.tags.count, 1)
XCTAssertEqual(ev.tags[0][0].string(), "p")
XCTAssertEqual(ev.tags[0][1].string(), "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
}
func testTagGeneration_Nrelay_ContainsRTag() {
let ev = createEventFromContentString("nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t")
XCTAssertEqual(ev.tags.count, 1)
XCTAssertEqual(ev.tags[0][0].string(), "r")
XCTAssertEqual(ev.tags[0][1].string(), "wss://relay.nostr.band")
}
func testTagGeneration_Naddr_ContainsATag(){
let ev = createEventFromContentString("naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld")
XCTAssertEqual(ev.tags.count, 1)
XCTAssertEqual(ev.tags[0][0].string(), "a")
XCTAssertEqual(ev.tags[0][1].string(), "30023:599f67f7df7694c603a6d0636e15ebc610db77dcfd47d6e5d05386d821fb3ea9:1700730909108")
}
}
private func createEventFromContentString(_ content: String) -> NostrEvent {
let post = NostrPost(content: content, references: [])
guard let ev = post_to_event(post: post, keypair: test_keypair_full) else {
XCTFail("Could not create event")
return test_note
}
return ev
}