Skip to content

Commit 486d97f

Browse files
authored
Merge pull request #338 from cffls/fix/asset_too_large
Add boundary check for asset value
2 parents 7a642b0 + e0cbb1a commit 486d97f

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

pycardano/serialization.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,13 @@ def __copy__(self):
870870
def __deepcopy__(self, memodict={}):
871871
return self.__class__(deepcopy(self.data))
872872

873+
def validate(self):
874+
for key, value in self.data.items():
875+
if isinstance(key, CBORSerializable):
876+
key.validate()
877+
if isinstance(value, CBORSerializable):
878+
value.validate()
879+
873880
def to_shallow_primitive(self) -> dict:
874881
# Sort keys in a map according to https://datatracker.ietf.org/doc/html/rfc7049#section-3.9
875882
def _get_sortable_val(key):

pycardano/transaction.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
"Withdrawals",
5555
]
5656

57+
_MAX_INT64 = (1 << 63) - 1
58+
_MIN_INT64 = -(1 << 63)
59+
5760

5861
@dataclass(repr=False)
5962
class TransactionInput(ArrayCBORSerializable):
@@ -561,6 +564,15 @@ class TransactionBody(MapCBORSerializable):
561564
},
562565
)
563566

567+
def validate(self):
568+
if (
569+
self.mint
570+
and self.mint.count(lambda p, n, v: v < _MIN_INT64 or v > _MAX_INT64) > 0
571+
):
572+
raise InvalidDataException(
573+
f"Mint amount must be between {_MIN_INT64} and {_MAX_INT64}. \n Mint amount: {self.mint}"
574+
)
575+
564576
def hash(self) -> bytes:
565577
return blake2b(self.to_cbor(), TRANSACTION_HASH_SIZE, encoder=RawEncoder) # type: ignore
566578

test/pycardano/test_transaction.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,15 @@ class TestDatum(PlutusData):
469469
cbor = output.to_cbor_hex()
470470

471471
assert cbor == TransactionOutput.from_cbor(cbor).to_cbor_hex()
472+
473+
474+
def test_out_of_bound_asset():
475+
a = Asset({AssetName(b"abc"): 1 << 64})
476+
477+
a.to_cbor_hex() # okay to have out of bound asset
478+
479+
tx = TransactionBody(mint=MultiAsset({ScriptHash(b"1" * SCRIPT_HASH_SIZE): a}))
480+
481+
# Not okay only when minting
482+
with pytest.raises(InvalidDataException):
483+
tx.to_cbor_hex()

0 commit comments

Comments
 (0)