From d58055d25f41e942e04ffeae5f25e37a60ee8829 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Mon, 9 Jul 2018 19:30:39 +1000 Subject: [PATCH] Move AddAndGetDestinationForScript from wallet to outputype module Makes AddAndGetDestinationForScript use a generic CKeyStore rather than the wallet, and makes it always add the script to the keystore, rather than only adding related (redeem) scripts. --- src/outputtype.cpp | 25 +++++++++++++++++++++++++ src/outputtype.h | 8 ++++++++ src/wallet/rpcwallet.cpp | 3 +-- src/wallet/wallet.cpp | 23 ----------------------- src/wallet/wallet.h | 6 ------ 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/outputtype.cpp b/src/outputtype.cpp index 025d8a96fd..3ff28bf9c2 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -73,4 +73,29 @@ std::vector GetAllDestinationsForKey(const CPubKey& key) } } +CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType type) +{ + // Add script to keystore + keystore.AddCScript(script); + // Note that scripts over 520 bytes are not yet supported. + switch (type) { + case OutputType::LEGACY: + return CScriptID(script); + case OutputType::P2SH_SEGWIT: + case OutputType::BECH32: { + CTxDestination witdest = WitnessV0ScriptHash(script); + CScript witprog = GetScriptForDestination(witdest); + // Check if the resulting program is solvable (i.e. doesn't use an uncompressed key) + if (!IsSolvable(keystore, witprog)) return CScriptID(script); + // Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours. + keystore.AddCScript(witprog); + if (type == OutputType::BECH32) { + return witdest; + } else { + return CScriptID(witprog); + } + } + default: assert(false); + } +} diff --git a/src/outputtype.h b/src/outputtype.h index 0c55ac9b18..21623e3b49 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_OUTPUTTYPE_H #define BITCOIN_OUTPUTTYPE_H +#include #include