SPB Git

Repository Public

AddressParameters.swift 6.5 KB 101 lines swift

1// SP Wallet Core2// File: Sources/SPWalletCore/Blockchain/AddressParameters.swift3// Description: Address parameters of every network of the backend registry (packages/core/src/networks.ts), embedded because4//              the API does not serve the `utxo` / `cosmos` blocks. COPIED, never guessed: AddressValidatorTests compares this5//              table with Fixtures/address-registry.json, which is generated from the TypeScript registry, and fails when the6//              registry gains a network or changes a parameter. A network absent from this table is `.unverifiable`.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation1112struct AddressParameters: Sendable, Equatable, Codable {13    struct UTXO: Sendable, Equatable, Codable {14        /** Human-readable part of native SegWit addresses; nil when the chain has none (Dogecoin, Bitcoin Cash). */15        let bech32: String?16        /** Base58Check version byte of pay-to-public-key-hash addresses. */17        let pubKeyHash: UInt818        /** Base58Check version byte of pay-to-script-hash addresses. */19        let scriptHash: UInt820        /** `wpkh` (SegWit chains) or `pkh` (legacy-only chains: no bech32 destination is accepted). */21        let kind: String22        /** CashAddr prefix (Bitcoin Cash). */23        let cashaddr: String?24    }25    struct Cosmos: Sendable, Equatable, Codable { let bech32: String }2627    let id: String28    let family: String29    let chainRef: String30    let utxo: UTXO?31    let cosmos: Cosmos?3233    private static func evm(_ id: String, _ chainId: String) -> AddressParameters { .init(id: id, family: "evm", chainRef: chainId, utxo: nil, cosmos: nil) }34    private static func plain(_ id: String, _ family: String, _ chainRef: String) -> AddressParameters { .init(id: id, family: family, chainRef: chainRef, utxo: nil, cosmos: nil) }3536    /** Same order as networks.ts. */37    static let table: [AddressParameters] = [38        evm("ethereum", "1"), evm("base", "8453"), evm("arbitrum", "42161"), evm("polygon", "137"), evm("optimism", "10"),39        evm("bsc", "56"), evm("avalanche", "43114"), evm("hyperevm", "999"),40        .init(id: "bitcoin", family: "btc", chainRef: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",41              utxo: .init(bech32: "bc", pubKeyHash: 0x00, scriptHash: 0x05, kind: "wpkh", cashaddr: nil), cosmos: nil),42        .init(id: "litecoin", family: "btc", chainRef: "12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2",43              utxo: .init(bech32: "ltc", pubKeyHash: 0x30, scriptHash: 0x32, kind: "wpkh", cashaddr: nil), cosmos: nil),44        .init(id: "dogecoin", family: "btc", chainRef: "1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691",45              utxo: .init(bech32: nil, pubKeyHash: 0x1e, scriptHash: 0x16, kind: "pkh", cashaddr: nil), cosmos: nil),46        .init(id: "bitcoincash", family: "bch", chainRef: "0000000000000000029e471c41818d24b8b74c911071c4ef0b4a0509f9b5a8ce",47              utxo: .init(bech32: nil, pubKeyHash: 0x00, scriptHash: 0x05, kind: "pkh", cashaddr: "bitcoincash"), cosmos: nil),48        plain("solana", "sol", "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d"),49        plain("stellar", "stellar", "Public Global Stellar Network ; September 2015"),50        plain("xrpl", "xrpl", "4109C6F2045FC7EFF4CDE8F9905D19C28820D86304080FF886B299F0206E42B5"),51        plain("tron", "tron", "00000000000000001ebf88508a03865c71d452e25f4d51194196a1d22b6653dc"),52        plain("aptos", "aptos", "1"),53        .init(id: "cosmos", family: "cosmos", chainRef: "cosmoshub-4", utxo: nil, cosmos: .init(bech32: "cosmos")),54        plain("sui", "sui", "4btiuiMPvEENsttpZC7CZ53DruC3MAgfznDbASZ7DR6S"),55        plain("near", "near", "EPnLgE7iEq9s7yTkos96M3cWymH5avBAPm3qx3NXqR8H"),56        plain("ton", "ton", "17a3a92992aabea785a7a090985a265cd31f323d849da51239737e321fb05569"),57        plain("cardano", "cardano", "764824073"),58        plain("polkadot", "polkadot", "68d56f15f85d3136970ec16946040bc1752654e906147f7e43e9d539d7c3de2f"),59    ]6061    /** Exact (byte-wise) lookup: Swift's `String ==` treats canonically equivalent text as equal, which an identifier must not. */62    static func find(id: String) -> AddressParameters? { table.first { JSText.same($0.id, id) } }63}6465/**66 The few JavaScript string semantics the backend's validators rely on, reproduced on Unicode scalars. Swift's `String`67 compares by canonical equivalence ("K" == KELVIN SIGN U+212A) and groups scalars into characters; an address must be68 compared code point by code point.69 */70enum JSText {71    static func same(_ a: String, _ b: String) -> Bool { a.utf8.elementsEqual(b.utf8) }72    static func scalars(_ s: String) -> [Unicode.Scalar] { Array(s.unicodeScalars) }73    static func string(_ s: some Sequence<Unicode.Scalar>) -> String { var u = String.UnicodeScalarView(); u.append(contentsOf: s); return String(u) }7475    /** ECMAScript WhiteSpace + LineTerminator: what `String.prototype.trim` removes. */76    static func isTrimmed(_ u: Unicode.Scalar) -> Bool {77        switch u.value {78        case 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x20, 0xa0, 0x1680, 0x2000...0x200a, 0x2028, 0x2029, 0x202f, 0x205f, 0x3000, 0xfeff: true79        default: false80        }81    }82    static func trim(_ s: [Unicode.Scalar]) -> [Unicode.Scalar] {83        var lo = 0, hi = s.count84        while lo < hi, isTrimmed(s[lo]) { lo += 1 }85        while hi > lo, isTrimmed(s[hi - 1]) { hi -= 1 }86        return Array(s[lo..<hi])87    }88    /** `toLowerCase` / `toUpperCase`: full, locale-independent Unicode case mapping, scalar by scalar. */89    static func lower(_ s: [Unicode.Scalar]) -> [Unicode.Scalar] { s.flatMap { $0.isASCII ? [asciiLower($0)] : Array($0.properties.lowercaseMapping.unicodeScalars) } }90    static func upper(_ s: [Unicode.Scalar]) -> [Unicode.Scalar] { s.flatMap { $0.isASCII ? [asciiUpper($0)] : Array($0.properties.uppercaseMapping.unicodeScalars) } }91    private static func asciiLower(_ u: Unicode.Scalar) -> Unicode.Scalar { (65...90).contains(u.value) ? Unicode.Scalar(UInt8(u.value) + 32) : u }92    private static func asciiUpper(_ u: Unicode.Scalar) -> Unicode.Scalar { (97...122).contains(u.value) ? Unicode.Scalar(UInt8(u.value) - 32) : u }93    /** The bytes of an all-ASCII string, nil otherwise. */94    static func ascii(_ s: [Unicode.Scalar]) -> [UInt8]? {95        var out = [UInt8](); out.reserveCapacity(s.count)96        for u in s { guard u.isASCII else { return nil }; out.append(UInt8(u.value)) }97        return out98    }99    static func text(_ ascii: some Sequence<UInt8>) -> String { String(decoding: Array(ascii), as: UTF8.self) }100}101
Files main
Menu

Theme