| 272 | }; |
| 273 | |
| 274 | CTxDestination DecodeDestination(const std::string& str, const CChainParams& params) |
| 275 | { |
| 276 | std::vector<unsigned char> data; |
| 277 | uint160 hash; |
| 278 | if (DecodeBase58Check(str, data)) { |
| 279 | // base58-encoded Bitcoin addresses. |
| 280 | // Public-key-hash-addresses have version 0 (or 111 testnet). |
| 281 | // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. |
| 282 | const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS); |
| 283 | if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) { |
| 284 | std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin()); |
| 285 | return CKeyID(hash); |
| 286 | } |
| 287 | // Script-hash-addresses have version 5 (or 196 testnet). |
| 288 | // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script. |
| 289 | const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS); |
| 290 | if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) { |
| 291 | std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin()); |
| 292 | return CScriptID(hash); |
| 293 | } |
| 294 | } |
| 295 | data.clear(); |
| 296 | auto bech = bech32::Decode(str); |
| 297 | if (bech.second.size() > 0 && bech.first == params.Bech32HRP()) { |
| 298 | // Bech32 decoding |
| 299 | int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16) |
| 300 | // The rest of the symbols are converted witness program bytes. |
| 301 | if (ConvertBits<5, 8, false>(data, bech.second.begin() + 1, bech.second.end())) { |
| 302 | if (version == 0) { |
| 303 | { |
| 304 | WitnessV0KeyHash keyid; |
| 305 | if (data.size() == keyid.size()) { |
| 306 | std::copy(data.begin(), data.end(), keyid.begin()); |
| 307 | return keyid; |
| 308 | } |
| 309 | } |
| 310 | { |
| 311 | WitnessV0ScriptHash scriptid; |
| 312 | if (data.size() == scriptid.size()) { |
| 313 | std::copy(data.begin(), data.end(), scriptid.begin()); |
| 314 | return scriptid; |
| 315 | } |
| 316 | } |
| 317 | return CNoDestination(); |
| 318 | } |
| 319 | if (version > 16 || data.size() < 2 || data.size() > 40) { |
| 320 | return CNoDestination(); |
| 321 | } |
| 322 | WitnessUnknown unk; |
| 323 | unk.version = version; |
| 324 | std::copy(data.begin(), data.end(), unk.program); |
| 325 | unk.length = data.size(); |
| 326 | return unk; |
| 327 | } |
| 328 | } |
| 329 | return CNoDestination(); |
| 330 | } |
| 331 | } // namespace |