mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-30 15:54:03 +02:00
Merge bitcoin/bitcoin#21283: Implement BIP 370 PSBTv2
9fa4076b20test: Test merging implicit PSBTv0 with explicit PSBTv0 (w0xlt)1660c18232doc: Release notes for psbtv2 (Ava Chow)470e52a5f8fuzz: Enforce additional version invariants in PSBT fuzzer (Antoine Poinsot)5bd0579c09test: Tests for PSBT AddInput and AddOutput (Ava Chow)b8b6e7f0c2tests: Add PSBT unit test for ComputeTimeLock (Ava Chow)0bc1c2e508tests: Add test vectors from BIP 370 (Ava Chow)e0e4dbdeb5psbt: Change default psbt version to 2 (Ava Chow)bcc1dca77bAdd psbt_version to PSBT RPCs and default to v2 (Ava Chow)ab38c30195Implement PSBTv2 field merging (Ava Chow)93e339e29fImplement PSBTv2 AddInput and AddOutput (Ava Chow)b39c86ae60Allow specifying PSBT version in constructor (Ava Chow)dcc9a3c8dfImplement PSBTv2 in decodepsbt (Ava Chow)5770dbd39fAdd PSBT::ComputeLockTime() (Ava Chow)863cf47b33Update test_framework/psbt.py for PSBTv2 (Ava Chow)925161eaf0Implement PSBTv2 fields de/ser (Ava Chow)d9cf658ee0Restrict joinpsbts to PSBTv0 only (Ava Chow)3da0e16012Replace PSBT.tx with PSBT::GetUnsignedTx and PSBT::GetUniqueID (Ava Chow)c568624ff2psbt: Return std::optional from PrecomputePSBTData (Ava Chow)092de4f1f6Replace PSBT::GetInputUTXO with PSBTInput::GetUTXO (Ava Chow)82c9fe3179psbt: Use PSBTInput and PSBTOutput fields instead of accessing global tx (Ava Chow)95897507e9psbt: AddInput and AddOutput should take only PSBTInput and PSBTOutput (Ava Chow)1b7d323a72Add PSBTInput::GetOutPoint (Ava Chow)543d3e1cdcpsbt: add PSBTv2 global tx fields (Ava Chow)c01c7f068cpsbt: Remove default constructor (Ava Chow)9671aa08c2psbt: add tx input and output fields in PSBTInput and PSBTOutput (Ava Chow)990b084f11Have PSBTInput and PSBTOutput know the PSBT's version (Ava Chow)7eacc21ff6psbt: make PSBT structs into classes (Ava Chow)f926c326bbgui: Store PSBT in std::optional in PSBTOperationsDialog (Ava Chow)1e2d146b47psbt: Refactor duplicate key lookup and size checks (Ava Chow)88384180d3test: PSBTs should roundtrip through RPCs that do nothing (Ava Chow)001877500dtest: construct psbt with unknown field programmatically (David Gumberg)0cb884e6dfpsbt: Fill hash preimages and taproot builder from SignatureData (Ava Chow) Pull request description: BIP 370 PSBTv2 introduces several new fields and different invariants for PSBT. This PR implements those new fields and restructures the PSBT implementation to match PSBTv2 but still remain compatible with PSBTv0. ACKs for top commit: nervana21: re-ACK9fa4076b20theStack: re-ACK9fa4076b20w0xlt: ACK9fa4076b20Tree-SHA512: ab0a5ada4fa5fca27ba9ec9c291a44b30e69d6db11971957572d86c58c71c4caa4557dc25f403e1170ba4fac751306d074cc582defefc6e2fdd37be51c3d9dd0
This commit is contained in:
@@ -4,11 +4,15 @@
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import base64
|
||||
import struct
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
from .util import assert_equal
|
||||
from .messages import (
|
||||
CTransaction,
|
||||
deser_string,
|
||||
deser_compact_size,
|
||||
from_binary,
|
||||
ser_compact_size,
|
||||
)
|
||||
@@ -108,37 +112,81 @@ class PSBT:
|
||||
self.g = g if g is not None else PSBTMap()
|
||||
self.i = i if i is not None else []
|
||||
self.o = o if o is not None else []
|
||||
self.tx = None
|
||||
self.version = None
|
||||
|
||||
def deserialize(self, f):
|
||||
assert_equal(f.read(5), b"psbt\xff")
|
||||
self.g = from_binary(PSBTMap, f)
|
||||
assert PSBT_GLOBAL_UNSIGNED_TX in self.g.map
|
||||
self.tx = from_binary(CTransaction, self.g.map[PSBT_GLOBAL_UNSIGNED_TX])
|
||||
self.i = [from_binary(PSBTMap, f) for _ in self.tx.vin]
|
||||
self.o = [from_binary(PSBTMap, f) for _ in self.tx.vout]
|
||||
|
||||
self.version = 0
|
||||
if PSBT_GLOBAL_VERSION in self.g.map:
|
||||
self.version = struct.unpack("<I", self.g.map[PSBT_GLOBAL_VERSION])[0]
|
||||
assert self.version in [0, 2]
|
||||
if self.version == 2:
|
||||
assert PSBT_GLOBAL_INPUT_COUNT in self.g.map
|
||||
assert PSBT_GLOBAL_OUTPUT_COUNT in self.g.map
|
||||
in_count = deser_compact_size(BytesIO(self.g.map[PSBT_GLOBAL_INPUT_COUNT]))
|
||||
out_count = deser_compact_size(BytesIO(self.g.map[PSBT_GLOBAL_OUTPUT_COUNT]))
|
||||
else:
|
||||
assert PSBT_GLOBAL_UNSIGNED_TX in self.g.map
|
||||
tx = from_binary(CTransaction, self.g.map[PSBT_GLOBAL_UNSIGNED_TX])
|
||||
in_count = len(tx.vin)
|
||||
out_count = len(tx.vout)
|
||||
|
||||
self.i = [from_binary(PSBTMap, f) for _ in range(in_count)]
|
||||
self.o = [from_binary(PSBTMap, f) for _ in range(out_count)]
|
||||
return self
|
||||
|
||||
def serialize(self):
|
||||
assert isinstance(self.g, PSBTMap)
|
||||
assert isinstance(self.i, list) and all(isinstance(x, PSBTMap) for x in self.i)
|
||||
assert isinstance(self.o, list) and all(isinstance(x, PSBTMap) for x in self.o)
|
||||
assert PSBT_GLOBAL_UNSIGNED_TX in self.g.map
|
||||
tx = from_binary(CTransaction, self.g.map[PSBT_GLOBAL_UNSIGNED_TX])
|
||||
assert_equal(len(tx.vin), len(self.i))
|
||||
assert_equal(len(tx.vout), len(self.o))
|
||||
if self.version is not None and self.version == 2:
|
||||
self.g.map[PSBT_GLOBAL_INPUT_COUNT] = ser_compact_size(len(self.i))
|
||||
self.g.map[PSBT_GLOBAL_OUTPUT_COUNT] = ser_compact_size(len(self.o))
|
||||
if self.version is None or (self.version is not None and self.version == 0):
|
||||
assert PSBT_GLOBAL_UNSIGNED_TX in self.g.map
|
||||
tx = from_binary(CTransaction, self.g.map[PSBT_GLOBAL_UNSIGNED_TX])
|
||||
assert_equal(len(tx.vin), len(self.i))
|
||||
assert_equal(len(tx.vout), len(self.o))
|
||||
|
||||
psbt = [x.serialize() for x in [self.g] + self.i + self.o]
|
||||
return b"psbt\xff" + b"".join(psbt)
|
||||
|
||||
def make_blank(self):
|
||||
"""
|
||||
Remove all fields except for PSBT_GLOBAL_UNSIGNED_TX
|
||||
Remove all fields except for required fields depending on version
|
||||
"""
|
||||
for m in self.i + self.o:
|
||||
m.map.clear()
|
||||
if self.version == 0:
|
||||
for m in self.i + self.o:
|
||||
m.map.clear()
|
||||
|
||||
self.g = PSBTMap(map={PSBT_GLOBAL_UNSIGNED_TX: self.g.map[PSBT_GLOBAL_UNSIGNED_TX]})
|
||||
self.g = PSBTMap(map={PSBT_GLOBAL_UNSIGNED_TX: self.g.map[PSBT_GLOBAL_UNSIGNED_TX]})
|
||||
elif self.version == 2:
|
||||
self.g = PSBTMap(map={
|
||||
PSBT_GLOBAL_TX_VERSION: self.g.map[PSBT_GLOBAL_TX_VERSION],
|
||||
PSBT_GLOBAL_INPUT_COUNT: self.g.map[PSBT_GLOBAL_INPUT_COUNT],
|
||||
PSBT_GLOBAL_OUTPUT_COUNT: self.g.map[PSBT_GLOBAL_OUTPUT_COUNT],
|
||||
PSBT_GLOBAL_VERSION: self.g.map[PSBT_GLOBAL_VERSION],
|
||||
})
|
||||
|
||||
new_i = []
|
||||
for m in self.i:
|
||||
new_i.append(PSBTMap(map={
|
||||
PSBT_IN_PREVIOUS_TXID: m.map[PSBT_IN_PREVIOUS_TXID],
|
||||
PSBT_IN_OUTPUT_INDEX: m.map[PSBT_IN_OUTPUT_INDEX],
|
||||
}))
|
||||
self.i = new_i
|
||||
|
||||
new_o = []
|
||||
for m in self.o:
|
||||
new_o.append(PSBTMap(map={
|
||||
PSBT_OUT_SCRIPT: m.map[PSBT_OUT_SCRIPT],
|
||||
PSBT_OUT_AMOUNT: m.map[PSBT_OUT_AMOUNT],
|
||||
}))
|
||||
self.o = new_o
|
||||
else:
|
||||
assert False
|
||||
|
||||
def to_base64(self):
|
||||
return base64.b64encode(self.serialize()).decode("utf8")
|
||||
|
||||
Reference in New Issue
Block a user