Skip to content

Commit

Permalink
Only raise exception when minting assets whose values are out of bound
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Apr 28, 2024
1 parent 0daa16e commit e0cbb1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
16 changes: 9 additions & 7 deletions pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions test/pycardano/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e0cbb1a

Please sign in to comment.