Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <outputtype.h>8#include <pubkey.h>9#include <script/script.h>10#include <script/sign.h>11#include <script/signingprovider.h>13#include <cassert>14#include <optional>15#include <string>17static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";18static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";19static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";20static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";21static const std::string OUTPUT_TYPE_STRING_UNKNOWN = "unknown";23std::optional<OutputType> ParseOutputType(std::string_view type)24{25if (type == OUTPUT_TYPE_STRING_LEGACY) {26return OutputType::LEGACY;27} else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {28return OutputType::P2SH_SEGWIT;29} else if (type == OUTPUT_TYPE_STRING_BECH32) {30return OutputType::BECH32;31} else if (type == OUTPUT_TYPE_STRING_BECH32M) {32return OutputType::BECH32M;33}34return std::nullopt;35}37const std::string& FormatOutputType(OutputType type)38{39switch (type) {40case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;41case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;42case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;43case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;44case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;45} // no default case, so the compiler can warn about missing cases46assert(false);47}49std::string FormatAllOutputTypes()50{51return util::Join(OUTPUT_TYPES, ", ", [](const auto& i) { return "\"" + FormatOutputType(i) + "\""; });52}54CTxDestination AddAndGetDestinationForScript(FlatSigningProvider& keystore, const CScript& script, OutputType type)55{56// Add script to keystore57keystore.scripts.emplace(CScriptID(script), script);59switch (type) {60case OutputType::LEGACY:61return ScriptHash(script);62case OutputType::P2SH_SEGWIT:63case OutputType::BECH32: {64CTxDestination witdest = WitnessV0ScriptHash(script);65CScript witprog = GetScriptForDestination(witdest);66// Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.67keystore.scripts.emplace(CScriptID(witprog), witprog);68if (type == OutputType::BECH32) {69return witdest;70} else {71return ScriptHash(witprog);72}73}74case OutputType::BECH32M:75case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert76} // no default case, so the compiler can warn about missing cases77assert(false);78}80std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {81if (std::holds_alternative<PKHash>(dest) ||82std::holds_alternative<ScriptHash>(dest)) {83return OutputType::LEGACY;84}85if (std::holds_alternative<WitnessV0KeyHash>(dest) ||86std::holds_alternative<WitnessV0ScriptHash>(dest)) {87return OutputType::BECH32;88}89if (std::holds_alternative<WitnessV1Taproot>(dest) ||90std::holds_alternative<WitnessUnknown>(dest)) {91return OutputType::BECH32M;92}93return std::nullopt;94}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.