|
| 1 | +#include "address_utils.hpp" |
| 2 | + |
| 3 | +#include <cstring> |
| 4 | +#include <sstream> |
| 5 | +#include <iomanip> |
| 6 | + |
| 7 | +#include <btclibs/crypto/sha256.h> |
| 8 | +#include <btclibs/bech32.h> |
| 9 | + |
| 10 | +namespace core { |
| 11 | + |
| 12 | +std::string base58check_to_hash160(const std::string& address) |
| 13 | +{ |
| 14 | + static constexpr const char* B58 = |
| 15 | + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 16 | + |
| 17 | + // decoded[0..24]: 1 version byte + 20 hash160 bytes + 4 checksum bytes |
| 18 | + uint8_t decoded[25] = {}; |
| 19 | + for (unsigned char ch : address) { |
| 20 | + const char* p = std::strchr(B58, static_cast<char>(ch)); |
| 21 | + if (!p) return ""; |
| 22 | + int carry = static_cast<int>(p - B58); |
| 23 | + for (int i = 24; i >= 0; --i) { |
| 24 | + carry += 58 * static_cast<int>(decoded[i]); |
| 25 | + decoded[i] = static_cast<uint8_t>(carry & 0xFF); |
| 26 | + carry >>= 8; |
| 27 | + } |
| 28 | + if (carry) return ""; |
| 29 | + } |
| 30 | + |
| 31 | + // Verify checksum: SHA256d(decoded[0..20])[0..4] == decoded[21..24] |
| 32 | + uint8_t tmp[32], chk[32]; |
| 33 | + CSHA256().Write(decoded, 21).Finalize(tmp); |
| 34 | + CSHA256().Write(tmp, 32).Finalize(chk); |
| 35 | + for (int i = 0; i < 4; ++i) |
| 36 | + if (chk[i] != decoded[21 + i]) return ""; |
| 37 | + |
| 38 | + static const char* HEX = "0123456789abcdef"; |
| 39 | + std::string hex; |
| 40 | + hex.reserve(40); |
| 41 | + for (int i = 1; i <= 20; ++i) { |
| 42 | + hex += HEX[decoded[i] >> 4]; |
| 43 | + hex += HEX[decoded[i] & 0x0f]; |
| 44 | + } |
| 45 | + return hex; |
| 46 | +} |
| 47 | + |
| 48 | +std::string address_to_hash160(const std::string& address, std::string& addr_type) |
| 49 | +{ |
| 50 | + addr_type.clear(); |
| 51 | + |
| 52 | + // Try Bech32 first |
| 53 | + static const std::vector<std::string> bech32_hrps = {"tltc", "ltc", "bc", "tb"}; |
| 54 | + for (const auto& hrp : bech32_hrps) { |
| 55 | + std::string prefix = hrp + "1"; |
| 56 | + if (address.size() > prefix.size() && |
| 57 | + address.substr(0, prefix.size()) == prefix) |
| 58 | + { |
| 59 | + int witver = -1; |
| 60 | + std::vector<uint8_t> prog; |
| 61 | + if (bech32::decode_segwit(hrp, address, witver, prog)) { |
| 62 | + if (witver == 0 && prog.size() == 20) { |
| 63 | + addr_type = "p2wpkh"; |
| 64 | + static const char* HEX = "0123456789abcdef"; |
| 65 | + std::string hex; |
| 66 | + hex.reserve(40); |
| 67 | + for (uint8_t b : prog) { hex += HEX[b >> 4]; hex += HEX[b & 0x0f]; } |
| 68 | + return hex; |
| 69 | + } |
| 70 | + } |
| 71 | + return ""; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Base58Check |
| 76 | + auto h160 = base58check_to_hash160(address); |
| 77 | + if (h160.size() == 40) { |
| 78 | + static constexpr const char* B58 = |
| 79 | + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 80 | + uint8_t decoded[25] = {}; |
| 81 | + for (unsigned char ch : address) { |
| 82 | + const char* p = std::strchr(B58, static_cast<char>(ch)); |
| 83 | + if (!p) return ""; |
| 84 | + int carry = static_cast<int>(p - B58); |
| 85 | + for (int i = 24; i >= 0; --i) { |
| 86 | + carry += 58 * static_cast<int>(decoded[i]); |
| 87 | + decoded[i] = static_cast<uint8_t>(carry & 0xFF); |
| 88 | + carry >>= 8; |
| 89 | + } |
| 90 | + } |
| 91 | + uint8_t version = decoded[0]; |
| 92 | + if (version == 0x32 || version == 0x05 || version == 0x3a || |
| 93 | + version == 0xc4 || version == 0x16) { |
| 94 | + addr_type = "p2sh"; |
| 95 | + } else { |
| 96 | + addr_type = "p2pkh"; |
| 97 | + } |
| 98 | + return h160; |
| 99 | + } |
| 100 | + |
| 101 | + return ""; |
| 102 | +} |
| 103 | + |
| 104 | +std::vector<unsigned char> hash160_to_merged_script( |
| 105 | + const std::string& h160_hex, const std::string& addr_type) |
| 106 | +{ |
| 107 | + if (h160_hex.size() != 40) return {}; |
| 108 | + std::vector<unsigned char> hash_bytes; |
| 109 | + hash_bytes.reserve(20); |
| 110 | + for (size_t i = 0; i < h160_hex.size(); i += 2) |
| 111 | + hash_bytes.push_back(static_cast<unsigned char>( |
| 112 | + std::stoul(h160_hex.substr(i, 2), nullptr, 16))); |
| 113 | + |
| 114 | + std::vector<unsigned char> script; |
| 115 | + if (addr_type == "p2sh") { |
| 116 | + script.reserve(23); |
| 117 | + script.push_back(0xa9); |
| 118 | + script.push_back(0x14); |
| 119 | + script.insert(script.end(), hash_bytes.begin(), hash_bytes.end()); |
| 120 | + script.push_back(0x87); |
| 121 | + } else { |
| 122 | + script.reserve(25); |
| 123 | + script.push_back(0x76); |
| 124 | + script.push_back(0xa9); |
| 125 | + script.push_back(0x14); |
| 126 | + script.insert(script.end(), hash_bytes.begin(), hash_bytes.end()); |
| 127 | + script.push_back(0x88); |
| 128 | + script.push_back(0xac); |
| 129 | + } |
| 130 | + return script; |
| 131 | +} |
| 132 | + |
| 133 | +bool is_address_for_chain(const std::string& address, |
| 134 | + const std::vector<std::string>& chain_hrps, |
| 135 | + const std::vector<uint8_t>& chain_versions) |
| 136 | +{ |
| 137 | + // Check bech32 |
| 138 | + for (const auto& hrp : chain_hrps) { |
| 139 | + std::string prefix = hrp + "1"; |
| 140 | + if (address.size() > prefix.size() && |
| 141 | + address.substr(0, prefix.size()) == prefix) |
| 142 | + return true; |
| 143 | + } |
| 144 | + // Check base58 version byte |
| 145 | + if (address.size() >= 25) { |
| 146 | + static constexpr const char* B58 = |
| 147 | + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
| 148 | + uint8_t decoded[25] = {}; |
| 149 | + bool valid = true; |
| 150 | + for (unsigned char ch : address) { |
| 151 | + const char* p = std::strchr(B58, static_cast<char>(ch)); |
| 152 | + if (!p) { valid = false; break; } |
| 153 | + int carry = static_cast<int>(p - B58); |
| 154 | + for (int i = 24; i >= 0; --i) { |
| 155 | + carry += 58 * static_cast<int>(decoded[i]); |
| 156 | + decoded[i] = static_cast<uint8_t>(carry & 0xFF); |
| 157 | + carry >>= 8; |
| 158 | + } |
| 159 | + } |
| 160 | + if (valid) { |
| 161 | + for (uint8_t ver : chain_versions) { |
| 162 | + if (decoded[0] == ver) return true; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return false; |
| 167 | +} |
| 168 | + |
| 169 | +std::vector<unsigned char> address_to_script(const std::string& address) |
| 170 | +{ |
| 171 | + // Try Bech32 first |
| 172 | + static const std::vector<std::string> bech32_hrps = {"tltc", "ltc", "bc", "tb"}; |
| 173 | + for (const auto& hrp : bech32_hrps) { |
| 174 | + std::string prefix = hrp + "1"; |
| 175 | + if (address.size() > prefix.size() && |
| 176 | + address.substr(0, prefix.size()) == prefix) |
| 177 | + { |
| 178 | + int witver = -1; |
| 179 | + std::vector<uint8_t> prog; |
| 180 | + if (bech32::decode_segwit(hrp, address, witver, prog)) { |
| 181 | + std::vector<unsigned char> script; |
| 182 | + script.push_back(static_cast<unsigned char>(witver == 0 ? 0x00 : (0x50 + witver))); |
| 183 | + script.push_back(static_cast<unsigned char>(prog.size())); |
| 184 | + script.insert(script.end(), prog.begin(), prog.end()); |
| 185 | + return script; |
| 186 | + } |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Try Base58Check (P2PKH) |
| 192 | + auto h160 = base58check_to_hash160(address); |
| 193 | + if (h160.size() == 40) { |
| 194 | + std::vector<unsigned char> script = {0x76, 0xa9, 0x14}; |
| 195 | + for (size_t i = 0; i < h160.size(); i += 2) |
| 196 | + script.push_back(static_cast<unsigned char>( |
| 197 | + std::stoul(h160.substr(i, 2), nullptr, 16))); |
| 198 | + script.push_back(0x88); |
| 199 | + script.push_back(0xac); |
| 200 | + return script; |
| 201 | + } |
| 202 | + |
| 203 | + return {}; |
| 204 | +} |
| 205 | + |
| 206 | +} // namespace core |
0 commit comments