Repository Public
1// SP Wallet Core2// File: Sources/SPWalletCore/Crypto/CashAddr.swift3// Description: CashAddr (Bitcoin Cash address format): 40-bit BCH checksum over the prefix and the payload, version byte =4// type (0 P2PKH, 1 P2SH, 2/3 token-aware) and hash size. Port of the codec in packages/core/src/plugins/bch.ts.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public enum CashAddr {11 public struct Decoded: Sendable, Equatable {12 public let prefix: String13 /** 0 = P2PKH, 1 = P2SH; 2 and 3 are the token-aware forms. */14 public let type: UInt815 public let hash: [UInt8]16 }1718 private static let hashSizes = [20, 24, 28, 32, 40, 48, 56, 64]19 private static let generators: [UInt64] = [0x98_f2bc_8e61, 0x79_b76d_99e2, 0xf3_3e5f_b3c4, 0xae_2eab_e2a8, 0x1e_4f43_e470]2021 private static func polymod(_ values: [UInt8]) -> UInt64 {22 var c: UInt64 = 123 for d in values {24 let c0 = c >> 3525 c = ((c & 0x07_ffff_ffff) << 5) ^ UInt64(d)26 for k in 0..<5 where (c0 >> UInt64(k)) & 1 == 1 { c ^= generators[k] }27 }28 return c ^ 129 }3031 private static func prefixValues(_ prefix: [UInt8]) -> [UInt8] { prefix.map { $0 & 0x1f } + [0] }32 private static func isLowerAlpha(_ prefix: [UInt8]) -> Bool { !prefix.isEmpty && prefix.allSatisfy { $0 >= 97 && $0 <= 122 } }3334 /** "prefix:payload" in lower case; nil when the prefix is not lower-case letters, the type is above 15 or the hash size is not one of the eight CashAddr sizes. */35 public static func encode(prefix: String, type: UInt8, hash: [UInt8]) -> String? {36 let p = [UInt8](prefix.utf8)37 guard let size = hashSizes.firstIndex(of: hash.count), type <= 15, isLowerAlpha(p), prefix.unicodeScalars.allSatisfy({ $0.isASCII }) else { return nil }38 let payload = Bech32.toWords([(type << 3) | UInt8(size)] + hash)39 let mod = polymod(prefixValues(p) + payload + [UInt8](repeating: 0, count: 8))40 let check = (0..<8).map { UInt8((mod >> UInt64(5 * (7 - $0))) & 31) }41 return String(decoding: p + [0x3a] + (payload + check).map { Bech32.charset[Int($0)] }, as: UTF8.self)42 }4344 /**45 Decode and verify the checksum. A string without "prefix:" is checked against `defaultPrefix` (the prefix is part of the46 checksum). All-upper-case input is accepted; mixed case is not.47 */48 public static func decode(_ text: String, defaultPrefix: String? = nil) -> Decoded? {49 guard text.unicodeScalars.allSatisfy({ $0.isASCII }) else { return nil }50 return decode(ascii: [UInt8](text.utf8), defaultPrefix: defaultPrefix)51 }5253 static func decode(ascii: [UInt8], defaultPrefix: String?) -> Decoded? {54 let hasUpper = ascii.contains { $0 >= 65 && $0 <= 90 }, hasLower = ascii.contains { $0 >= 97 && $0 <= 122 }55 guard !(hasUpper && hasLower) else { return nil }56 let lower = ascii.map { $0 >= 65 && $0 <= 90 ? $0 + 32 : $0 }57 let parts = lower.split(separator: 0x3a, omittingEmptySubsequences: false)58 guard parts.count <= 2, let last = parts.last else { return nil }59 let prefix: [UInt8]60 if parts.count == 2 { prefix = [UInt8](parts[0]) } else if let d = defaultPrefix { prefix = [UInt8](d.utf8) } else { return nil }61 let body = [UInt8](last)62 guard isLowerAlpha(prefix), body.count >= 42, body.count <= 112 else { return nil }63 var values = [UInt8](); values.reserveCapacity(body.count)64 for c in body { guard let v = Bech32.value(of: c) else { return nil }; values.append(v) }65 guard polymod(prefixValues(prefix) + values) == 0, let bytes = Bech32.fromWords(values.dropLast(8)), let version = bytes.first,66 version & 0x80 == 0, bytes.count - 1 == hashSizes[Int(version & 7)] else { return nil }67 return Decoded(prefix: String(decoding: prefix, as: UTF8.self), type: version >> 3, hash: [UInt8](bytes.dropFirst()))68 }69}70