Bitcoin Core 29.99.0
P2P Digital Currency
outputtype.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <outputtype.h>
7
8#include <pubkey.h>
9#include <script/script.h>
10#include <script/sign.h>
12
13#include <cassert>
14#include <optional>
15#include <string>
16
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";
22
23std::optional<OutputType> ParseOutputType(const std::string& type)
24{
25 if (type == OUTPUT_TYPE_STRING_LEGACY) {
26 return OutputType::LEGACY;
27 } else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
29 } else if (type == OUTPUT_TYPE_STRING_BECH32) {
30 return OutputType::BECH32;
31 } else if (type == OUTPUT_TYPE_STRING_BECH32M) {
33 }
34 return std::nullopt;
35}
36
37const std::string& FormatOutputType(OutputType type)
38{
39 switch (type) {
45 } // no default case, so the compiler can warn about missing cases
46 assert(false);
47}
48
50{
51 // Add script to keystore
52 keystore.scripts.emplace(CScriptID(script), script);
53
54 switch (type) {
56 return ScriptHash(script);
58 case OutputType::BECH32: {
60 CScript witprog = GetScriptForDestination(witdest);
61 // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
62 keystore.scripts.emplace(CScriptID(witprog), witprog);
63 if (type == OutputType::BECH32) {
64 return witdest;
65 } else {
66 return ScriptHash(witprog);
67 }
68 }
70 case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
71 } // no default case, so the compiler can warn about missing cases
72 assert(false);
73}
74
75std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
76 if (std::holds_alternative<PKHash>(dest) ||
77 std::holds_alternative<ScriptHash>(dest)) {
78 return OutputType::LEGACY;
79 }
80 if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
81 std::holds_alternative<WitnessV0ScriptHash>(dest)) {
82 return OutputType::BECH32;
83 }
84 if (std::holds_alternative<WitnessV1Taproot>(dest) ||
85 std::holds_alternative<WitnessUnknown>(dest)) {
87 }
88 return std::nullopt;
89}
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:143
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:604
static const std::string OUTPUT_TYPE_STRING_BECH32
Definition: outputtype.cpp:19
CTxDestination AddAndGetDestinationForScript(FlatSigningProvider &keystore, const CScript &script, OutputType type)
Get a destination of the requested type (if possible) to the specified script.
Definition: outputtype.cpp:49
static const std::string OUTPUT_TYPE_STRING_BECH32M
Definition: outputtype.cpp:20
std::optional< OutputType > OutputTypeFromDestination(const CTxDestination &dest)
Get the OutputType for a CTxDestination.
Definition: outputtype.cpp:75
static const std::string OUTPUT_TYPE_STRING_LEGACY
Definition: outputtype.cpp:17
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT
Definition: outputtype.cpp:18
std::optional< OutputType > ParseOutputType(const std::string &type)
Definition: outputtype.cpp:23
const std::string & FormatOutputType(OutputType type)
Definition: outputtype.cpp:37
static const std::string OUTPUT_TYPE_STRING_UNKNOWN
Definition: outputtype.cpp:21
OutputType
Definition: outputtype.h:17
std::map< CScriptID, CScript > scripts
assert(!tx.IsCoinBase())