nip19: add high level bech32 encoding method

Add encode method for converting Bech32Object to a bech32 encoded
string. This will be useful for generalized conversion of Bech32Objects
to its string representation.

Lightning-url: LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G
Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-01-13 14:19:45 -05:00
committed by William Casarin
parent cb4adf06f1
commit a9e9701243
2 changed files with 198 additions and 0 deletions

View File

@@ -115,4 +115,101 @@ class Bech32ObjectTests: XCTestCase {
XCTAssertEqual(expectedObject, actualObject)
}
func testTLVEncoding_NeventHasRelaysNoAuthorNoKind_ValidContent() throws {
guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
XCTFail("Parsing note ID failed")
return
}
let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"]
let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5nxnepm"
let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays)))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
func testTLVEncoding_NeventHasRelaysNoAuthorHasKind_ValidContent() throws {
guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
XCTFail()
return
}
let relays = [
"wss://nostr-relay.untethr.me",
"wss://nostr-pub.wellorder.net"
]
let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qvzqqqqqqyjyqz7d"
let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, kind: 1)))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
func testTLVEncoding_NeventHasRelaysHasAuthorHasKind_ValidContent() throws {
guard let noteid = hex_decode_noteid("b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696") else {
XCTFail("Parsing note ID failed")
return
}
guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else {
XCTFail()
return
}
let relays = ["wss://nostr-relay.untethr.me", "wss://nostr-pub.wellorder.net"]
let expectedEncoding = "nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr3mhxue69uhkummnw3ez6un9d3shjtn4de6x2argwghx6egpr4mhxue69uhkummnw3ez6ur4vgh8wetvd3hhyer9wghxuet5qgsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8grqsqqqqqpw4032x"
let actualEncoding = Bech32Object.encode(.nevent(NEvent(noteid: noteid, relays: relays, author: Pubkey(author.data), kind: 1)))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
func testTLVEncoding_NProfileExample_ValidContent() throws {
guard let author = try bech32_decode("npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6") else {
XCTFail()
return
}
let relays = [
"wss://r.x.com",
"wss://djbas.sadkb.com"
]
let expectedEncoding = "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p"
let actualEncoding = Bech32Object.encode(.nprofile(NProfile(author: Pubkey(author.data), relays: relays)))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
func testTLVEncoding_NRelayExample_ValidContent() throws {
let relay = "wss://relay.nostr.band"
let expectedEncoding = "nrelay1qqt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueq4r295t"
let actualEncoding = Bech32Object.encode(.nrelay(relay))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
func testTLVEncoding_NaddrExample_ValidContent() throws {
guard let author = try bech32_decode("npub1tx0k0a7lw62vvqax6p3ku90tccgdka7ul4radews2wrdsg0m865szf9fw6") else {
XCTFail()
return
}
let relays = ["wss://relay.nostr.band"]
let identifier = "1700730909108"
let kind: UInt32 = 30023
let expectedEncoding = "naddr1qqxnzdesxqmnxvpexqunzvpcqyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqzypve7elhmamff3sr5mgxxms4a0rppkmhmn7504h96pfcdkpplvl2jqcyqqq823cnmhuld"
let actualEncoding = Bech32Object.encode(.naddr(NAddr(identifier: identifier, author: Pubkey(author.data), relays: relays, kind: kind)))
XCTAssertEqual(expectedEncoding, actualEncoding)
}
}