Skip to content
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
9 changes: 7 additions & 2 deletions examples/example_transfer_transaction_bls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ def main() -> None:
sec = PrivateKey.from_string(
"TSECRET1PZF33H72N9PHXZATY5URH5SS4M33VVDFAWYVESL5JTLT9A00TNWKQYNGM6Z"
)
pub = sec.public_key()

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

print(f"Signed transaction hex: {signed_tx.hex()}")
if not pub.verify(bytes(tx.sign_bytes()), tx.signature):
print("Signature verification failed")
exit(1)

print(f"Signed transaction hex: {signed_data.hex()}")


if __name__ == "__main__":
Expand Down
5 changes: 5 additions & 0 deletions examples/example_transfer_transaction_ed25519.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ def main() -> None:
sec = PrivateKey.from_string(
"TSECRET1RGLSGPYLQRVET27AZUVS9TSP8MPGF9LH4U4RKKARMCATFK9L0KUCS7DCC09"
)
pub = sec.public_key()

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

if not pub.verify(bytes(tx.sign_bytes()), tx.signature):
print("Signature verification failed")
exit(1)

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


Expand Down
3 changes: 3 additions & 0 deletions pactus/amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def __eq__(self, other: "Amount") -> bool:

return False

def __hash__(self) -> int:
return hash(self.value)

@classmethod
def from_nano_pac(cls, a: int) -> "Amount":
"""Store the value as NanoPAC in the Amount instance."""
Expand Down
11 changes: 11 additions & 0 deletions pactus/transaction/transaction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 ._payload import (
Expand All @@ -11,6 +13,9 @@
WithdrawPayload,
)

FLAG_STRIPPED_PUBLIC_KEY = 0x01
FLAG_NOT_SIGNED = 0x02


class Transaction:
def __init__(
Expand All @@ -26,6 +31,8 @@ def __init__(
self.version = 1
self.fee = fee
self.payload = payload
self.public_key: PublicKey = None
self.signature: Signature = None

@classmethod
def create_transfer_tx(
Expand Down Expand Up @@ -129,4 +136,8 @@ def sign(self, private_key: PrivateKey) -> bytes:
encoding.append_fixed_bytes(buf, sig.raw_bytes())
encoding.append_fixed_bytes(buf, pub.raw_bytes())

self.public_key = pub
self.signature = sig
self.flags |= FLAG_NOT_SIGNED

return buf
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ignore = [
"D107",
"D203",
"D212",
"TC001",
"COM812",
"ISC001",
"D205",
Expand Down
Loading