Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/psbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
}
}
m_proprietary.insert(psbt.m_proprietary.begin(), psbt.m_proprietary.end());
unknown.insert(psbt.unknown.begin(), psbt.unknown.end());

return true;
Expand Down Expand Up @@ -225,6 +226,7 @@ void PSBTInput::Merge(const PSBTInput& input)
hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
m_proprietary.insert(input.m_proprietary.begin(), input.m_proprietary.end());
unknown.insert(input.unknown.begin(), input.unknown.end());
m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
Expand Down Expand Up @@ -307,6 +309,7 @@ bool PSBTOutput::IsNull() const
void PSBTOutput::Merge(const PSBTOutput& output)
{
hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
m_proprietary.insert(output.m_proprietary.begin(), output.m_proprietary.end());
unknown.insert(output.unknown.begin(), output.unknown.end());
m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());

Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ add_executable(test_bitcoin
orphanage_tests.cpp
pcp_tests.cpp
peerman_tests.cpp
psbt_tests.cpp
pmt_tests.cpp
policyestimator_tests.cpp
pool_tests.cpp
Expand Down
65 changes: 65 additions & 0 deletions src/test/psbt_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2026-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.

#include <psbt.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(psbt_tests, BasicTestingSetup)

static PSBTProprietary MakeProprietary(uint8_t scope_type, uint8_t unique_tag, uint8_t value_tag)
{
return PSBTProprietary{
.subtype = unique_tag,
.identifier = {'p', 's', 'b', 't'},
.key = {scope_type, unique_tag},
.value = {value_tag},
};
}

BOOST_AUTO_TEST_CASE(merge_proprietary_fields)
{
CMutableTransaction tx;
tx.vin.emplace_back(COutPoint{});
tx.vout.emplace_back(0, CScript{});

PartiallySignedTransaction left(tx);
PartiallySignedTransaction right(tx);

const auto global_left = MakeProprietary(PSBT_GLOBAL_PROPRIETARY, 1, 0xaa);
const auto global_right = MakeProprietary(PSBT_GLOBAL_PROPRIETARY, 2, 0xbb);
const auto input_left = MakeProprietary(PSBT_IN_PROPRIETARY, 3, 0xcc);
const auto input_right = MakeProprietary(PSBT_IN_PROPRIETARY, 4, 0xdd);
const auto output_left = MakeProprietary(PSBT_OUT_PROPRIETARY, 5, 0xee);
const auto output_right = MakeProprietary(PSBT_OUT_PROPRIETARY, 6, 0xff);

left.m_proprietary.insert(global_left);
left.inputs[0].m_proprietary.insert(input_left);
left.outputs[0].m_proprietary.insert(output_left);

right.m_proprietary.insert(global_right);
right.inputs[0].m_proprietary.insert(input_right);
right.outputs[0].m_proprietary.insert(output_right);

BOOST_REQUIRE(left.Merge(right));

BOOST_REQUIRE_EQUAL(left.m_proprietary.size(), 2U);
BOOST_REQUIRE_EQUAL(left.inputs[0].m_proprietary.size(), 2U);
BOOST_REQUIRE_EQUAL(left.outputs[0].m_proprietary.size(), 2U);

const auto global_it = left.m_proprietary.find(global_right);
BOOST_REQUIRE(global_it != left.m_proprietary.end());
BOOST_CHECK(global_it->value == global_right.value);

const auto input_it = left.inputs[0].m_proprietary.find(input_right);
BOOST_REQUIRE(input_it != left.inputs[0].m_proprietary.end());
BOOST_CHECK(input_it->value == input_right.value);

const auto output_it = left.outputs[0].m_proprietary.find(output_right);
BOOST_REQUIRE(output_it != left.outputs[0].m_proprietary.end());
BOOST_CHECK(output_it->value == output_right.value);
}

BOOST_AUTO_TEST_SUITE_END()
62 changes: 62 additions & 0 deletions test/functional/rpc_psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
CTxOut,
MAX_BIP125_RBF_SEQUENCE,
WITNESS_SCALE_FACTOR,
ser_compact_size,
)
from test_framework.psbt import (
PSBT,
PSBTMap,
PSBT_GLOBAL_PROPRIETARY,
PSBT_GLOBAL_UNSIGNED_TX,
PSBT_IN_RIPEMD160,
PSBT_IN_SHA256,
Expand All @@ -34,8 +36,10 @@
PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS,
PSBT_IN_MUSIG2_PUB_NONCE,
PSBT_IN_NON_WITNESS_UTXO,
PSBT_IN_PROPRIETARY,
PSBT_IN_WITNESS_UTXO,
PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS,
PSBT_OUT_PROPRIETARY,
PSBT_OUT_TAP_TREE,
)
from test_framework.script import CScript, OP_TRUE, SIGHASH_ALL, SIGHASH_ANYONECANPAY
Expand Down Expand Up @@ -1170,6 +1174,64 @@ def test_psbt_input_keys(psbt_input, keys):

self.test_decodepsbt_musig2_input_output_types()

self.log.info("Test that combining PSBTs preserves proprietary fields")

def proprietary_key(type_byte, identifier, subtype, key_data=b""):
return bytes([type_byte]) + ser_compact_size(len(identifier)) + identifier + ser_compact_size(subtype) + key_data

def proprietary_entry(key, value, identifier, subtype):
return {"identifier": identifier.hex(), "subtype": subtype, "key": key.hex(), "value": value.hex()}

tx = CTransaction()
tx.vin = [CTxIn(outpoint=COutPoint(hash=int('aa' * 32, 16), n=0), scriptSig=b"")]
tx.vout = [CTxOut(nValue=0, scriptPubKey=b"")]

global_key_a = proprietary_key(PSBT_GLOBAL_PROPRIETARY, b"gc", 1, b"\x01")
global_key_b = proprietary_key(PSBT_GLOBAL_PROPRIETARY, b"gc", 2, b"\x02")
input_key_a = proprietary_key(PSBT_IN_PROPRIETARY, b"in", 3, b"\x03")
input_key_b = proprietary_key(PSBT_IN_PROPRIETARY, b"in", 4, b"\x04")
output_key_a = proprietary_key(PSBT_OUT_PROPRIETARY, b"out", 5, b"\x05")
output_key_b = proprietary_key(PSBT_OUT_PROPRIETARY, b"out", 6, b"\x06")

psbt1 = PSBT(
g=PSBTMap({
PSBT_GLOBAL_UNSIGNED_TX: tx.serialize(),
global_key_a: b"\xaa",
}),
i=[PSBTMap({
input_key_a: b"\xbb",
})],
o=[PSBTMap({
output_key_a: b"\xcc",
})],
).to_base64()
psbt2 = PSBT(
g=PSBTMap({
PSBT_GLOBAL_UNSIGNED_TX: tx.serialize(),
global_key_b: b"\xdd",
}),
i=[PSBTMap({
input_key_b: b"\xee",
})],
o=[PSBTMap({
output_key_b: b"\xff",
})],
).to_base64()

decoded = self.nodes[0].decodepsbt(self.nodes[0].combinepsbt([psbt1, psbt2]))
assert_equal(decoded["proprietary"], [
proprietary_entry(global_key_a, b"\xaa", b"gc", 1),
proprietary_entry(global_key_b, b"\xdd", b"gc", 2),
])
assert_equal(decoded["inputs"][0]["proprietary"], [
proprietary_entry(input_key_a, b"\xbb", b"in", 3),
proprietary_entry(input_key_b, b"\xee", b"in", 4),
])
assert_equal(decoded["outputs"][0]["proprietary"], [
proprietary_entry(output_key_a, b"\xcc", b"out", 5),
proprietary_entry(output_key_b, b"\xff", b"out", 6),
])

self.log.info("Test that combining PSBTs with different transactions fails")
tx = CTransaction()
tx.vin = [CTxIn(outpoint=COutPoint(hash=int('aa' * 32, 16), n=0), scriptSig=b"")]
Expand Down
Loading