Skip to content

Commit

Permalink
feat(examples): add ed25519 examples. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Sep 12, 2024
1 parent fe24e91 commit 035224c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def main() -> None:

sender = Address.from_string("tpc1z4rlzvq8lv92cnp0k3mk5jfr00kjnz0qvklvn3u")
sec = PrivateKey.from_string(
"tsecret1pzf33h72n9phxzaty5urh5ss4m33vvdfawyvesl5jtlt9a00tnwkqyngm6z"
"TSECRET1PZF33H72N9PHXZATY5URH5SS4M33VVDFAWYVESL5JTLT9A00TNWKQYNGM6Z"
)

tx = Transaction.create_transfer_tx(lock_time, sender, receiver, amount, fee, memo)
Expand Down
29 changes: 29 additions & 0 deletions examples/example_transfer_transaction_ed25519.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from pactus.crypto import CryptoConfig
from pactus.crypto.address import Address
from pactus.crypto.ed25519.private_key import PrivateKey
from pactus.transaction.transaction import Transaction
from pactus.types.amount import Amount


def main() -> None:
CryptoConfig.use_testnet()

lock_time = 1_735_096
memo = "This is a test transaction"
amount = Amount.from_string("1.5")
fee = Amount.from_string("0.01")
receiver = Address.from_string("tpc1ryz6m8meyfemyr4dhavz5aq067kkvs9mptpqnxr")

sender = Address.from_string("tpc1rv75w2y9hj64ht9spxx0e5s7avpclk9ey2eaavf")
sec = PrivateKey.from_string(
"TSECRET1RGLSGPYLQRVET27AZUVS9TSP8MPGF9LH4U4RKKARMCATFK9L0KUCS7DCC09"
)

tx = Transaction.create_transfer_tx(lock_time, sender, receiver, amount, fee, memo)
signed_tx = tx.sign(sec)

print(f"Signed transaction hex: {signed_tx}")


if __name__ == "__main__":
main()
47 changes: 0 additions & 47 deletions tests/test_crypto.py → tests/test_crypto_bls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from pactus.crypto.bls.public_key import PublicKey as BLSPublicKey
from pactus.crypto.bls.signature import Signature as BLSSignature

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


class TestBLSCrypto(unittest.TestCase):
def test_private_key_to_public_key(self):
Expand Down Expand Up @@ -99,48 +95,5 @@ def test_key_gen(self):
self.fail(f"Test '{i}' failed. Unexpected error: {e}")


class TestEd25519Crypto(unittest.TestCase):
def test_private_key_to_public_key(self):
prv_str = "SECRET1RJ6STNTA7Y3P2QLQF8A6QCX05F2H5TFNE5RSH066KZME4WVFXKE7QW097LG"
expected_pub_str = (
"public1ry2cqw5yfhmr7ve8nctgzg6wgcyc73xqr2uud486jgsq7wu253egsx6msep"
)

prv = Ed25519PrivateKey.from_string(prv_str)
pub = prv.public_key()
pub_str = pub.string()

self.assertEqual(pub_str, expected_pub_str)

def test_public_key_to_address(self):
pub_str = "public1ry2cqw5yfhmr7ve8nctgzg6wgcyc73xqr2uud486jgsq7wu253egsx6msep"
expected_acc_addr_str = "pc1reer4damrrdxznmrrl7a9acy7x5cwe6dyt8ftv4"

pub = Ed25519PublicKey.from_string(pub_str)
acc_add_str = pub.account_address().string()

self.assertEqual(acc_add_str, expected_acc_addr_str)

def test_sign(self):
prv_str = "secret1r0up878rawjec2evjnd4k42a4g4pcardesjk48jtn64qwjnfv7veqal53e2"
prv = Ed25519PrivateKey.from_string(prv_str)
pub = prv.public_key()
msg = b"pactus"
sig = prv.sign(msg)

valid_sig = Ed25519Signature.from_string(
"55eaa9656158874bff726c8d62abe0a5b66d2434705f46b58c905a42f1b39fb95f640fbacac97e6e6862220fe7b4f249a0f79dc3d37b4460156c58580778b70e"
)

invalid_sig = Ed25519Signature.from_string(
"5a61ecb3c08825010678f12c036cec2e1dd1b8767ed9fd95a97f560dfd6196b600fdc7bba22f13eae19ca578b920eb807eb6cd956d55f1d778fee75155d4ea07"
)

self.assertTrue(pub.verify(msg, valid_sig))
self.assertFalse(pub.verify(msg, invalid_sig))
self.assertEqual(sig.string(), valid_sig.string())
self.assertEqual(sig.raw_bytes(), valid_sig.raw_bytes())


if __name__ == "__main__":
unittest.main()
52 changes: 52 additions & 0 deletions tests/test_crypto_ed25519.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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


class TestEd25519Crypto(unittest.TestCase):
def test_private_key_to_public_key(self):
prv_str = "SECRET1RJ6STNTA7Y3P2QLQF8A6QCX05F2H5TFNE5RSH066KZME4WVFXKE7QW097LG"
expected_pub_str = (
"public1ry2cqw5yfhmr7ve8nctgzg6wgcyc73xqr2uud486jgsq7wu253egsx6msep"
)

prv = Ed25519PrivateKey.from_string(prv_str)
pub = prv.public_key()
pub_str = pub.string()

self.assertEqual(pub_str, expected_pub_str)

def test_public_key_to_address(self):
pub_str = "public1ry2cqw5yfhmr7ve8nctgzg6wgcyc73xqr2uud486jgsq7wu253egsx6msep"
expected_acc_addr_str = "pc1reer4damrrdxznmrrl7a9acy7x5cwe6dyt8ftv4"

pub = Ed25519PublicKey.from_string(pub_str)
acc_add_str = pub.account_address().string()

self.assertEqual(acc_add_str, expected_acc_addr_str)

def test_sign(self):
prv_str = "secret1r0up878rawjec2evjnd4k42a4g4pcardesjk48jtn64qwjnfv7veqal53e2"
prv = Ed25519PrivateKey.from_string(prv_str)
pub = prv.public_key()
msg = b"pactus"
sig = prv.sign(msg)

valid_sig = Ed25519Signature.from_string(
"55eaa9656158874bff726c8d62abe0a5b66d2434705f46b58c905a42f1b39fb95f640fbacac97e6e6862220fe7b4f249a0f79dc3d37b4460156c58580778b70e"
)

invalid_sig = Ed25519Signature.from_string(
"5a61ecb3c08825010678f12c036cec2e1dd1b8767ed9fd95a97f560dfd6196b600fdc7bba22f13eae19ca578b920eb807eb6cd956d55f1d778fee75155d4ea07"
)

self.assertTrue(pub.verify(msg, valid_sig))
self.assertFalse(pub.verify(msg, invalid_sig))
self.assertEqual(sig.string(), valid_sig.string())
self.assertEqual(sig.raw_bytes(), valid_sig.raw_bytes())


if __name__ == "__main__":
unittest.main()

0 comments on commit 035224c

Please sign in to comment.