Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the default returning type of 'to_cbor' to bytes #232

Merged
merged 1 commit into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ ecpy==1.2.5 ; python_version >= "3.7" and python_version < "4.0"
exceptiongroup==1.1.1 ; python_version >= "3.7" and python_version < "3.11"
execnet==1.9.0 ; python_version >= "3.7" and python_version < "4.0"
flake8==5.0.4 ; python_version >= "3.7" and python_version < "4.0"
flask==2.2.3 ; python_version >= "3.7" and python_version < "4.0"
flask==2.2.4 ; python_version >= "3.7" and python_version < "4.0"
frozendict==2.3.8 ; python_version >= "3.7" and python_version < "4.0"
frozenlist==1.3.3 ; python_version >= "3.7" and python_version < "4.0"
idna==3.4 ; python_version >= "3.7" and python_version < "4"
imagesize==1.4.1 ; python_version >= "3.7" and python_version < "4.0"
importlib-metadata==4.2.0 ; python_version >= "3.7" and python_version < "3.10"
Expand All @@ -37,7 +39,7 @@ mypy==1.2.0 ; python_version >= "3.7" and python_version < "4.0"
oscrypto==1.3.0 ; python_version >= "3.7" and python_version < "4.0"
packaging==23.1 ; python_version >= "3.7" and python_version < "4.0"
pathspec==0.11.1 ; python_version >= "3.7" and python_version < "4.0"
platformdirs==3.2.0 ; python_version >= "3.7" and python_version < "4.0"
platformdirs==3.3.0 ; python_version >= "3.7" and python_version < "4.0"
pluggy==1.0.0 ; python_version >= "3.7" and python_version < "4.0"
pprintpp==0.4.0 ; python_version >= "3.7" and python_version < "4.0"
py==1.11.0 ; python_version >= "3.7" and python_version < "4.0"
Expand All @@ -52,7 +54,7 @@ pytest==7.3.1 ; python_version >= "3.7" and python_version < "4.0"
pytz==2023.3 ; python_version >= "3.7" and python_version < "3.9"
requests==2.28.2 ; python_version >= "3.7" and python_version < "4"
retry==0.9.2 ; python_version >= "3.7" and python_version < "4.0"
setuptools==67.7.1 ; python_version >= "3.7" and python_version < "4.0"
setuptools==67.7.2 ; python_version >= "3.7" and python_version < "4.0"
six==1.16.0 ; python_version >= "3.7" and python_version < "4.0"
snowballstemmer==2.2.0 ; python_version >= "3.7" and python_version < "4.0"
sphinx-copybutton==0.5.2 ; python_version >= "3.7" and python_version < "4.0"
Expand Down
12 changes: 6 additions & 6 deletions docs/source/guides/plutus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To calculate the hash of a datum, we can leverage the helper class `PlutusData`.
Empty datum::

>>> empty_datum = PlutusData()
>>> empty_datum.to_cbor()
>>> empty_datum.to_cbor_hex()
'd87980'

Sample datum with int, bytes, List and hashmap inputs::
Expand All @@ -39,7 +39,7 @@ Sample datum with int, bytes, List and hashmap inputs::
... d: dict

>>> datum = MyDatum(123, b"1234", IndefiniteList([4, 5, 6]), {1: b"1", 2: b"2"})
>>> datum.to_cbor()
>>> datum.to_cbor_hex()
'd87a9f187b43333231ff'

You can also wrap `PlutusData` within `PlutusData`::
Expand All @@ -55,7 +55,7 @@ You can also wrap `PlutusData` within `PlutusData`::
>>> deadline = 1643235300000
>>> other_datum = MyDatum(123, b"1234", IndefiniteList([4, 5, 6]), {1: b"1", 2: b"2"})
>>> include_datum = InclusionDatum(key_hash, deadline, other_datum)
>>> include_datum.to_cbor()
>>> include_datum.to_cbor_hex()
'd87a9f581cc2ff616e11299d9094ce0a7eb5b7284b705147a822f4ffbd471f971a1b0000017e9874d2a0d8668218829f187b44313233349f040506ffa2014131024132ffff'

`PlutusData` supports conversion from/to JSON format, which
Expand All @@ -67,7 +67,7 @@ Similarly, redeemer can be serialized like following::

>>> data = MyDatum(123, b"234", IndefiniteList([]), {1: b"1", 2: b"2"})
>>> redeemer = Redeemer(data, ExecutionUnits(1000000, 1000000))
>>> redeemer.to_cbor()
>>> redeemer.to_cbor_hex()
'840000d8668218829f187b433233349fffa2014131024132ff821a000f42401a000f4240'

-----------------------
Expand Down Expand Up @@ -178,7 +178,7 @@ Note that we will just use the datatype defined in the contract, as it also uses
Build, sign and submit the transaction:

>>> signed_tx = builder.build_and_sign([payment_skey], giver_address)
>>> context.submit_tx(signed_tx.to_cbor())
>>> context.submit_tx(signed_tx.to_cbor_hex())

Step 6

Expand Down Expand Up @@ -219,7 +219,7 @@ Now lets try to resubmit this::

>>> signed_tx = builder.build_and_sign([payment_skey_2], taker_address)

>>> context.submit_tx(signed_tx.to_cbor())
>>> context.submit_tx(signed_tx.to_cbor_hex())

The funds locked in script address is successfully retrieved to the taker address.

Expand Down
2 changes: 1 addition & 1 deletion docs/source/guides/serialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Examples::
... outputs=[output1, output2],
... fee=fee
... )
>>> cbor_hex = tx_body.to_cbor()
>>> cbor_hex = tx_body.to_cbor_hex()
a50081825820732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e500018282581d60f6532850e1bccee9c72a9113ad98bcc5dbb30d2ac960262444f6e5f41b000000174876e80082581d60f6532850e1bccee9c72a9113ad98bcc5dbb30d2ac960262444f6e5f41b000000ba43b4b7f7021a000288090d800e80

>>> restored_tx_body = TransactionBody.from_cbor(cbor_hex)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guides/transaction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@ Transaction submission
Once we have a signed transaction, it could be submitted to the network. The easiest way to do so is through a chain
context::

>>> context.submit_tx(signed_tx.to_cbor())
>>> context.submit_tx(signed_tx.to_cbor_hex())

4 changes: 2 additions & 2 deletions examples/full_stack/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def home_page():
@app.route("/build_tx", methods=["POST"])
def build_tx():
tx = build_transaction(request.json)
cbor_hex = tx.to_cbor()
cbor_hex = tx.to_cbor_hex()
print(cbor_hex)
return {"tx": cbor_hex}

Expand All @@ -75,7 +75,7 @@ def submit_tx():
tx = compose_tx_and_witness(request.json)
tx_id = tx.transaction_body.hash().hex()
print(f"Transaction: \n {tx}")
print(f"Transaction cbor: {tx.to_cbor()}")
print(f"Transaction cbor: {tx.to_cbor_hex()}")
print(f"Transaction ID: {tx_id}")
chain_context.submit_tx(tx)
return {"tx_id": tx_id}
2 changes: 1 addition & 1 deletion examples/native_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def load_or_create_key_pair(base_dir, base_name):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand Down
2 changes: 1 addition & 1 deletion examples/plutus/forty_two/forty_two.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def wait_for_tx(tx_id):
def submit_tx(tx):
print("############### Transaction created ###############")
print(tx)
print(tx.to_cbor())
print(tx.to_cbor_hex())
print("############### Submitting transaction ###############")
chain_context.submit_tx(tx)
wait_for_tx(str(tx.id))
Expand Down
2 changes: 1 addition & 1 deletion integration-test/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def fund(self, source_address, source_key, target_address, amount=5000000):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
self.assert_output(target_address, target_output=output)
6 changes: 3 additions & 3 deletions integration-test/test/test_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_stake_delegation(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)

Expand All @@ -58,7 +58,7 @@ def test_stake_delegation(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)

Expand All @@ -84,6 +84,6 @@ def test_stake_delegation(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
2 changes: 1 addition & 1 deletion integration-test/test/test_min_utxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class MyPlutusData(PlutusData):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand Down
8 changes: 4 additions & 4 deletions integration-test/test/test_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def load_or_create_key_pair(base_dir, base_name):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand All @@ -158,7 +158,7 @@ def load_or_create_key_pair(base_dir, base_name):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand Down Expand Up @@ -247,7 +247,7 @@ def test_mint_nft_with_script(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand Down Expand Up @@ -334,7 +334,7 @@ class MyPlutusData(PlutusData):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())

# Submit signed transaction to the network
print("############### Submitting transaction ###############")
Expand Down
18 changes: 9 additions & 9 deletions integration-test/test/test_plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_plutus_v1(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand All @@ -53,7 +53,7 @@ def test_plutus_v1(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_plutus_v1(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)

Expand Down Expand Up @@ -119,7 +119,7 @@ def test_plutus_v2_datum_hash(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_plutus_v2_datum_hash(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)

Expand Down Expand Up @@ -196,7 +196,7 @@ def test_plutus_v2_inline_script_inline_datum(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_plutus_v2_ref_script(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand All @@ -274,7 +274,7 @@ def test_plutus_v2_ref_script(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)
time.sleep(3)
Expand Down Expand Up @@ -306,7 +306,7 @@ def test_plutus_v2_ref_script(self):

print("############### Transaction created ###############")
print(signed_tx)
print(signed_tx.to_cbor())
print(signed_tx.to_cbor_hex())
print("############### Submitting transaction ###############")
self.chain_context.submit_tx(signed_tx)

Expand Down
6 changes: 3 additions & 3 deletions pycardano/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from dataclasses import dataclass
from typing import Dict, List, Union

from pycardano.exception import InvalidArgumentException
from pycardano.address import Address
from pycardano.exception import InvalidArgumentException
from pycardano.network import Network
from pycardano.plutus import ExecutionUnits
from pycardano.transaction import Transaction, UTxO
Expand Down Expand Up @@ -170,7 +170,7 @@ def submit_tx(self, tx: Union[Transaction, bytes, str]):
:class:`TransactionFailedException`: When fails to submit the transaction to blockchain.
"""
if isinstance(tx, Transaction):
return self.submit_tx_cbor(tx.to_cbor("bytes"))
return self.submit_tx_cbor(tx.to_cbor())
elif isinstance(tx, bytes):
return self.submit_tx_cbor(tx)
else:
Expand Down Expand Up @@ -199,7 +199,7 @@ def evaluate_tx(self, tx: Transaction) -> Dict[str, ExecutionUnits]:
Returns:
List[ExecutionUnits]: A list of execution units calculated for each of the transaction's redeemers
"""
return self.evaluate_tx_cbor(tx.to_cbor("bytes"))
return self.evaluate_tx_cbor(tx.to_cbor())

def evaluate_tx_cbor(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
"""Evaluate execution units of a transaction.
Expand Down
2 changes: 1 addition & 1 deletion pycardano/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def to_json(self) -> str:
{
"type": self.key_type,
"description": self.description,
"cborHex": self.to_cbor(),
"cborHex": self.to_cbor_hex(),
}
)

Expand Down
2 changes: 1 addition & 1 deletion pycardano/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ def from_primitive(cls: Type[AuxiliaryData], value: Primitive) -> AuxiliaryData:

def hash(self) -> AuxiliaryDataHash:
return AuxiliaryDataHash(
blake2b(self.to_cbor("bytes"), AUXILIARY_DATA_HASH_SIZE, encoder=RawEncoder) # type: ignore
blake2b(self.to_cbor(), AUXILIARY_DATA_HASH_SIZE, encoder=RawEncoder) # type: ignore
)
2 changes: 1 addition & 1 deletion pycardano/nativescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def from_primitive(
raise DeserializeException(f"Unknown script type indicator: {script_type}")

def hash(self) -> ScriptHash:
cbor_bytes = cast(bytes, self.to_cbor("bytes"))
cbor_bytes = cast(bytes, self.to_cbor())
return ScriptHash(
blake2b(bytes(1) + cbor_bytes, SCRIPT_HASH_SIZE, encoder=RawEncoder)
)
Expand Down
6 changes: 3 additions & 3 deletions pycardano/plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class will reduce the complexity of serialization and deserialization tremendous
... a: int
... b: bytes
>>> test = Test(123, b"321")
>>> test.to_cbor()
>>> test.to_cbor_hex()
'd87a9f187b43333231ff'
>>> assert test == Test.from_cbor("d87a9f187b43333231ff")
"""
Expand Down Expand Up @@ -667,7 +667,7 @@ def from_json(cls: Type[PlutusData], data: str) -> PlutusData:
return cls.from_dict(obj)

def __deepcopy__(self, memo):
return self.__class__.from_cbor(self.to_cbor())
return self.__class__.from_cbor(self.to_cbor_hex())


@dataclass
Expand Down Expand Up @@ -696,7 +696,7 @@ def from_primitive(cls: Type[RawPlutusData], value: CBORTag) -> RawPlutusData:
return cls(value)

def __deepcopy__(self, memo):
return self.__class__.from_cbor(self.to_cbor())
return self.__class__.from_cbor(self.to_cbor_hex())


Datum = Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, RawPlutusData]
Expand Down
Loading