Repository Public
1// SP Wallet Core2// File: Tests/SPWalletCoreTests/PaymentURITests.swift3// Description: What a QR code may contain: every accepted scheme (BIP-21, EIP-681, Solana Pay transfer, tron:, ripple:/xrpl:,4// stellar: / SEP-7 pay, cosmos:, ton://transfer, cardano:), every typed refusal, exact amount conversions, and5// the URIs built for the Receive screen. Addresses are public ones (genesis coinbase, published specification6// examples, well-known contracts) or the synthetic ones of Fixtures/address-vectors.json.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation11import Testing12@testable import SPWalletCore1314private let genesis = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"15private let segwit = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"16private let evm = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"17private let usdc = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"18private let solMint = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"19private let solAccount = "So11111111111111111111111111111111111111112"20private let xrp = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"21private let tron = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"22private let ada = "addr1vx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzers66hrl8"23private let tonAddress = "UQAzWZa6nM5mJev91wGc7VCSfBoIsYRqKJpV78N8Add9-RKY"24private let cosmos = "cosmos19rl4cm2hmr8afy4kldpxz3fka4jguq0auqdal4"25private let cash = "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"2627private func refusal(_ text: String) -> PaymentURIRejection? {28 do { _ = try PaymentURI.parse(text); return nil } catch { return error }29}3031@Suite struct PaymentURIParsingTests {32 @Test func plainAddress() throws {33 let r = try PaymentURI.parse(" \(evm)\n")34 #expect(r.scheme == nil); #expect(r.networkHint == nil); #expect(r.address == evm)35 #expect(r.amount == nil); #expect(r.amountBaseUnits == nil); #expect(r.asset == nil); #expect(r.memo == nil)36 #expect(try r.amountText(decimals: 18) == nil)37 // a TON raw address has a colon but no scheme38 let raw = "0:" + String(repeating: "ab", count: 32)39 #expect(try PaymentURI.parse(raw).address == raw)40 #expect(try PaymentURI.parse("-1:" + String(repeating: "ab", count: 32)).scheme == nil)41 // a plain address resolves to the networks where it validates42 #expect(r.networks(among: AddressFixtures.allNetworks).map(\.id) == ["ethereum", "base", "arbitrum", "polygon", "optimism", "bsc", "avalanche", "hyperevm"])43 #expect(try PaymentURI.parse(tron).networks(among: AddressFixtures.allNetworks).map(\.id) == ["tron"])44 }4546 @Test func bip21() throws {47 let r = try PaymentURI.parse("bitcoin:\(genesis)?amount=0.015&label=Luke-Jr&message=Donation%20for%20project%20xyz")48 #expect(r.scheme == "bitcoin"); #expect(r.networkHint == .networkId("bitcoin")); #expect(r.address == genesis)49 #expect(r.amount == "0.015"); #expect(r.label == "Luke-Jr"); #expect(r.message == "Donation for project xyz")50 #expect(try r.amountText(decimals: 8) == "0.015")51 #expect(r.networks(among: AddressFixtures.allNetworks).map(\.id) == ["bitcoin"])52 // upper-case QR form, address untouched53 let upper = try PaymentURI.parse("BITCOIN:\(segwit.uppercased())?AMOUNT=20.3")54 #expect(upper.scheme == "bitcoin"); #expect(upper.address == segwit.uppercased()); #expect(upper.amount == "20.3")55 // unknown optional parameters are ignored and reported; an empty query is fine56 let extra = try PaymentURI.parse("bitcoin:\(genesis)?somethingyoudontunderstand=50&lightning=lnbc1&amount=1")57 #expect(extra.amount == "1"); #expect(extra.ignoredParameters == ["somethingyoudontunderstand", "lightning"])58 #expect(try PaymentURI.parse("bitcoin:\(genesis)?").address == genesis)59 #expect(try PaymentURI.parse("bitcoin:\(genesis)").amount == nil)60 // percent-encoded UTF-8 label61 #expect(try PaymentURI.parse("bitcoin:\(genesis)?label=Caf%C3%A9%20%E2%82%BF").label == "Café ₿")62 for (scheme, id) in [("litecoin", "litecoin"), ("dogecoin", "dogecoin"), ("bitcoincash", "bitcoincash")] {63 let x = try PaymentURI.parse("\(scheme):\(cash)?amount=1.5")64 #expect(x.networkHint == .networkId(id)); #expect(x.address == cash); #expect(x.amount == "1.5")65 }66 // a CashAddr with its prefix is read as the bitcoincash: scheme; the address part is still what the backend accepts67 let c = try PaymentURI.parse("bitcoincash:\(cash)")68 #expect(c.address == cash)69 #expect(AddressValidator.check(c.address, network: AddressFixtures.network("bitcoincash")) == .valid(normalized: "bitcoincash:\(cash)"))70 // the amount is only text until the asset is known: 9 decimals do not fit 871 let precise = try PaymentURI.parse("bitcoin:\(genesis)?amount=0.000000001")72 #expect(throws: AmountError.tooManyDecimals) { try precise.amountText(decimals: 8) }73 }7475 @Test func bip21Refusals() {76 #expect(refusal("bitcoin:\(genesis)?req-somethingyoudontunderstand=50") == .requiredParameterUnknown("req-somethingyoudontunderstand"))77 #expect(refusal("bitcoin:\(genesis)?amount=1&req-pj=https%3A%2F%2Fexample.com") == .requiredParameterUnknown("req-pj"))78 #expect(refusal("bitcoin:?amount=1") == .missingAddress)79 #expect(refusal("bitcoin:") == .missingAddress)80 #expect(refusal("bitcoin://\(genesis)") == .malformed)81 #expect(refusal("bitcoin:\(genesis)?amount=1&amount=2") == .duplicateParameter("amount"))82 #expect(refusal("bitcoin:\(genesis)?amount=1&AMOUNT=2") == .duplicateParameter("amount"))83 for bad in ["1,5", "1e3", "-1", "+1", ".5", "1.", "", "0x10", "1 000", "1", "1.2.3", String(repeating: "9", count: 61)] {84 let got = refusal("bitcoin:\(genesis)?amount=\(bad)")85 #expect(got == .badAmount || got == .malformed, "amount \(bad): \(String(describing: got))")86 }87 #expect(refusal("bitcoin:\(genesis)?amount=1&&label=x") == .malformed)88 #expect(refusal("bitcoin:\(genesis)?amount=1#fragment") == .malformed)89 #expect(refusal("bitcoin:\(genesis)?label=a?b") == .malformed)90 #expect(refusal("bitcoin:\(genesis)?label=%zz") == .badEncoding)91 #expect(refusal("bitcoin:\(genesis)?label=%e9") == .badEncoding) // not UTF-892 #expect(refusal("bitcoin:\(genesis)?label=%") == .badEncoding)93 #expect(refusal("bitcoin:\(genesis)?label=a%00b") == .controlCharacter)94 #expect(refusal("bitcoin:\(genesis)?label=a%0Ab") == .controlCharacter)95 #expect(refusal("bitcoin:\(genesis)?label=a%E2%80%AEb") == .controlCharacter) // right-to-left override96 #expect(refusal("bitcoin:\(genesis)?label=My Shop") == .malformed) // unencoded space97 #expect(refusal("bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7Div%66Na") == .malformed) // an address is never percent-decoded98 #expect(refusal("bitcoin:\(genesis)@example.com") == .malformed)99 #expect(refusal("bitcoin:bitcoin:\(genesis)") == .malformed)100 }101102 @Test func eip681NativeTransfer() throws {103 let r = try PaymentURI.parse("ethereum:\(evm)@8453?value=1500000000000000000")104 #expect(r.scheme == "ethereum"); #expect(r.networkHint == .evm(chainId: "8453")); #expect(r.address == evm); #expect(r.asset == nil)105 #expect(r.amountBaseUnits?.description == "1500000000000000000"); #expect(r.amount == "1.5")106 #expect(r.networks(among: AddressFixtures.allNetworks).map(\.id) == ["base"])107 // no chain id: any EVM network, the app keeps the one selected108 let any = try PaymentURI.parse("ethereum:pay-\(evm)?value=1")109 #expect(any.networkHint == .evm(chainId: nil)); #expect(any.amount == "0.000000000000000001")110 #expect(any.networks(among: AddressFixtures.allNetworks).count == 8)111 #expect(try PaymentURI.parse("ethereum:\(evm)@1").amount == nil)112 // a chain this wallet does not have resolves to nothing113 #expect(try PaymentURI.parse("ethereum:\(evm)@59144").networks(among: AddressFixtures.allNetworks).isEmpty)114 // scientific notation of EIP-681, exact115 #expect(try PaymentURI.parse("ethereum:\(evm)@1?value=2.014e18").amount == "2.014")116 #expect(try PaymentURI.parse("ethereum:\(evm)@1?value=1e18").amountBaseUnits?.description == "1000000000000000000")117 #expect(try PaymentURI.parse("ethereum:\(evm)@1?value=1.50E3").amountBaseUnits?.description == "1500")118 #expect(try PaymentURI.parse("ethereum:\(evm)@1?value=0").amount == "0")119 // fee hints are never used120 let gas = try PaymentURI.parse("ethereum:\(evm)@1?value=1e18&gas=21000&gasPrice=5e9&gasLimit=21000")121 #expect(gas.amount == "1"); #expect(gas.ignoredParameters == ["gas", "gaslimit", "gasprice"])122 #expect(PaymentURI.eip681Number("115792089237316195423570985008687907853269984665640564039457584007913129639935") == .max256)123 #expect(PaymentURI.eip681Number("115792089237316195423570985008687907853269984665640564039457584007913129639936") == nil)124 #expect(PaymentURI.eip681Number("1.15792089237316195423570985008687907853269984665640564039457584007913129639935e77") == .max256)125 #expect(PaymentURI.eip681Number("0.000e2")?.isZero == true)126 for bad in ["1.5", "1e", "e5", "1.5e0", "0.0001e3", "-1", "+1", "1e78", "1e-3", "1e+3", "0x10", "1_000", "", ".", "1.", ".5e3", "2e77", "1,5e3", "1 e3"] {127 #expect(PaymentURI.eip681Number(bad) == nil, "\(bad)")128 }129 }130131 @Test func eip681TokenTransfer() throws {132 let r = try PaymentURI.parse("ethereum:\(usdc)@1/transfer?address=\(evm)&uint256=2500000")133 #expect(r.address == evm); #expect(r.asset == usdc); #expect(r.networkHint == .evm(chainId: "1"))134 #expect(r.amountBaseUnits?.description == "2500000")135 #expect(r.amount == nil) // decimals unknown until the token is matched against the allowlist136 #expect(try r.amountText(decimals: 6) == "2.5")137 #expect(try PaymentURI.parse("ethereum:\(usdc)/transfer?address=\(evm)&uint256=1e6").amountBaseUnits?.description == "1000000")138 #expect(try PaymentURI.parse("ethereum:\(usdc)@1/transfer?address=\(evm)").amountBaseUnits == nil)139 }140141 @Test func eip681Refusals() {142 #expect(refusal("ethereum:vitalik.eth?value=1") == .unsupportedAddressForm)143 #expect(refusal("ethereum:\(evm.dropLast())?value=1") == .unsupportedAddressForm)144 #expect(refusal("ethereum:?value=1") == .missingAddress)145 #expect(refusal("ethereum:\(evm)@0x1") == .badChainId)146 #expect(refusal("ethereum:\(evm)@") == .badChainId)147 #expect(refusal("ethereum:\(evm)@01") == .badChainId)148 #expect(refusal("ethereum:\(evm)@1?value=1.5") == .badAmount)149 #expect(refusal("ethereum:\(evm)@1?value=-1") == .badAmount)150 #expect(refusal("ethereum:\(evm)@1?value=") == .badAmount)151 #expect(refusal("ethereum:\(evm)@1?value=1&data=0xdeadbeef") == .unsupportedParameter("data"))152 #expect(refusal("ethereum:\(usdc)@1/approve?address=\(evm)&uint256=1") == .unsupportedFunction("approve"))153 #expect(refusal("ethereum:\(usdc)@1/transferFrom?address=\(evm)&uint256=1") == .unsupportedFunction("transferFrom"))154 #expect(refusal("ethereum:\(usdc)@1/transfer?uint256=1") == .missingAddress)155 #expect(refusal("ethereum:\(usdc)@1/transfer?address=vitalik.eth&uint256=1") == .unsupportedAddressForm)156 #expect(refusal("ethereum:\(usdc)@1/transfer?address=\(evm)&uint256=1&value=1e18") == .unsupportedParameter("value"))157 #expect(refusal("ethereum:\(usdc)@1/transfer?address=\(evm)&uint256=1.5") == .badAmount)158 #expect(refusal("ethereum:\(evm)@1?value=1&value=2") == .duplicateParameter("value"))159 }160161 @Test func solanaPayTransferOnly() throws {162 let r = try PaymentURI.parse("solana:\(solAccount)?amount=0.01&spl-token=\(solMint)&reference=\(solMint)&reference=\(solAccount)&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId12345")163 #expect(r.networkHint == .networkId("solana")); #expect(r.address == solAccount); #expect(r.amount == "0.01"); #expect(r.asset == solMint)164 #expect(r.label == "Michael"); #expect(r.message == "Thanks for all the fish"); #expect(r.memo == "OrderId12345"); #expect(r.ignoredParameters == ["reference"])165 #expect(try PaymentURI.parse("solana:\(solAccount)").amount == nil)166 // transaction requests are never fetched nor executed167 #expect(refusal("solana:https://example.com/solana-pay") == .transactionRequest)168 #expect(refusal("solana:https%3A%2F%2Fexample.com%2Fsolana-pay%3Forder%3D12345") == .transactionRequest)169 #expect(refusal("solana:HTTPS://EXAMPLE.COM/PAY") == .transactionRequest)170 #expect(refusal("solana://example.com/pay") == .transactionRequest)171 #expect(refusal("solana:\(solAccount)?amount=1e3") == .badAmount)172 #expect(refusal("solana:\(solAccount)?amount=.5") == .badAmount)173 #expect(refusal("solana:\(solAccount)?spl-token=notamint") == .malformed)174 #expect(refusal("solana:\(solAccount)?link=https%3A%2F%2Fexample.com") == .unsupportedParameter("link"))175 #expect(refusal("solana:\(solAccount)?amount=1&amount=1") == .duplicateParameter("amount"))176 #expect(refusal("solana:") == .missingAddress)177 }178179 @Test func tronRippleCosmosCardano() throws {180 let t = try PaymentURI.parse("tron:\(tron)?amount=12.5")181 #expect(t.networkHint == .networkId("tron")); #expect(t.address == tron); #expect(t.amount == "12.5")182 #expect(refusal("tron:\(tron)?token=\(tron)") == .unsupportedParameter("token"))183184 for scheme in ["ripple", "xrpl"] {185 let r = try PaymentURI.parse("\(scheme):\(xrp)?amount=25.5&dt=4294967295")186 #expect(r.scheme == scheme); #expect(r.networkHint == .networkId("xrpl")); #expect(r.address == xrp); #expect(r.amount == "25.5"); #expect(r.memo == "4294967295")187 }188 #expect(try PaymentURI.parse("ripple:\(xrp)?dt=0").memo == "0")189 #expect(try PaymentURI.parse("ripple:\(xrp)").memo == nil)190 for bad in ["4294967296", "01", "-1", "1.0", "", "abc", "12345678901"] { #expect(refusal("ripple:\(xrp)?dt=\(bad)") == .unsupportedMemo, "\(bad)") }191 #expect(refusal("ripple:\(xrp)?amount=1¤cy=USD") == .unsupportedParameter("currency"))192193 let c = try PaymentURI.parse("cosmos:\(cosmos)?amount=1.25&memo=order%2042")194 #expect(c.networkHint == .networkId("cosmos")); #expect(c.address == cosmos); #expect(c.amount == "1.25"); #expect(c.memo == "order 42")195 #expect(refusal("cosmos:\(cosmos)?amount=1000uatom") == .badAmount)196197 for scheme in ["cardano", "web+cardano"] {198 let a = try PaymentURI.parse("\(scheme):\(ada)?amount=4.5")199 #expect(a.networkHint == .networkId("cardano")); #expect(a.address == ada); #expect(a.amount == "4.5"); #expect(a.memo == nil)200 }201 #expect(refusal("web+cardano://stake?c64333ae3e83") == .malformed)202 #expect(refusal("cardano:\(ada)?memo=x") == .unsupportedParameter("memo"))203 }204205 @Test func stellarShortFormAndSEP7Pay() throws {206 let g = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF" // the all-zero key: a syntactic example, never a destination207 let short = try PaymentURI.parse("stellar:\(g)?amount=10.5&memo=hello")208 #expect(short.networkHint == .networkId("stellar")); #expect(short.address == g); #expect(short.amount == "10.5"); #expect(short.memo == "hello")209 let pay = try PaymentURI.parse("web+stellar:pay?destination=\(g)&amount=120.1234567&memo=skdjfasf&memo_type=MEMO_TEXT&msg=pay%20me%20with%20lumens&network_passphrase=Public%20Global%20Stellar%20Network%20%3B%20September%202015&origin_domain=example.com&signature=abc&callback=url%3Ahttps%3A%2F%2Fexample.com")210 #expect(pay.scheme == "web+stellar"); #expect(pay.address == g); #expect(pay.amount == "120.1234567"); #expect(pay.memo == "skdjfasf"); #expect(pay.message == "pay me with lumens")211 #expect(pay.asset == nil); #expect(pay.ignoredParameters == ["callback", "origin_domain", "signature"])212 #expect(try pay.amountText(decimals: 7) == "120.1234567")213 let issued = try PaymentURI.parse("web+stellar:pay?destination=\(g)&asset_code=USDC&asset_issuer=\(g)&memo=42&memo_type=MEMO_ID")214 #expect(issued.asset == "USDC:\(g)"); #expect(issued.memo == "42")215 #expect(try PaymentURI.parse("stellar:pay?destination=\(g)").address == g)216 #expect(try PaymentURI.parse("web+stellar:pay?destination=\(g)&memo=123").memo == "123") // no type given: the backend's single memo rule applies217218 #expect(refusal("web+stellar:tx?xdr=AAAAAA") == .transactionRequest)219 #expect(refusal("stellar:tx?xdr=AAAAAA") == .transactionRequest)220 #expect(refusal("web+stellar:pay?amount=1") == .missingAddress)221 #expect(refusal("web+stellar:\(g)") == .malformed)222 #expect(refusal("web+stellar:pay?destination=\(g)&network_passphrase=Test%20SDF%20Network%20%3B%20September%202015") == .wrongNetwork)223 #expect(refusal("web+stellar:pay?destination=\(g)&memo=AAAA&memo_type=MEMO_HASH") == .unsupportedMemo)224 #expect(refusal("web+stellar:pay?destination=\(g)&memo=AAAA&memo_type=MEMO_RETURN") == .unsupportedMemo)225 #expect(refusal("web+stellar:pay?destination=\(g)&memo=123&memo_type=MEMO_TEXT") == .unsupportedMemo) // the backend would send it as an id226 #expect(refusal("web+stellar:pay?destination=\(g)&memo=007&memo_type=MEMO_ID") == .unsupportedMemo)227 #expect(refusal("web+stellar:pay?destination=\(g)&memo=18446744073709551616&memo_type=MEMO_ID") == .unsupportedMemo)228 #expect(refusal("web+stellar:pay?destination=\(g)&memo_type=MEMO_ID") == .unsupportedMemo)229 #expect(refusal("web+stellar:pay?destination=\(g)&asset_code=USDC") == .malformed)230 #expect(refusal("web+stellar:pay?destination=\(g)&asset_issuer=\(g)") == .malformed)231 #expect(refusal("web+stellar:pay?destination=\(g)&xdr=AAAA") == .unsupportedParameter("xdr"))232 #expect(refusal("web+stellar:pay?destination=\(g)%0A") == .controlCharacter)233 }234235 @Test func tonTransferLink() throws {236 let r = try PaymentURI.parse("ton://transfer/\(tonAddress)?amount=1500000000&text=hello%20ton")237 #expect(r.scheme == "ton"); #expect(r.networkHint == .networkId("ton")); #expect(r.address == tonAddress)238 #expect(r.amountBaseUnits?.description == "1500000000"); #expect(r.amount == "1.5"); #expect(r.memo == "hello ton")239 let raw = "0:" + String(repeating: "ab", count: 32)240 #expect(try PaymentURI.parse("ton://transfer/\(raw)").address == raw)241 let jetton = try PaymentURI.parse("ton://transfer/\(tonAddress)?amount=1000000&jetton=\(tonAddress)")242 #expect(jetton.asset == tonAddress); #expect(jetton.amount == nil); #expect(jetton.amountBaseUnits?.description == "1000000")243 #expect(try jetton.amountText(decimals: 6) == "1")244 #expect(refusal("ton://transfer/\(tonAddress)?amount=1.5") == .badAmount) // nanotons are an integer245 #expect(refusal("ton://transfer/\(tonAddress)?amount=01") == .badAmount)246 #expect(refusal("ton://transfer/\(tonAddress)?bin=te6ccgEBAQEAAgAAAA") == .unsupportedParameter("bin"))247 #expect(refusal("ton://transfer/\(tonAddress)?amount=1&init=te6ccgEBAQEAAgAAAA") == .unsupportedParameter("init"))248 #expect(refusal("ton://transfer/") == .missingAddress)249 #expect(refusal("ton:\(tonAddress)") == .malformed)250 #expect(refusal("ton://dns/example.ton") == .malformed)251 #expect(refusal("ton://transfer/\(tonAddress)/extra") == .malformed)252 }253254 @Test func refusedSchemesSizesAndCharacters() {255 for (text, scheme) in [("https://example.com/pay?to=\(evm)", "https"), ("http://example.com", "http"), ("javascript:alert(1)", "javascript"), ("JavaScript:alert(1)", "javascript"),256 ("data:text/html;base64,PHNjcmlwdD4=", "data"), ("file:///etc/passwd", "file"), ("wss://example.com", "wss"), ("mailto:a@example.com", "mailto"), ("intent://scan/#Intent;end", "intent")] {257 #expect(refusal(text) == .forbiddenScheme(scheme), "\(text)")258 }259 for (text, scheme) in [("wc:7f6e504bfad60b485450578e05678ed3e8e8c4751d3c6160be17160d63ec90f9@2?relay-protocol=irn", "wc"), ("lightning:lnbc1pvjluez", "lightning"), ("monero:4AdUndXHHZ", "monero"),260 ("bchtest:\(cash)", "bchtest"), ("near:alice.near", "near"), ("polkadot:15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5", "polkadot"), ("tonkeeper://transfer/\(tonAddress)", "tonkeeper"), ("otpauth://totp/x?secret=y", "otpauth")] {261 #expect(refusal(text) == .unknownScheme(scheme), "\(text)")262 }263 #expect(refusal("") == .empty)264 #expect(refusal(" \n\t ") == .empty)265 #expect(refusal(String(repeating: "1", count: 2049)) == .tooLong)266 #expect(refusal("bitcoin:\(genesis)?label=" + String(repeating: "a", count: 2048)) == .tooLong)267 #expect(refusal(String(repeating: "1", count: 131)) == .malformed) // longer than any destination the backend accepts268 #expect(refusal(String(repeating: "1", count: 130)) == nil)269 #expect(refusal("\(evm)\u{0000}") == .controlCharacter)270 #expect(refusal("0xd8dA6BF26964aF9D\n7eEd9e03E53415D37aA96045") == .controlCharacter)271 #expect(refusal("\(evm)\u{200b}") == .controlCharacter) // zero-width space272 #expect(refusal("\u{202e}\(evm)") == .controlCharacter) // right-to-left override273 #expect(refusal("bitcoin:\(genesis)\u{007f}") == .controlCharacter)274 #expect(refusal("\(evm) \(evm)") == .malformed)275 #expect(refusal("send 1 BTC to \(genesis)") == .malformed)276 }277}278279@Suite struct PaymentURIMakeTests {280 /** First address of the backend-generated fixture that the backend accepts unchanged, per network. */281 private func ownAddresses() throws -> [String: String] {282 var out: [String: String] = [:]283 for c in try AddressFixtures.vectors().cases where c.valid && out[c.network] == nil && ["synthetic", "implicit", "known"].contains(c.kind) && c.normalized == c.input { out[c.network] = c.input }284 return out285 }286287 @Test func withoutAnAmountTheCodeIsThePlainAddress() throws {288 let own = try ownAddresses()289 #expect(own.count == 23)290 for n in AddressFixtures.allNetworks {291 let address = try #require(own[n.id])292 #expect(try PaymentURI.make(address: address, network: n) == address)293 #expect(try PaymentURI.parse(PaymentURI.make(address: address, network: n)).address.hasSuffix(address.split(separator: ":").last.map(String.init) ?? address))294 }295 }296297 @Test func withAnAmountTheStandardOfTheFamilyOrThePlainAddress() throws {298 let own = try ownAddresses()299 func make(_ id: String, _ amount: String, decimals: Int) throws -> String {300 let p = try #require(AddressParameters.find(id: id))301 let n = Network(id: id, name: id, family: p.family, chainRef: p.chainRef, native: AssetDef(id: "native", symbol: "X", name: "X", decimals: decimals, program: nil, onchainSymbol: nil),302 tokens: [], memo: nil, activation: nil, explorerAddr: "", minConf: 1, health: nil)303 return try PaymentURI.make(address: try #require(own[id]), network: n, amount: amount)304 }305 let a = { (id: String) in own[id] ?? "?" }306 #expect(try make("bitcoin", "0.0150", decimals: 8) == "bitcoin:\(a("bitcoin"))?amount=0.015")307 #expect(try make("litecoin", "2", decimals: 8) == "litecoin:\(a("litecoin"))?amount=2")308 #expect(try make("dogecoin", "100,5", decimals: 8) == "dogecoin:\(a("dogecoin"))?amount=100.5")309 #expect(try make("bitcoincash", "1.5", decimals: 8) == "\(a("bitcoincash"))?amount=1.5")310 #expect(a("bitcoincash").hasPrefix("bitcoincash:q"))311 #expect(try make("ethereum", "1.5", decimals: 18) == "ethereum:\(a("ethereum"))@1?value=1500000000000000000")312 #expect(try make("base", "0.000000000000000001", decimals: 18) == "ethereum:\(a("base"))@8453?value=1")313 #expect(try make("hyperevm", "2", decimals: 18) == "ethereum:\(a("hyperevm"))@999?value=2000000000000000000")314 #expect(try make("solana", "0.25", decimals: 9) == "solana:\(a("solana"))?amount=0.25")315 #expect(try make("stellar", "10.1234567", decimals: 7) == "web+stellar:pay?destination=\(a("stellar"))&amount=10.1234567")316 #expect(try make("ton", "1.5", decimals: 9) == "ton://transfer/\(a("ton"))?amount=1500000000")317 #expect(try make("cardano", "4.5", decimals: 6) == "web+cardano:\(a("cardano"))?amount=4.5")318 // no standard for these families: the plain address, never an invented scheme319 for (id, decimals) in [("tron", 6), ("xrpl", 6), ("cosmos", 6), ("sui", 9), ("aptos", 8), ("near", 24), ("polkadot", 10)] {320 #expect(try make(id, "1", decimals: decimals) == a(id), "\(id)")321 #expect(PaymentURI.standardScheme(for: AddressFixtures.network(id)) == nil)322 }323 // what is built parses back to the same destination and the same exact amount324 for (id, decimals, amount) in [("bitcoin", 8, "0.00000001"), ("litecoin", 8, "12.5"), ("dogecoin", 8, "1000"), ("bitcoincash", 8, "0.5"), ("ethereum", 18, "0.123456789012345678"), ("polygon", 18, "3"),325 ("solana", 9, "0.000000001"), ("stellar", 7, "0.0000001"), ("ton", 9, "2.000000001"), ("cardano", 6, "1.000001")] {326 let uri = try make(id, amount, decimals: decimals)327 let back = try PaymentURI.parse(uri)328 #expect(back.networks(among: AddressFixtures.allNetworks).map(\.id) == [id], "\(uri)")329 #expect(AddressValidator.check(back.address, network: AddressFixtures.network(id)) == .valid(normalized: a(id)), "\(uri)")330 #expect(try back.amountText(decimals: decimals) == amount, "\(uri)")331 #expect(back.asset == nil); #expect(back.memo == nil)332 }333 }334335 @Test func refusesWhatItCannotStandBehind() throws {336 let own = try ownAddresses()337 let eth = AddressFixtures.network("ethereum"), address = try #require(own["ethereum"])338 #expect(throws: PaymentURIRejection.invalidAddress) { try PaymentURI.make(address: address.lowercased(), network: eth) } // valid but not the normalized form: never rewritten here339 #expect(throws: PaymentURIRejection.invalidAddress) { try PaymentURI.make(address: " " + address, network: eth) }340 #expect(throws: PaymentURIRejection.invalidAddress) { try PaymentURI.make(address: "0x0000000000000000000000000000000000000000", network: eth, amount: "1") }341 #expect(throws: PaymentURIRejection.invalidAddress) { try PaymentURI.make(address: try #require(own["bitcoin"]), network: eth) }342 for bad in ["0", "0.0", "", "abc", "-1", "1e3", "0.0000000000000000001"] {343 #expect(throws: PaymentURIRejection.badAmount) { try PaymentURI.make(address: address, network: eth, amount: bad) }344 }345 // a network this build does not know: the plain address only, whatever the amount346 let unknown = AddressFixtures.network("linea", family: "evm", chainRef: "59144")347 #expect(try PaymentURI.make(address: address, network: unknown) == address)348 #expect(try PaymentURI.make(address: address, network: unknown, amount: "1") == address)349 #expect(PaymentURI.standardScheme(for: unknown) == nil)350 #expect(PaymentURI.standardScheme(for: eth) == "ethereum")351 }352}353