Skip to content

Commit e222dc2

Browse files
committed
Replace ismine with producesignature check in witnessifier
Instead of using ismine to check whether an address can be spent by us, make the witness version of the script or address first and then use ProduceSignature with the DummySignatureCreator to check if we can solve for the script. Also fixes test cases to reflect this change.
1 parent 9edda0c commit e222dc2

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,11 +1067,15 @@ class Witnessifier : public boost::static_visitor<bool>
10671067
bool operator()(const CKeyID &keyID) {
10681068
if (pwallet) {
10691069
CScript basescript = GetScriptForDestination(keyID);
1070-
isminetype typ;
1071-
typ = IsMine(*pwallet, basescript, SIGVERSION_WITNESS_V0);
1072-
if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1073-
return false;
10741070
CScript witscript = GetScriptForWitness(basescript);
1071+
SignatureData sigs;
1072+
// This check is to make sure that the script we created can actually be solved for and signed by us
1073+
// if we were to have the private keys. This is just to make sure that the script is valid and that,
1074+
// if found in a transaction, we would still accept and relay that transcation.
1075+
if (!ProduceSignature(DummySignatureCreator(pwallet), witscript, sigs) ||
1076+
!VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
1077+
return false;
1078+
}
10751079
pwallet->AddCScript(witscript);
10761080
result = CScriptID(witscript);
10771081
return true;
@@ -1088,11 +1092,15 @@ class Witnessifier : public boost::static_visitor<bool>
10881092
result = scriptID;
10891093
return true;
10901094
}
1091-
isminetype typ;
1092-
typ = IsMine(*pwallet, subscript, SIGVERSION_WITNESS_V0);
1093-
if (typ != ISMINE_SPENDABLE && typ != ISMINE_WATCH_SOLVABLE)
1094-
return false;
10951095
CScript witscript = GetScriptForWitness(subscript);
1096+
SignatureData sigs;
1097+
// This check is to make sure that the script we created can actually be solved for and signed by us
1098+
// if we were to have the private keys. This is just to make sure that the script is valid and that,
1099+
// if found in a transaction, we would still accept and relay that transcation.
1100+
if (!ProduceSignature(DummySignatureCreator(pwallet), witscript, sigs) ||
1101+
!VerifyScript(sigs.scriptSig, witscript, &sigs.scriptWitness, MANDATORY_SCRIPT_VERIFY_FLAGS | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, DummySignatureCreator(pwallet).Checker())) {
1102+
return false;
1103+
}
10961104
pwallet->AddCScript(witscript);
10971105
result = CScriptID(witscript);
10981106
return true;

test/functional/segwit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,14 @@ def run_test(self):
459459
self.mine_and_test_listunspent(unsolvable_after_importaddress, 1)
460460
self.mine_and_test_listunspent(unseen_anytime, 0)
461461

462-
# addwitnessaddress should refuse to return a witness address if an uncompressed key is used or the address is
463-
# not in the wallet
462+
# addwitnessaddress should refuse to return a witness address if an uncompressed key is used
464463
# note that no witness address should be returned by unsolvable addresses
465-
# the multisig_without_privkey_address will fail because its keys were not added with importpubkey
466-
for i in uncompressed_spendable_address + uncompressed_solvable_address + unknown_address + unsolvable_address + [multisig_without_privkey_address]:
464+
for i in uncompressed_spendable_address + uncompressed_solvable_address + unknown_address + unsolvable_address:
467465
assert_raises_jsonrpc(-4, "Public key or redeemscript not known to wallet, or the key is uncompressed", self.nodes[0].addwitnessaddress, i)
468466

467+
# addwitnessaddress should return a witness addresses even if keys are not in the wallet
468+
self.nodes[0].addwitnessaddress(multisig_without_privkey_address)
469+
469470
for i in compressed_spendable_address + compressed_solvable_address:
470471
witaddress = self.nodes[0].addwitnessaddress(i)
471472
# addwitnessaddress should return the same address if it is a known P2SH-witness address
@@ -542,7 +543,7 @@ def run_test(self):
542543
# addwitnessaddress should refuse to return a witness address if an uncompressed key is used
543544
# note that a multisig address returned by addmultisigaddress is not solvable until it is added with importaddress
544545
# premature_witaddress are not accepted until the script is added with addwitnessaddress first
545-
for i in uncompressed_spendable_address + uncompressed_solvable_address + premature_witaddress + [compressed_solvable_address[1]]:
546+
for i in uncompressed_spendable_address + uncompressed_solvable_address + premature_witaddress:
546547
# This will raise an exception
547548
assert_raises_jsonrpc(-4, "Public key or redeemscript not known to wallet, or the key is uncompressed", self.nodes[0].addwitnessaddress, i)
548549

0 commit comments

Comments
 (0)