Skip to content

Commit d58055d

Browse files
committed
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.
1 parent 9a44db2 commit d58055d

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
lines changed

src/outputtype.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,29 @@ std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
7373
}
7474
}
7575

76+
CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType type)
77+
{
78+
// Add script to keystore
79+
keystore.AddCScript(script);
80+
// Note that scripts over 520 bytes are not yet supported.
81+
switch (type) {
82+
case OutputType::LEGACY:
83+
return CScriptID(script);
84+
case OutputType::P2SH_SEGWIT:
85+
case OutputType::BECH32: {
86+
CTxDestination witdest = WitnessV0ScriptHash(script);
87+
CScript witprog = GetScriptForDestination(witdest);
88+
// Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
89+
if (!IsSolvable(keystore, witprog)) return CScriptID(script);
90+
// Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
91+
keystore.AddCScript(witprog);
92+
if (type == OutputType::BECH32) {
93+
return witdest;
94+
} else {
95+
return CScriptID(witprog);
96+
}
97+
}
98+
default: assert(false);
99+
}
100+
}
76101

src/outputtype.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_OUTPUTTYPE_H
77
#define BITCOIN_OUTPUTTYPE_H
88

9+
#include <keystore.h>
910
#include <script/standard.h>
1011

1112
#include <string>
@@ -37,5 +38,12 @@ CTxDestination GetDestinationForKey(const CPubKey& key, OutputType);
3738
/** Get all destinations (potentially) supported by the wallet for the given key. */
3839
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key);
3940

41+
/**
42+
* Get a destination of the requested type (if possible) to the specified script.
43+
* This function will automatically add the script (and any other
44+
* necessary scripts) to the keystore.
45+
*/
46+
CTxDestination AddAndGetDestinationForScript(CKeyStore& keystore, const CScript& script, OutputType);
47+
4048
#endif // BITCOIN_OUTPUTTYPE_H
4149

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,8 +1363,7 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
13631363

13641364
// Construct using pay-to-script-hash:
13651365
CScript inner = CreateMultisigRedeemscript(required, pubkeys);
1366-
pwallet->AddCScript(inner);
1367-
CTxDestination dest = pwallet->AddAndGetDestinationForScript(inner, output_type);
1366+
CTxDestination dest = AddAndGetDestinationForScript(*pwallet, inner, output_type);
13681367
pwallet->SetAddressBook(dest, label, "send");
13691368

13701369
UniValue result(UniValue::VOBJ);

src/wallet/wallet.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4470,26 +4470,3 @@ void CWallet::LearnAllRelatedScripts(const CPubKey& key)
44704470
LearnRelatedScripts(key, OutputType::P2SH_SEGWIT);
44714471
}
44724472

4473-
CTxDestination CWallet::AddAndGetDestinationForScript(const CScript& script, OutputType type)
4474-
{
4475-
// Note that scripts over 520 bytes are not yet supported.
4476-
switch (type) {
4477-
case OutputType::LEGACY:
4478-
return CScriptID(script);
4479-
case OutputType::P2SH_SEGWIT:
4480-
case OutputType::BECH32: {
4481-
CTxDestination witdest = WitnessV0ScriptHash(script);
4482-
CScript witprog = GetScriptForDestination(witdest);
4483-
// Check if the resulting program is solvable (i.e. doesn't use an uncompressed key)
4484-
if (!IsSolvable(*this, witprog)) return CScriptID(script);
4485-
// Add the redeemscript, so that P2WSH and P2SH-P2WSH outputs are recognized as ours.
4486-
AddCScript(witprog);
4487-
if (type == OutputType::BECH32) {
4488-
return witdest;
4489-
} else {
4490-
return CScriptID(witprog);
4491-
}
4492-
}
4493-
default: assert(false);
4494-
}
4495-
}

src/wallet/wallet.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,6 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
11811181
*/
11821182
void LearnAllRelatedScripts(const CPubKey& key);
11831183

1184-
/**
1185-
* Get a destination of the requested type (if possible) to the specified script.
1186-
* This function will automatically add the necessary scripts to the wallet.
1187-
*/
1188-
CTxDestination AddAndGetDestinationForScript(const CScript& script, OutputType);
1189-
11901184
/** Whether a given output is spendable by this wallet */
11911185
bool OutputEligibleForSpending(const COutput& output, const CoinEligibilityFilter& eligibility_filter) const;
11921186
};

0 commit comments

Comments
 (0)