2018-07-09 04:15:50 -04:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2021-12-30 14:36:57 -03:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2018-07-09 04:15:50 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <outputtype.h>
|
|
|
|
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/script.h>
|
2019-06-06 16:52:24 -04:00
|
|
|
#include <script/sign.h>
|
|
|
|
#include <script/signingprovider.h>
|
2018-07-09 04:15:50 -04:00
|
|
|
#include <script/standard.h>
|
2019-08-28 18:12:51 -04:00
|
|
|
#include <util/vector.h>
|
2018-07-09 04:15:50 -04:00
|
|
|
|
|
|
|
#include <assert.h>
|
2021-08-04 01:38:36 -04:00
|
|
|
#include <optional>
|
2018-07-09 04:15:50 -04:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
static const std::string OUTPUT_TYPE_STRING_LEGACY = "legacy";
|
|
|
|
static const std::string OUTPUT_TYPE_STRING_P2SH_SEGWIT = "p2sh-segwit";
|
|
|
|
static const std::string OUTPUT_TYPE_STRING_BECH32 = "bech32";
|
2021-06-04 16:38:47 -04:00
|
|
|
static const std::string OUTPUT_TYPE_STRING_BECH32M = "bech32m";
|
2022-07-28 10:19:56 -04:00
|
|
|
static const std::string OUTPUT_TYPE_STRING_UNKNOWN = "unknown";
|
2018-07-09 04:15:50 -04:00
|
|
|
|
2021-08-04 01:38:36 -04:00
|
|
|
std::optional<OutputType> ParseOutputType(const std::string& type)
|
2018-07-09 04:15:50 -04:00
|
|
|
{
|
|
|
|
if (type == OUTPUT_TYPE_STRING_LEGACY) {
|
2021-08-04 01:38:36 -04:00
|
|
|
return OutputType::LEGACY;
|
2018-07-09 04:15:50 -04:00
|
|
|
} else if (type == OUTPUT_TYPE_STRING_P2SH_SEGWIT) {
|
2021-08-04 01:38:36 -04:00
|
|
|
return OutputType::P2SH_SEGWIT;
|
2018-07-09 04:15:50 -04:00
|
|
|
} else if (type == OUTPUT_TYPE_STRING_BECH32) {
|
2021-08-04 01:38:36 -04:00
|
|
|
return OutputType::BECH32;
|
2021-06-04 16:38:47 -04:00
|
|
|
} else if (type == OUTPUT_TYPE_STRING_BECH32M) {
|
2021-08-04 01:38:36 -04:00
|
|
|
return OutputType::BECH32M;
|
2022-07-28 10:19:56 -04:00
|
|
|
} else if (type == OUTPUT_TYPE_STRING_UNKNOWN) {
|
|
|
|
return OutputType::UNKNOWN;
|
2018-07-09 04:15:50 -04:00
|
|
|
}
|
2021-08-04 01:38:36 -04:00
|
|
|
return std::nullopt;
|
2018-07-09 04:15:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& FormatOutputType(OutputType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case OutputType::LEGACY: return OUTPUT_TYPE_STRING_LEGACY;
|
|
|
|
case OutputType::P2SH_SEGWIT: return OUTPUT_TYPE_STRING_P2SH_SEGWIT;
|
|
|
|
case OutputType::BECH32: return OUTPUT_TYPE_STRING_BECH32;
|
2021-06-04 16:38:47 -04:00
|
|
|
case OutputType::BECH32M: return OUTPUT_TYPE_STRING_BECH32M;
|
2022-07-28 10:19:56 -04:00
|
|
|
case OutputType::UNKNOWN: return OUTPUT_TYPE_STRING_UNKNOWN;
|
2020-06-27 11:05:03 -04:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2018-07-09 04:15:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
2019-02-19 19:00:45 -03:00
|
|
|
case OutputType::LEGACY: return PKHash(key);
|
2018-07-09 04:15:50 -04:00
|
|
|
case OutputType::P2SH_SEGWIT:
|
|
|
|
case OutputType::BECH32: {
|
2019-02-19 19:00:45 -03:00
|
|
|
if (!key.IsCompressed()) return PKHash(key);
|
2020-01-15 04:46:14 -03:00
|
|
|
CTxDestination witdest = WitnessV0KeyHash(key);
|
2018-07-09 04:15:50 -04:00
|
|
|
CScript witprog = GetScriptForDestination(witdest);
|
|
|
|
if (type == OutputType::P2SH_SEGWIT) {
|
2019-02-19 19:00:45 -03:00
|
|
|
return ScriptHash(witprog);
|
2018-07-09 04:15:50 -04:00
|
|
|
} else {
|
|
|
|
return witdest;
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 10:19:56 -04:00
|
|
|
case OutputType::BECH32M:
|
|
|
|
case OutputType::UNKNOWN: {} // This function should never be used with BECH32M or UNKNOWN, so let it assert
|
2020-06-27 11:05:03 -04:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2018-07-09 04:15:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
|
|
|
|
{
|
2019-02-19 19:00:45 -03:00
|
|
|
PKHash keyid(key);
|
2019-08-28 18:12:51 -04:00
|
|
|
CTxDestination p2pkh{keyid};
|
2018-07-09 04:15:50 -04:00
|
|
|
if (key.IsCompressed()) {
|
|
|
|
CTxDestination segwit = WitnessV0KeyHash(keyid);
|
2019-02-19 19:00:45 -03:00
|
|
|
CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit));
|
2019-08-28 18:12:51 -04:00
|
|
|
return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));
|
2018-07-09 04:15:50 -04:00
|
|
|
} else {
|
2019-08-28 18:12:51 -04:00
|
|
|
return Vector(std::move(p2pkh));
|
2018-07-09 04:15:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 10:33:23 -04:00
|
|
|
CTxDestination AddAndGetDestinationForScript(FillableSigningProvider& keystore, const CScript& script, OutputType type)
|
2018-07-09 05:30:39 -04:00
|
|
|
{
|
|
|
|
// Add script to keystore
|
|
|
|
keystore.AddCScript(script);
|
|
|
|
// Note that scripts over 520 bytes are not yet supported.
|
|
|
|
switch (type) {
|
|
|
|
case OutputType::LEGACY:
|
2020-02-04 19:27:41 -03:00
|
|
|
return ScriptHash(script);
|
2018-07-09 05:30:39 -04:00
|
|
|
case OutputType::P2SH_SEGWIT:
|
|
|
|
case OutputType::BECH32: {
|
|
|
|
CTxDestination witdest = WitnessV0ScriptHash(script);
|
|
|
|
CScript witprog = GetScriptForDestination(witdest);
|
|
|
|
// Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
|
|
|
|
keystore.AddCScript(witprog);
|
|
|
|
if (type == OutputType::BECH32) {
|
|
|
|
return witdest;
|
|
|
|
} else {
|
2020-02-04 19:27:41 -03:00
|
|
|
return ScriptHash(witprog);
|
2018-07-09 05:30:39 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 10:19:56 -04:00
|
|
|
case OutputType::BECH32M:
|
|
|
|
case OutputType::UNKNOWN: {} // This function should not be used for BECH32M or UNKNOWN, so let it assert
|
2020-06-27 11:05:03 -04:00
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
2018-07-09 05:30:39 -04:00
|
|
|
}
|
2021-06-04 17:35:47 -04:00
|
|
|
|
|
|
|
std::optional<OutputType> OutputTypeFromDestination(const CTxDestination& dest) {
|
|
|
|
if (std::holds_alternative<PKHash>(dest) ||
|
|
|
|
std::holds_alternative<ScriptHash>(dest)) {
|
|
|
|
return OutputType::LEGACY;
|
|
|
|
}
|
|
|
|
if (std::holds_alternative<WitnessV0KeyHash>(dest) ||
|
|
|
|
std::holds_alternative<WitnessV0ScriptHash>(dest)) {
|
|
|
|
return OutputType::BECH32;
|
|
|
|
}
|
|
|
|
if (std::holds_alternative<WitnessV1Taproot>(dest) ||
|
|
|
|
std::holds_alternative<WitnessUnknown>(dest)) {
|
|
|
|
return OutputType::BECH32M;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|