|
| 1 | +import LDKNode |
| 2 | + |
| 3 | +extension LDKNode.AddressType { |
| 4 | + // MARK: - All cases (ordered) |
| 5 | + |
| 6 | + static let allAddressTypes: [LDKNode.AddressType] = [.legacy, .nestedSegwit, .nativeSegwit, .taproot] |
| 7 | + |
| 8 | + /// All address types with `selected` first, remaining in standard order. |
| 9 | + static func prioritized(selected: LDKNode.AddressType) -> [LDKNode.AddressType] { |
| 10 | + var types = [selected] |
| 11 | + for type in allAddressTypes where type != selected { |
| 12 | + types.append(type) |
| 13 | + } |
| 14 | + return types |
| 15 | + } |
| 16 | + |
| 17 | + // MARK: - Storage string (UserDefaults / BitkitCore APIs) |
| 18 | + |
| 19 | + /// String value used in UserDefaults and BitkitCore APIs. |
| 20 | + var stringValue: String { |
| 21 | + switch self { |
| 22 | + case .legacy: return "legacy" |
| 23 | + case .nestedSegwit: return "nestedSegwit" |
| 24 | + case .nativeSegwit: return "nativeSegwit" |
| 25 | + case .taproot: return "taproot" |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Parses storage string; returns nil for invalid or unknown values. |
| 30 | + static func from(string: String) -> LDKNode.AddressType? { |
| 31 | + switch string { |
| 32 | + case "legacy": return .legacy |
| 33 | + case "nestedSegwit": return .nestedSegwit |
| 34 | + case "nativeSegwit": return .nativeSegwit |
| 35 | + case "taproot": return .taproot |
| 36 | + default: return nil |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Parses storage string; returns `.nativeSegwit` for nil or invalid (backward compatibility). |
| 41 | + static func fromStorage(_ string: String?) -> LDKNode.AddressType { |
| 42 | + guard let s = string, let type = from(string: s) else { return .nativeSegwit } |
| 43 | + return type |
| 44 | + } |
| 45 | + |
| 46 | + /// Parses a comma-separated string of address types; filters invalid values. |
| 47 | + static func parseCommaSeparated(_ string: String) -> [LDKNode.AddressType] { |
| 48 | + string.split(separator: ",") |
| 49 | + .map { String($0).trimmingCharacters(in: .whitespaces) } |
| 50 | + .compactMap { from(string: $0) } |
| 51 | + } |
| 52 | + |
| 53 | + // MARK: - Derivation path |
| 54 | + |
| 55 | + /// BIP derivation path using current network (Env.network) for coin type. |
| 56 | + var derivationPath: String { |
| 57 | + let coinType = Env.network == .bitcoin ? "0" : "1" |
| 58 | + return derivationPath(coinType: coinType) |
| 59 | + } |
| 60 | + |
| 61 | + /// BIP derivation path for the given coin type ("0" mainnet, "1" testnet). |
| 62 | + func derivationPath(coinType: String) -> String { |
| 63 | + switch self { |
| 64 | + case .legacy: return "m/44'/\(coinType)'/0'/0" // BIP 44 |
| 65 | + case .nestedSegwit: return "m/49'/\(coinType)'/0'/0" // BIP 49 |
| 66 | + case .nativeSegwit: return "m/84'/\(coinType)'/0'/0" // BIP 84 |
| 67 | + case .taproot: return "m/86'/\(coinType)'/0'/0" // BIP 86 |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: - Localized display |
| 72 | + |
| 73 | + var localizedTitle: String { |
| 74 | + switch self { |
| 75 | + case .legacy: return "Legacy" |
| 76 | + case .nestedSegwit: return "Nested Segwit" |
| 77 | + case .nativeSegwit: return "Native Segwit" |
| 78 | + case .taproot: return "Taproot" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + var localizedDescription: String { |
| 83 | + switch self { |
| 84 | + case .legacy: return "Pay-to-public-key-hash (1x...)" |
| 85 | + case .nestedSegwit: return "Pay-to-Script-Hash (3x...)" |
| 86 | + case .nativeSegwit: return "Pay-to-witness-public-key-hash (bc1x...)" |
| 87 | + case .taproot: return "Pay-to-Taproot (bc1px...)" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + var example: String { |
| 92 | + switch self { |
| 93 | + case .legacy: return "(1x...)" |
| 94 | + case .nestedSegwit: return "(3x...)" |
| 95 | + case .nativeSegwit: return "(bc1x...)" |
| 96 | + case .taproot: return "(bc1px...)" |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + var shortExample: String { |
| 101 | + switch self { |
| 102 | + case .legacy: return "1x..." |
| 103 | + case .nestedSegwit: return "3x..." |
| 104 | + case .nativeSegwit: return "bc1q..." |
| 105 | + case .taproot: return "bc1p..." |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// Accessibility / UI test identifier. |
| 110 | + var testId: String { |
| 111 | + switch self { |
| 112 | + case .legacy: return "p2pkh" |
| 113 | + case .nestedSegwit: return "p2sh-p2wpkh" |
| 114 | + case .nativeSegwit: return "p2wpkh" |
| 115 | + case .taproot: return "p2tr" |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // MARK: - Address format validation |
| 120 | + |
| 121 | + /// Returns true if the address has the expected prefix for this address type on the given network. |
| 122 | + /// Defensive check only; not a full script/checksum validation. |
| 123 | + func matchesAddressFormat(_ address: String, network: LDKNode.Network) -> Bool { |
| 124 | + let trimmed = address.trimmingCharacters(in: .whitespaces) |
| 125 | + guard !trimmed.isEmpty else { return false } |
| 126 | + let isMainnet = network == .bitcoin |
| 127 | + switch self { |
| 128 | + case .legacy: |
| 129 | + return isMainnet ? trimmed.hasPrefix("1") : trimmed.hasPrefix("m") || trimmed.hasPrefix("n") |
| 130 | + case .nestedSegwit: |
| 131 | + return isMainnet ? trimmed.hasPrefix("3") : trimmed.hasPrefix("2") |
| 132 | + case .nativeSegwit: |
| 133 | + return isMainnet ? trimmed.hasPrefix("bc1q") : trimmed.hasPrefix("tb1q") || trimmed.hasPrefix("bcrt1q") |
| 134 | + case .taproot: |
| 135 | + return isMainnet ? trimmed.hasPrefix("bc1p") : trimmed.hasPrefix("tb1p") || trimmed.hasPrefix("bcrt1p") |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments