diff --git a/examples/example_bls_multisig.py b/examples/example_bls_multisig.py index 5dc96b8..436ab33 100644 --- a/examples/example_bls_multisig.py +++ b/examples/example_bls_multisig.py @@ -1,6 +1,6 @@ -from pactus.crypto.bls.private_key import PrivateKey -from pactus.crypto.bls.public_key import PublicKey -from pactus.crypto.bls.signature import Signature +from pactus.crypto.bls import PrivateKey +from pactus.crypto.bls import PublicKey +from pactus.crypto.bls import Signature def main() -> None: diff --git a/examples/example_key_generation.py b/examples/example_key_generation.py index 952176a..1467bf1 100644 --- a/examples/example_key_generation.py +++ b/examples/example_key_generation.py @@ -1,12 +1,8 @@ import argparse -from pactus.crypto.hrp import HRP -from pactus.crypto.address import AddressType -from pactus.crypto.bls.private_key import PrivateKey as BLSPrivateKey -from pactus.crypto.ed25519.private_key import PrivateKey as Ed25519PrivateKey -from pactus.crypto.private_key import PrivateKey -from pactus.crypto.public_key import PublicKey -from pactus.crypto.address import Address +from pactus.crypto import Address, AddressType, HRP, PrivateKey, PublicKey +from pactus.crypto.bls import PrivateKey as BLSPrivateKey +from pactus.crypto.ed25519 import PrivateKey as Ed25519PrivateKey def main() -> None: diff --git a/examples/example_transfer_transaction_bls.py b/examples/example_transfer_transaction_bls.py index 18f1b4c..57f9dea 100644 --- a/examples/example_transfer_transaction_bls.py +++ b/examples/example_transfer_transaction_bls.py @@ -1,8 +1,8 @@ -from pactus.crypto.hrp import HRP -from pactus.crypto.address import Address -from pactus.crypto.bls.private_key import PrivateKey -from pactus.transaction.transaction import Transaction -from pactus.amount import Amount +from pactus.crypto import HRP +from pactus.crypto import Address +from pactus.crypto.bls import PrivateKey +from pactus.transaction import Transaction +from pactus.types.amount import Amount def main() -> None: diff --git a/examples/example_transfer_transaction_ed25519.py b/examples/example_transfer_transaction_ed25519.py index 614797d..0d1f3c8 100644 --- a/examples/example_transfer_transaction_ed25519.py +++ b/examples/example_transfer_transaction_ed25519.py @@ -1,8 +1,8 @@ -from pactus.crypto.hrp import HRP -from pactus.crypto.address import Address -from pactus.crypto.ed25519.private_key import PrivateKey -from pactus.transaction.transaction import Transaction -from pactus.amount import Amount +from pactus.crypto import HRP +from pactus.crypto import Address +from pactus.crypto.ed25519 import PrivateKey +from pactus.transaction import Transaction +from pactus.types.amount import Amount def main() -> None: diff --git a/pactus/crypto/__init__.py b/pactus/crypto/__init__.py index e69de29..7f448b8 100644 --- a/pactus/crypto/__init__.py +++ b/pactus/crypto/__init__.py @@ -0,0 +1,6 @@ +from .address import Address, AddressType +from .hrp import HRP +from .private_key import PrivateKey +from .public_key import PublicKey + +__all__ = ["HRP", "Address", "AddressType", "PrivateKey", "PublicKey"] diff --git a/pactus/crypto/bls/__init__.py b/pactus/crypto/bls/__init__.py index e69de29..4c1784c 100644 --- a/pactus/crypto/bls/__init__.py +++ b/pactus/crypto/bls/__init__.py @@ -0,0 +1,6 @@ +from .private_key import PrivateKey +from .public_key import PublicKey +from .signature import DST, SIGNATURE_TYPE_BLS, Signature + +__all__ = ["DST", "SIGNATURE_TYPE_BLS", "PrivateKey", "PublicKey", "Signature"] + diff --git a/pactus/crypto/ed25519/__init__.py b/pactus/crypto/ed25519/__init__.py index e69de29..e6e249f 100644 --- a/pactus/crypto/ed25519/__init__.py +++ b/pactus/crypto/ed25519/__init__.py @@ -0,0 +1,6 @@ +from .private_key import PrivateKey +from .public_key import PublicKey +from .signature import SIGNATURE_TYPE_ED25519, Signature + +__all__ = ["SIGNATURE_TYPE_ED25519", "PrivateKey", "PublicKey", "Signature"] + diff --git a/pactus/crypto/hrp.py b/pactus/crypto/hrp.py index 877856a..f399ebc 100644 --- a/pactus/crypto/hrp.py +++ b/pactus/crypto/hrp.py @@ -3,6 +3,12 @@ class HRP: PUBLIC_KEY_HRP = "public" PRIVATE_KEY_HRP = "secret" + @classmethod + def use_mainnet(cls) -> None: + cls.ADDRESS_HRP = "pc" + cls.PUBLIC_KEY_HRP = "public" + cls.PRIVATE_KEY_HRP = "secret" + @classmethod def use_testnet(cls) -> None: cls.ADDRESS_HRP = "tpc" diff --git a/pactus/transaction/__init__.py b/pactus/transaction/__init__.py index e69de29..31c1c0a 100644 --- a/pactus/transaction/__init__.py +++ b/pactus/transaction/__init__.py @@ -0,0 +1,3 @@ +from .transaction import Transaction + +__all__ = ["Transaction"] diff --git a/pactus/transaction/_payload.py b/pactus/transaction/_payload.py index b5e348d..8d1c834 100644 --- a/pactus/transaction/_payload.py +++ b/pactus/transaction/_payload.py @@ -1,9 +1,9 @@ from abc import ABC, abstractmethod from enum import Enum -from pactus.amount import Amount from pactus.crypto.address import Address from pactus.encoding import encoding +from pactus.types.amount import Amount class PayloadType(Enum): diff --git a/pactus/transaction/transaction.py b/pactus/transaction/transaction.py index fba2459..d084d1a 100644 --- a/pactus/transaction/transaction.py +++ b/pactus/transaction/transaction.py @@ -1,9 +1,9 @@ -from pactus.amount import Amount from pactus.crypto.address import Address from pactus.crypto.private_key import PrivateKey from pactus.crypto.public_key import PublicKey from pactus.crypto.signature import Signature from pactus.encoding import encoding +from pactus.types.amount import Amount from ._payload import ( BondPayload, diff --git a/pactus/types/__init__.py b/pactus/types/__init__.py new file mode 100644 index 0000000..5a338b5 --- /dev/null +++ b/pactus/types/__init__.py @@ -0,0 +1,3 @@ +from .amount import Amount + +__all__ = ["Amount"] diff --git a/pactus/amount.py b/pactus/types/amount.py similarity index 93% rename from pactus/amount.py rename to pactus/types/amount.py index def8a40..23a36d2 100644 --- a/pactus/amount.py +++ b/pactus/types/amount.py @@ -29,6 +29,11 @@ def __eq__(self, other: "Amount") -> bool: def __hash__(self) -> int: return hash(self.value) + def __str__(self) -> str: + """Return a string representation of the amount in PAC.""" + pac_value = self.value / NANO_PAC_PER_PAC + return f"{pac_value}" + @classmethod def from_nano_pac(cls, a: int) -> "Amount": """Store the value as NanoPAC in the Amount instance.""" @@ -76,3 +81,4 @@ def round(self: float) -> float: return self - 0.5 return self + 0.5 + diff --git a/setup.py b/setup.py index 30404f2..5c3e1f4 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name="pactus-sdk", - version="1.2.4", + version="1.3.0", author="Pactus Development Team", author_email="info@pactus.org", description="Pactus Development Kit", diff --git a/tests/test_amount.py b/tests/test_amount.py index 0bb8826..8653892 100644 --- a/tests/test_amount.py +++ b/tests/test_amount.py @@ -1,6 +1,6 @@ import unittest -from pactus.amount import NANO_PAC_PER_PAC, Amount +from pactus.types.amount import NANO_PAC_PER_PAC, Amount class TestAmount(unittest.TestCase): @@ -61,6 +61,50 @@ def test_from_string(self): amt = Amount.from_string(case["input"]) self.assertEqual(amt, case["expected"]) + def test_str(self): + test_cases = [ + { + "input": Amount(0), + "expected": "0.0", + }, + { + "input": Amount.from_pac(42.5), + "expected": "42.5", + }, + { + "input": Amount.from_pac(1.0), + "expected": "1.0", + }, + { + "input": Amount.from_pac(0.5), + "expected": "0.5", + }, + { + "input": Amount.from_pac(1000000.0), + "expected": "1000000.0", + }, + { + "input": Amount.from_pac(0.000000001), + "expected": "1e-09", + }, + { + "input": Amount.from_pac(-10.5), + "expected": "-10.5", + }, + { + "input": Amount.from_nano_pac(1000000000), + "expected": "1.0", + }, + { + "input": Amount.from_nano_pac(500000000), + "expected": "0.5", + }, + ] + + for case in test_cases: + result = str(case["input"]) + self.assertEqual(result, case["expected"]) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_crypto_bls.py b/tests/test_crypto_bls.py index d1f74f5..3d0b59e 100644 --- a/tests/test_crypto_bls.py +++ b/tests/test_crypto_bls.py @@ -1,8 +1,8 @@ import unittest -from pactus.crypto.bls.private_key import PrivateKey as BLSPrivateKey -from pactus.crypto.bls.public_key import PublicKey as BLSPublicKey -from pactus.crypto.bls.signature import Signature as BLSSignature +from pactus.crypto.bls import PrivateKey as BLSPrivateKey +from pactus.crypto.bls import PublicKey as BLSPublicKey +from pactus.crypto.bls import Signature as BLSSignature class TestBLSCrypto(unittest.TestCase): diff --git a/tests/test_crypto_ed25519.py b/tests/test_crypto_ed25519.py index 253cb28..994090c 100644 --- a/tests/test_crypto_ed25519.py +++ b/tests/test_crypto_ed25519.py @@ -1,8 +1,8 @@ import unittest -from pactus.crypto.ed25519.private_key import PrivateKey as Ed25519PrivateKey -from pactus.crypto.ed25519.public_key import PublicKey as Ed25519PublicKey -from pactus.crypto.ed25519.signature import Signature as Ed25519Signature +from pactus.crypto.ed25519 import PrivateKey as Ed25519PrivateKey +from pactus.crypto.ed25519 import PublicKey as Ed25519PublicKey +from pactus.crypto.ed25519 import Signature as Ed25519Signature class TestEd25519Crypto(unittest.TestCase): diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 34bd113..3ed5bda 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -1,9 +1,9 @@ import unittest from pactus.crypto.address import Address -from pactus.crypto.bls.private_key import PrivateKey -from pactus.transaction.transaction import Transaction -from pactus.amount import Amount +from pactus.crypto.bls import PrivateKey +from pactus.transaction import Transaction +from pactus.types.amount import Amount class TestTransaction(unittest.TestCase):