Skip to content

Commit eaadc38

Browse files
authored
Change the default returning type of 'to_cbor' to bytes (#232)
Change the default returning type of 'to_cbor' to bytes and add 'to_cbor_hex' that returns cbor hex.
1 parent 40f00fb commit eaadc38

28 files changed

+123
-136
lines changed

docs/requirements.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ ecpy==1.2.5 ; python_version >= "3.7" and python_version < "4.0"
2121
exceptiongroup==1.1.1 ; python_version >= "3.7" and python_version < "3.11"
2222
execnet==1.9.0 ; python_version >= "3.7" and python_version < "4.0"
2323
flake8==5.0.4 ; python_version >= "3.7" and python_version < "4.0"
24-
flask==2.2.3 ; python_version >= "3.7" and python_version < "4.0"
24+
flask==2.2.4 ; python_version >= "3.7" and python_version < "4.0"
25+
frozendict==2.3.8 ; python_version >= "3.7" and python_version < "4.0"
26+
frozenlist==1.3.3 ; python_version >= "3.7" and python_version < "4.0"
2527
idna==3.4 ; python_version >= "3.7" and python_version < "4"
2628
imagesize==1.4.1 ; python_version >= "3.7" and python_version < "4.0"
2729
importlib-metadata==4.2.0 ; python_version >= "3.7" and python_version < "3.10"
@@ -37,7 +39,7 @@ mypy==1.2.0 ; python_version >= "3.7" and python_version < "4.0"
3739
oscrypto==1.3.0 ; python_version >= "3.7" and python_version < "4.0"
3840
packaging==23.1 ; python_version >= "3.7" and python_version < "4.0"
3941
pathspec==0.11.1 ; python_version >= "3.7" and python_version < "4.0"
40-
platformdirs==3.2.0 ; python_version >= "3.7" and python_version < "4.0"
42+
platformdirs==3.3.0 ; python_version >= "3.7" and python_version < "4.0"
4143
pluggy==1.0.0 ; python_version >= "3.7" and python_version < "4.0"
4244
pprintpp==0.4.0 ; python_version >= "3.7" and python_version < "4.0"
4345
py==1.11.0 ; python_version >= "3.7" and python_version < "4.0"
@@ -52,7 +54,7 @@ pytest==7.3.1 ; python_version >= "3.7" and python_version < "4.0"
5254
pytz==2023.3 ; python_version >= "3.7" and python_version < "3.9"
5355
requests==2.28.2 ; python_version >= "3.7" and python_version < "4"
5456
retry==0.9.2 ; python_version >= "3.7" and python_version < "4.0"
55-
setuptools==67.7.1 ; python_version >= "3.7" and python_version < "4.0"
57+
setuptools==67.7.2 ; python_version >= "3.7" and python_version < "4.0"
5658
six==1.16.0 ; python_version >= "3.7" and python_version < "4.0"
5759
snowballstemmer==2.2.0 ; python_version >= "3.7" and python_version < "4.0"
5860
sphinx-copybutton==0.5.2 ; python_version >= "3.7" and python_version < "4.0"

docs/source/guides/plutus.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To calculate the hash of a datum, we can leverage the helper class `PlutusData`.
2424
Empty datum::
2525

2626
>>> empty_datum = PlutusData()
27-
>>> empty_datum.to_cbor()
27+
>>> empty_datum.to_cbor_hex()
2828
'd87980'
2929

3030
Sample datum with int, bytes, List and hashmap inputs::
@@ -39,7 +39,7 @@ Sample datum with int, bytes, List and hashmap inputs::
3939
... d: dict
4040

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

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

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

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

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

180180
>>> signed_tx = builder.build_and_sign([payment_skey], giver_address)
181-
>>> context.submit_tx(signed_tx.to_cbor())
181+
>>> context.submit_tx(signed_tx.to_cbor_hex())
182182

183183
Step 6
184184

@@ -219,7 +219,7 @@ Now lets try to resubmit this::
219219

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

222-
>>> context.submit_tx(signed_tx.to_cbor())
222+
>>> context.submit_tx(signed_tx.to_cbor_hex())
223223

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

docs/source/guides/serialization.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Examples::
3434
... outputs=[output1, output2],
3535
... fee=fee
3636
... )
37-
>>> cbor_hex = tx_body.to_cbor()
37+
>>> cbor_hex = tx_body.to_cbor_hex()
3838
a50081825820732bfd67e66be8e8288349fcaaa2294973ef6271cc189a239bb431275401b8e500018282581d60f6532850e1bccee9c72a9113ad98bcc5dbb30d2ac960262444f6e5f41b000000174876e80082581d60f6532850e1bccee9c72a9113ad98bcc5dbb30d2ac960262444f6e5f41b000000ba43b4b7f7021a000288090d800e80
3939

4040
>>> restored_tx_body = TransactionBody.from_cbor(cbor_hex)

docs/source/guides/transaction.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,5 @@ Transaction submission
178178
Once we have a signed transaction, it could be submitted to the network. The easiest way to do so is through a chain
179179
context::
180180

181-
>>> context.submit_tx(signed_tx.to_cbor())
181+
>>> context.submit_tx(signed_tx.to_cbor_hex())
182182

examples/full_stack/server.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def home_page():
6565
@app.route("/build_tx", methods=["POST"])
6666
def build_tx():
6767
tx = build_transaction(request.json)
68-
cbor_hex = tx.to_cbor()
68+
cbor_hex = tx.to_cbor_hex()
6969
print(cbor_hex)
7070
return {"tx": cbor_hex}
7171

@@ -75,7 +75,7 @@ def submit_tx():
7575
tx = compose_tx_and_witness(request.json)
7676
tx_id = tx.transaction_body.hash().hex()
7777
print(f"Transaction: \n {tx}")
78-
print(f"Transaction cbor: {tx.to_cbor()}")
78+
print(f"Transaction cbor: {tx.to_cbor_hex()}")
7979
print(f"Transaction ID: {tx_id}")
8080
chain_context.submit_tx(tx)
8181
return {"tx_id": tx_id}

examples/native_token.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def load_or_create_key_pair(base_dir, base_name):
168168

169169
print("############### Transaction created ###############")
170170
print(signed_tx)
171-
print(signed_tx.to_cbor())
171+
print(signed_tx.to_cbor_hex())
172172

173173
# Submit signed transaction to the network
174174
print("############### Submitting transaction ###############")

examples/plutus/forty_two/forty_two.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def wait_for_tx(tx_id):
4141
def submit_tx(tx):
4242
print("############### Transaction created ###############")
4343
print(tx)
44-
print(tx.to_cbor())
44+
print(tx.to_cbor_hex())
4545
print("############### Submitting transaction ###############")
4646
chain_context.submit_tx(tx)
4747
wait_for_tx(str(tx.id))

integration-test/test/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def fund(self, source_address, source_key, target_address, amount=5000000):
6767

6868
print("############### Transaction created ###############")
6969
print(signed_tx)
70-
print(signed_tx.to_cbor())
70+
print(signed_tx.to_cbor_hex())
7171
print("############### Submitting transaction ###############")
7272
self.chain_context.submit_tx(signed_tx)
7373
self.assert_output(target_address, target_output=output)

integration-test/test/test_certificate.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_stake_delegation(self):
3131

3232
print("############### Transaction created ###############")
3333
print(signed_tx)
34-
print(signed_tx.to_cbor())
34+
print(signed_tx.to_cbor_hex())
3535
print("############### Submitting transaction ###############")
3636
self.chain_context.submit_tx(signed_tx)
3737

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

5959
print("############### Transaction created ###############")
6060
print(signed_tx)
61-
print(signed_tx.to_cbor())
61+
print(signed_tx.to_cbor_hex())
6262
print("############### Submitting transaction ###############")
6363
self.chain_context.submit_tx(signed_tx)
6464

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

8585
print("############### Transaction created ###############")
8686
print(signed_tx)
87-
print(signed_tx.to_cbor())
87+
print(signed_tx.to_cbor_hex())
8888
print("############### Submitting transaction ###############")
8989
self.chain_context.submit_tx(signed_tx)

integration-test/test/test_min_utxo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class MyPlutusData(PlutusData):
9898

9999
print("############### Transaction created ###############")
100100
print(signed_tx)
101-
print(signed_tx.to_cbor())
101+
print(signed_tx.to_cbor_hex())
102102

103103
# Submit signed transaction to the network
104104
print("############### Submitting transaction ###############")

integration-test/test/test_mint.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def load_or_create_key_pair(base_dir, base_name):
132132

133133
print("############### Transaction created ###############")
134134
print(signed_tx)
135-
print(signed_tx.to_cbor())
135+
print(signed_tx.to_cbor_hex())
136136

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

159159
print("############### Transaction created ###############")
160160
print(signed_tx)
161-
print(signed_tx.to_cbor())
161+
print(signed_tx.to_cbor_hex())
162162

163163
# Submit signed transaction to the network
164164
print("############### Submitting transaction ###############")
@@ -247,7 +247,7 @@ def test_mint_nft_with_script(self):
247247

248248
print("############### Transaction created ###############")
249249
print(signed_tx)
250-
print(signed_tx.to_cbor())
250+
print(signed_tx.to_cbor_hex())
251251

252252
# Submit signed transaction to the network
253253
print("############### Submitting transaction ###############")
@@ -334,7 +334,7 @@ class MyPlutusData(PlutusData):
334334

335335
print("############### Transaction created ###############")
336336
print(signed_tx)
337-
print(signed_tx.to_cbor())
337+
print(signed_tx.to_cbor_hex())
338338

339339
# Submit signed transaction to the network
340340
print("############### Submitting transaction ###############")

integration-test/test/test_plutus.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_plutus_v1(self):
3535

3636
print("############### Transaction created ###############")
3737
print(signed_tx)
38-
print(signed_tx.to_cbor())
38+
print(signed_tx.to_cbor_hex())
3939
print("############### Submitting transaction ###############")
4040
self.chain_context.submit_tx(signed_tx)
4141
time.sleep(3)
@@ -53,7 +53,7 @@ def test_plutus_v1(self):
5353

5454
print("############### Transaction created ###############")
5555
print(signed_tx)
56-
print(signed_tx.to_cbor())
56+
print(signed_tx.to_cbor_hex())
5757
print("############### Submitting transaction ###############")
5858
self.chain_context.submit_tx(signed_tx)
5959
time.sleep(3)
@@ -87,7 +87,7 @@ def test_plutus_v1(self):
8787

8888
print("############### Transaction created ###############")
8989
print(signed_tx)
90-
print(signed_tx.to_cbor())
90+
print(signed_tx.to_cbor_hex())
9191
print("############### Submitting transaction ###############")
9292
self.chain_context.submit_tx(signed_tx)
9393

@@ -119,7 +119,7 @@ def test_plutus_v2_datum_hash(self):
119119

120120
print("############### Transaction created ###############")
121121
print(signed_tx)
122-
print(signed_tx.to_cbor())
122+
print(signed_tx.to_cbor_hex())
123123
print("############### Submitting transaction ###############")
124124
self.chain_context.submit_tx(signed_tx)
125125
time.sleep(3)
@@ -162,7 +162,7 @@ def test_plutus_v2_datum_hash(self):
162162

163163
print("############### Transaction created ###############")
164164
print(signed_tx)
165-
print(signed_tx.to_cbor())
165+
print(signed_tx.to_cbor_hex())
166166
print("############### Submitting transaction ###############")
167167
self.chain_context.submit_tx(signed_tx)
168168

@@ -196,7 +196,7 @@ def test_plutus_v2_inline_script_inline_datum(self):
196196

197197
print("############### Transaction created ###############")
198198
print(signed_tx)
199-
print(signed_tx.to_cbor())
199+
print(signed_tx.to_cbor_hex())
200200
print("############### Submitting transaction ###############")
201201
self.chain_context.submit_tx(signed_tx)
202202
time.sleep(3)
@@ -257,7 +257,7 @@ def test_plutus_v2_ref_script(self):
257257

258258
print("############### Transaction created ###############")
259259
print(signed_tx)
260-
print(signed_tx.to_cbor())
260+
print(signed_tx.to_cbor_hex())
261261
print("############### Submitting transaction ###############")
262262
self.chain_context.submit_tx(signed_tx)
263263
time.sleep(3)
@@ -274,7 +274,7 @@ def test_plutus_v2_ref_script(self):
274274

275275
print("############### Transaction created ###############")
276276
print(signed_tx)
277-
print(signed_tx.to_cbor())
277+
print(signed_tx.to_cbor_hex())
278278
print("############### Submitting transaction ###############")
279279
self.chain_context.submit_tx(signed_tx)
280280
time.sleep(3)
@@ -306,7 +306,7 @@ def test_plutus_v2_ref_script(self):
306306

307307
print("############### Transaction created ###############")
308308
print(signed_tx)
309-
print(signed_tx.to_cbor())
309+
print(signed_tx.to_cbor_hex())
310310
print("############### Submitting transaction ###############")
311311
self.chain_context.submit_tx(signed_tx)
312312

pycardano/backend/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from dataclasses import dataclass
44
from typing import Dict, List, Union
55

6-
from pycardano.exception import InvalidArgumentException
76
from pycardano.address import Address
7+
from pycardano.exception import InvalidArgumentException
88
from pycardano.network import Network
99
from pycardano.plutus import ExecutionUnits
1010
from pycardano.transaction import Transaction, UTxO
@@ -170,7 +170,7 @@ def submit_tx(self, tx: Union[Transaction, bytes, str]):
170170
:class:`TransactionFailedException`: When fails to submit the transaction to blockchain.
171171
"""
172172
if isinstance(tx, Transaction):
173-
return self.submit_tx_cbor(tx.to_cbor("bytes"))
173+
return self.submit_tx_cbor(tx.to_cbor())
174174
elif isinstance(tx, bytes):
175175
return self.submit_tx_cbor(tx)
176176
else:
@@ -199,7 +199,7 @@ def evaluate_tx(self, tx: Transaction) -> Dict[str, ExecutionUnits]:
199199
Returns:
200200
List[ExecutionUnits]: A list of execution units calculated for each of the transaction's redeemers
201201
"""
202-
return self.evaluate_tx_cbor(tx.to_cbor("bytes"))
202+
return self.evaluate_tx_cbor(tx.to_cbor())
203203

204204
def evaluate_tx_cbor(self, cbor: Union[bytes, str]) -> Dict[str, ExecutionUnits]:
205205
"""Evaluate execution units of a transaction.

pycardano/key.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def to_json(self) -> str:
8383
{
8484
"type": self.key_type,
8585
"description": self.description,
86-
"cborHex": self.to_cbor(),
86+
"cborHex": self.to_cbor_hex(),
8787
}
8888
)
8989

pycardano/metadata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,5 @@ def from_primitive(cls: Type[AuxiliaryData], value: Primitive) -> AuxiliaryData:
127127

128128
def hash(self) -> AuxiliaryDataHash:
129129
return AuxiliaryDataHash(
130-
blake2b(self.to_cbor("bytes"), AUXILIARY_DATA_HASH_SIZE, encoder=RawEncoder) # type: ignore
130+
blake2b(self.to_cbor(), AUXILIARY_DATA_HASH_SIZE, encoder=RawEncoder) # type: ignore
131131
)

pycardano/nativescript.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def from_primitive(
5858
raise DeserializeException(f"Unknown script type indicator: {script_type}")
5959

6060
def hash(self) -> ScriptHash:
61-
cbor_bytes = cast(bytes, self.to_cbor("bytes"))
61+
cbor_bytes = cast(bytes, self.to_cbor())
6262
return ScriptHash(
6363
blake2b(bytes(1) + cbor_bytes, SCRIPT_HASH_SIZE, encoder=RawEncoder)
6464
)

pycardano/plutus.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class will reduce the complexity of serialization and deserialization tremendous
455455
... a: int
456456
... b: bytes
457457
>>> test = Test(123, b"321")
458-
>>> test.to_cbor()
458+
>>> test.to_cbor_hex()
459459
'd87a9f187b43333231ff'
460460
>>> assert test == Test.from_cbor("d87a9f187b43333231ff")
461461
"""
@@ -667,7 +667,7 @@ def from_json(cls: Type[PlutusData], data: str) -> PlutusData:
667667
return cls.from_dict(obj)
668668

669669
def __deepcopy__(self, memo):
670-
return self.__class__.from_cbor(self.to_cbor())
670+
return self.__class__.from_cbor(self.to_cbor_hex())
671671

672672

673673
@dataclass
@@ -696,7 +696,7 @@ def from_primitive(cls: Type[RawPlutusData], value: CBORTag) -> RawPlutusData:
696696
return cls(value)
697697

698698
def __deepcopy__(self, memo):
699-
return self.__class__.from_cbor(self.to_cbor())
699+
return self.__class__.from_cbor(self.to_cbor_hex())
700700

701701

702702
Datum = Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, RawPlutusData]

0 commit comments

Comments
 (0)