Skip to content

Commit a277ac2

Browse files
committed
Fix psbt serializations to always be consistent
Sort items before serializing so that the same psbt will always be serialized the same way.
1 parent d01cbae commit a277ac2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

hwilib/serializations.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ def DeserializeHDKeypath(f, key, hd_keypaths):
503503

504504
def SerializeHDKeypath(hd_keypaths, type):
505505
r = b""
506-
for pubkey, path in hd_keypaths.items():
506+
for pubkey, path in sorted(hd_keypaths.items()):
507507
r += ser_string(type + pubkey)
508508
packed = struct.pack("<" + "I" * len(path), *path)
509509
r += ser_string(packed)
@@ -638,7 +638,7 @@ def serialize(self):
638638
r += ser_string(tx)
639639

640640
if len(self.final_script_sig) == 0 and self.final_script_witness.is_null():
641-
for pubkey, sig in self.partial_sigs.items():
641+
for pubkey, sig in sorted(self.partial_sigs.items()):
642642
r += ser_string(b"\x02" + pubkey)
643643
r += ser_string(sig)
644644

@@ -664,7 +664,7 @@ def serialize(self):
664664
r += ser_string(b"\x08")
665665
r += self.final_script_witness.serialize()
666666

667-
for key, value in self.unknown:
667+
for key, value in sorted(self.unknown.items()):
668668
r += ser_string(key)
669669
r += ser_string(value)
670670

@@ -745,7 +745,7 @@ def serialize(self):
745745

746746
r += SerializeHDKeypath(self.hd_keypaths, b"\x02")
747747

748-
for key, value in self.unknown:
748+
for key, value in sorted(self.unknown.items()):
749749
r += ser_string(key)
750750
r += ser_string(value)
751751

@@ -762,7 +762,7 @@ def __init__(self, tx = None):
762762
self.tx = CTransaction()
763763
self.inputs = []
764764
self.outputs = []
765-
self.unknown = []
765+
self.unknown = {}
766766

767767
def deserialize(self, psbt):
768768
hexstring = Base64ToHex(psbt.strip())
@@ -860,7 +860,7 @@ def serialize(self):
860860
r += b"\x00"
861861

862862
# unknowns
863-
for key, value in self.unknown:
863+
for key, value in sorted(self.unknown.items()):
864864
r += ser_string(key)
865865
r += ser_string(value)
866866

0 commit comments

Comments
 (0)