| 2223 | } |
| 2224 | |
| 2225 | OutputType CWallet::TransactionChangeType(const std::optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const |
| 2226 | { |
| 2227 | // If -changetype is specified, always use that change type. |
| 2228 | if (change_type) { |
| 2229 | return *change_type; |
| 2230 | } |
| 2231 | |
| 2232 | // if m_default_address_type is legacy, use legacy address as change. |
| 2233 | if (m_default_address_type == OutputType::LEGACY) { |
| 2234 | return OutputType::LEGACY; |
| 2235 | } |
| 2236 | |
| 2237 | bool any_tr{false}; |
| 2238 | bool any_wpkh{false}; |
| 2239 | bool any_sh{false}; |
| 2240 | bool any_pkh{false}; |
| 2241 | |
| 2242 | for (const auto& recipient : vecSend) { |
| 2243 | std::vector<std::vector<uint8_t>> dummy; |
| 2244 | const TxoutType type{Solver(recipient.scriptPubKey, dummy)}; |
| 2245 | if (type == TxoutType::WITNESS_V1_TAPROOT) { |
| 2246 | any_tr = true; |
| 2247 | } else if (type == TxoutType::WITNESS_V0_KEYHASH) { |
| 2248 | any_wpkh = true; |
| 2249 | } else if (type == TxoutType::SCRIPTHASH) { |
| 2250 | any_sh = true; |
| 2251 | } else if (type == TxoutType::PUBKEYHASH) { |
| 2252 | any_pkh = true; |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | const bool has_bech32m_spkman(GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/true)); |
| 2257 | if (has_bech32m_spkman && any_tr) { |
| 2258 | // Currently tr is the only type supported by the BECH32M spkman |
| 2259 | return OutputType::BECH32M; |
| 2260 | } |
| 2261 | const bool has_bech32_spkman(GetScriptPubKeyMan(OutputType::BECH32, /*internal=*/true)); |
| 2262 | if (has_bech32_spkman && any_wpkh) { |
| 2263 | // Currently wpkh is the only type supported by the BECH32 spkman |
| 2264 | return OutputType::BECH32; |
| 2265 | } |
| 2266 | const bool has_p2sh_segwit_spkman(GetScriptPubKeyMan(OutputType::P2SH_SEGWIT, /*internal=*/true)); |
| 2267 | if (has_p2sh_segwit_spkman && any_sh) { |
| 2268 | // Currently sh_wpkh is the only type supported by the P2SH_SEGWIT spkman |
| 2269 | // As of 2021 about 80% of all SH are wrapping WPKH, so use that |
| 2270 | return OutputType::P2SH_SEGWIT; |
| 2271 | } |
| 2272 | const bool has_legacy_spkman(GetScriptPubKeyMan(OutputType::LEGACY, /*internal=*/true)); |
| 2273 | if (has_legacy_spkman && any_pkh) { |
| 2274 | // Currently pkh is the only type supported by the LEGACY spkman |
| 2275 | return OutputType::LEGACY; |
| 2276 | } |
| 2277 | |
| 2278 | if (has_bech32m_spkman) { |
| 2279 | return OutputType::BECH32M; |
| 2280 | } |
| 2281 | if (has_bech32_spkman) { |
| 2282 | return OutputType::BECH32; |