Skip to content

Commit 6c7a17a

Browse files
darosiorachow101
andcommitted
psbt: support externally provided preimages for Miniscript satisfaction
Co-Authored-By: Andrew Chow <[email protected]>
1 parent 840a396 commit 6c7a17a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/psbt.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ void PSBTInput::FillSignatureData(SignatureData& sigdata) const
132132
for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
133133
sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
134134
}
135+
for (const auto& [hash, preimage] : ripemd160_preimages) {
136+
sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
137+
}
138+
for (const auto& [hash, preimage] : sha256_preimages) {
139+
sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
140+
}
141+
for (const auto& [hash, preimage] : hash160_preimages) {
142+
sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
143+
}
144+
for (const auto& [hash, preimage] : hash256_preimages) {
145+
sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
146+
}
135147
}
136148

137149
void PSBTInput::FromSignatureData(const SignatureData& sigdata)

test/functional/wallet_miniscript.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Test Miniscript descriptors integration in the wallet."""
66

77
from test_framework.descriptors import descsum_create
8+
from test_framework.psbt import PSBT, PSBT_IN_SHA256
89
from test_framework.test_framework import BitcoinTestFramework
910
from test_framework.util import assert_equal
1011

@@ -60,6 +61,17 @@
6061
"sigs_count": 3,
6162
"stack_size": 5,
6263
},
64+
# The same policy but we provide the preimage. This path will be chosen as it's a smaller witness.
65+
{
66+
"ms": f"andor(ndv:older(2),and_v(v:pk({TPRVS[0]}),sha256(61e33e9dbfefc45f6a194187684d278f789fd4d5e207a357e79971b6519a8b12)),and_v(v:pkh({TPRVS[1]}),pk({TPRVS[2]}/*)))",
67+
"sequence": 2,
68+
"locktime": None,
69+
"sigs_count": 3,
70+
"stack_size": 4,
71+
"sha256_preimages": {
72+
"61e33e9dbfefc45f6a194187684d278f789fd4d5e207a357e79971b6519a8b12": "e8774f330f5f330c23e8bbefc5595cb87009ddb7ac3b8deaaa8e9e41702d919c"
73+
},
74+
},
6375
# Signature with a relative timelock
6476
{
6577
"ms": f"and_v(v:older(2),pk({TPRVS[0]}/*))",
@@ -167,7 +179,9 @@ def watchonly_test(self, ms):
167179
utxo = self.ms_wo_wallet.listunspent(minconf=0, addresses=[addr])[0]
168180
assert utxo["txid"] == txid and utxo["solvable"]
169181

170-
def signing_test(self, ms, sequence, locktime, sigs_count, stack_size):
182+
def signing_test(
183+
self, ms, sequence, locktime, sigs_count, stack_size, sha256_preimages
184+
):
171185
self.log.info(f"Importing private Miniscript '{ms}'")
172186
desc = descsum_create(f"wsh({ms})")
173187
res = self.ms_sig_wallet.importdescriptors(
@@ -208,6 +222,12 @@ def signing_test(self, ms, sequence, locktime, sigs_count, stack_size):
208222
)
209223

210224
self.log.info("Signing it and checking the satisfaction.")
225+
if sha256_preimages is not None:
226+
psbt = PSBT.from_base64(psbt)
227+
for (h, preimage) in sha256_preimages.items():
228+
k = PSBT_IN_SHA256.to_bytes(1, "big") + bytes.fromhex(h)
229+
psbt.i[0].map[k] = bytes.fromhex(preimage)
230+
psbt = psbt.to_base64()
211231
res = self.ms_sig_wallet.walletprocesspsbt(psbt=psbt, finalize=False)
212232
psbtin = self.nodes[0].rpc.decodepsbt(res["psbt"])["inputs"][0]
213233
assert len(psbtin["partial_signatures"]) == sigs_count
@@ -258,14 +278,15 @@ def run_test(self):
258278
for ms in MINISCRIPTS:
259279
self.watchonly_test(ms)
260280

261-
# Test we can sign most Miniscript (all but ones requiring preimages, for now)
281+
# Test we can sign for any Miniscript.
262282
for ms in MINISCRIPTS_PRIV:
263283
self.signing_test(
264284
ms["ms"],
265285
ms["sequence"],
266286
ms["locktime"],
267287
ms["sigs_count"],
268288
ms["stack_size"],
289+
ms.get("sha256_preimages"),
269290
)
270291

271292

0 commit comments

Comments
 (0)