diff --git a/pycardano/transaction.py b/pycardano/transaction.py index b097a27d..79b7ce01 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -81,13 +81,6 @@ class Asset(DictCBORSerializable): VALUE_TYPE = int - def validate(self): - for n in self: - if self[n] < _MIN_INT64 or self[n] > _MAX_INT64: - raise InvalidDataException( - f"Asset amount must be between {_MIN_INT64} and {_MAX_INT64}: \n {self[n]}" - ) - def union(self, other: Asset) -> Asset: return self + other @@ -571,6 +564,15 @@ class TransactionBody(MapCBORSerializable): }, ) + def validate(self): + if ( + self.mint + and self.mint.count(lambda p, n, v: v < _MIN_INT64 or v > _MAX_INT64) > 0 + ): + raise InvalidDataException( + f"Mint amount must be between {_MIN_INT64} and {_MAX_INT64}. \n Mint amount: {self.mint}" + ) + def hash(self) -> bytes: return blake2b(self.to_cbor(), TRANSACTION_HASH_SIZE, encoder=RawEncoder) # type: ignore diff --git a/test/pycardano/test_transaction.py b/test/pycardano/test_transaction.py index 59f9779a..d414bb32 100644 --- a/test/pycardano/test_transaction.py +++ b/test/pycardano/test_transaction.py @@ -472,7 +472,12 @@ class TestDatum(PlutusData): def test_out_of_bound_asset(): - bad_asset = Asset({AssetName(b"abc"): 1 << 64}) + a = Asset({AssetName(b"abc"): 1 << 64}) + a.to_cbor_hex() # okay to have out of bound asset + + tx = TransactionBody(mint=MultiAsset({ScriptHash(b"1" * SCRIPT_HASH_SIZE): a})) + + # Not okay only when minting with pytest.raises(InvalidDataException): - bad_asset.to_cbor_hex() + tx.to_cbor_hex()