Binary serialization: update defs, fix bugs, add test cases

This commit is contained in:
mDuo13
2025-06-23 18:00:49 -07:00
parent 824c335d08
commit b15f1392b0
6 changed files with 3242 additions and 1463 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -153,6 +153,7 @@ def amount_to_bytes(a):
xrp_amt = -xrp_amt
return xrp_amt.to_bytes(8, byteorder="big", signed=False)
elif type(a) == dict:
#TODO: handle mpt amounts
if sorted(a.keys()) != ["currency", "issuer", "value"]:
raise ValueError("amount must have currency, value, issuer only (actually had: %s)" %
sorted(a.keys()))
@@ -254,7 +255,7 @@ def hex_to_bytes(contents):
Helper function; serializes a hash value from a hexadecimal string
of any length.
"""
return bytes.fromhex(field_val)
return bytes.fromhex(contents)
def issue_to_bytes(issue):
"""
@@ -394,6 +395,16 @@ def uint384_to_bytes(i):
raise ValueError("UInt64 is not 64 bits long")
return b
def vector256_to_bytes(strlist):
"""
Serialize a Vector256 type which is a length-prefixed list of arbitrary
256-bit values.
"""
binarylist = []
for item in strlist:
binarylist.append(hash256_to_bytes(item))
return vl_encode(b''.join(binarylist))
# Core serialization logic -----------------------------------------------------
def field_to_bytes(field_name, field_val):
@@ -430,6 +441,7 @@ def field_to_bytes(field_name, field_val):
"UInt32": uint32_to_bytes,
"UInt64": uint64_to_bytes,
"UInt384": uint384_to_bytes,
"Vector256": vector256_to_bytes,
}
field_binary = dispatch[field_type](field_val)
return b''.join( (id_prefix, field_binary) )
@@ -465,6 +477,12 @@ def serialize_tx(tx, for_signing=False):
"Sequence": 2
}
"""
# Special case: DeliverMax is an API alias for Amount. De-alias it here.
# See also: https://github.com/XRPLF/rippled/issues/5506
if "DeliverMax" in tx.keys():
tx["Amount"] = tx["DeliverMax"]
del tx["DeliverMax"]
field_order = sorted(tx.keys(), key=field_sort_key)
logger.debug("Canonical field order: %s" % field_order)

View File

@@ -0,0 +1 @@
12000024003B498561600000000000002710003B49848403524C52FC5B7E804DFE38271A5B1B3E46A93B684000000000000064732102FC20ED4D4D43CFAF4E449A85F68CAEE35B59F19233773818CA05221643A09FBC74473045022100A6FFF3327662DCA0246EA196A2E56754341D614F2A04AF2378A0B273EEB1A87F02207C47C93E185083BEA57C5725947042C279DDEC36EBAE9BACF4880DCCE7D6845181148403524C52FC5B7E804DFE38271A5B1B3E46A93B831412F36D6C45FE5F0C37A6DFA21BE514C45D1E0655

View File

@@ -0,0 +1,13 @@
{
"Account": "rDspBaumB5u6YWRdw5BWoBP3rcExFrXxoY",
"DeliverMax": {
"mpt_issuance_id": "003B49848403524C52FC5B7E804DFE38271A5B1B3E46A93B",
"value": "10000"
},
"Destination": "rpjU8anTxTQX97GKHsuv8TyQ659j6pPak4",
"Fee": "100",
"Sequence": 3885445,
"SigningPubKey": "02FC20ED4D4D43CFAF4E449A85F68CAEE35B59F19233773818CA05221643A09FBC",
"TransactionType": "Payment",
"TxnSignature": "3045022100A6FFF3327662DCA0246EA196A2E56754341D614F2A04AF2378A0B273EEB1A87F02207C47C93E185083BEA57C5725947042C279DDEC36EBAE9BACF4880DCCE7D68451"
}

View File

@@ -0,0 +1 @@
1200332FFFFFFFFF2033000004D2750B6469645F6578616D706C65701C0863757272656E6379701D0870726F7669646572811401476926B590BA3245F63C829116A0A3AF7F382DF018E020301700000000000001E2041003011A0000000000000000000000000000000000000000021A0000000000000000000000005553440000000000E1F1

View File

@@ -0,0 +1,19 @@
{
"TransactionType": "OracleSet",
"Account": "rfmDuhDyLGgx94qiwf3YF8BUV5j6KSvE8",
"OracleDocumentID": 1234,
"LastUpdateTime": 4294967295,
"PriceDataSeries": [
{
"PriceData": {
"BaseAsset": "XRP",
"QuoteAsset": "USD",
"AssetPrice": "00000000000001E2",
"Scale": 3
}
}
],
"Provider": "70726F7669646572",
"URI": "6469645F6578616D706C65",
"AssetClass": "63757272656E6379"
}