| 71 | }; |
| 72 | |
| 73 | CTxDestination DecodeDestination(const std::string& str, const CChainParams& params) |
| 74 | { |
| 75 | std::vector<unsigned char> data; |
| 76 | uint160 hash; |
| 77 | if (DecodeBase58Check(str, data)) { |
| 78 | // base58-encoded Bitcoin addresses. |
| 79 | // Public-key-hash-addresses have version 0 (or 111 testnet). |
| 80 | // The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key. |
| 81 | const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS); |
| 82 | if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) { |
| 83 | std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin()); |
| 84 | return CKeyID(hash); |
| 85 | } |
| 86 | // Script-hash-addresses have version 5 (or 196 testnet). |
| 87 | // The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script. |
| 88 | const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS); |
| 89 | if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) { |
| 90 | std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin()); |
| 91 | return CScriptID(hash); |
| 92 | } |
| 93 | } |
| 94 | data.clear(); |
| 95 | auto bech = bech32::Decode(str); |
| 96 | if (bech.second.size() > 0 && bech.first == params.Bech32HRP()) { |
| 97 | // Bech32 decoding |
| 98 | int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16) |
| 99 | // The rest of the symbols are converted witness program bytes. |
| 100 | data.reserve(((bech.second.size() - 1) * 5) / 8); |
| 101 | if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) { |
| 102 | if (version == 0) { |
| 103 | { |
| 104 | WitnessV0KeyHash keyid; |
| 105 | if (data.size() == keyid.size()) { |
| 106 | std::copy(data.begin(), data.end(), keyid.begin()); |
| 107 | return keyid; |
| 108 | } |
| 109 | } |
| 110 | { |
| 111 | WitnessV0ScriptHash scriptid; |
| 112 | if (data.size() == scriptid.size()) { |
| 113 | std::copy(data.begin(), data.end(), scriptid.begin()); |
| 114 | return scriptid; |
| 115 | } |
| 116 | } |
| 117 | return CNoDestination(); |
| 118 | } |
| 119 | if (version > 16 || data.size() < 2 || data.size() > 40) { |
| 120 | return CNoDestination(); |
| 121 | } |
| 122 | WitnessUnknown unk; |
| 123 | unk.version = version; |
| 124 | std::copy(data.begin(), data.end(), unk.program); |
| 125 | unk.length = data.size(); |
| 126 | return unk; |
| 127 | } |
| 128 | } |
| 129 | return CNoDestination(); |
| 130 | } |