Skip to content

Commit c483743

Browse files
authored
feat: add signature and public key for transactions (#44)
1 parent 4ff0147 commit c483743

5 files changed

Lines changed: 27 additions & 2 deletions

File tree

examples/example_transfer_transaction_bls.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ def main() -> None:
1818
sec = PrivateKey.from_string(
1919
"TSECRET1PZF33H72N9PHXZATY5URH5SS4M33VVDFAWYVESL5JTLT9A00TNWKQYNGM6Z"
2020
)
21+
pub = sec.public_key()
2122

2223
tx = Transaction.create_transfer_tx(lock_time, sender, receiver, amount, fee, memo)
23-
signed_tx = tx.sign(sec)
24+
signed_data = tx.sign(sec)
2425

25-
print(f"Signed transaction hex: {signed_tx.hex()}")
26+
if not pub.verify(bytes(tx.sign_bytes()), tx.signature):
27+
print("Signature verification failed")
28+
exit(1)
29+
30+
print(f"Signed transaction hex: {signed_data.hex()}")
2631

2732

2833
if __name__ == "__main__":

examples/example_transfer_transaction_ed25519.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ def main() -> None:
1818
sec = PrivateKey.from_string(
1919
"TSECRET1RGLSGPYLQRVET27AZUVS9TSP8MPGF9LH4U4RKKARMCATFK9L0KUCS7DCC09"
2020
)
21+
pub = sec.public_key()
2122

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

26+
if not pub.verify(bytes(tx.sign_bytes()), tx.signature):
27+
print("Signature verification failed")
28+
exit(1)
29+
2530
print(f"Signed transaction hex: {signed_tx.hex()}")
2631

2732

pactus/amount.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def __eq__(self, other: "Amount") -> bool:
2626

2727
return False
2828

29+
def __hash__(self) -> int:
30+
return hash(self.value)
31+
2932
@classmethod
3033
def from_nano_pac(cls, a: int) -> "Amount":
3134
"""Store the value as NanoPAC in the Amount instance."""

pactus/transaction/transaction.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from pactus.amount import Amount
22
from pactus.crypto.address import Address
33
from pactus.crypto.private_key import PrivateKey
4+
from pactus.crypto.public_key import PublicKey
5+
from pactus.crypto.signature import Signature
46
from pactus.encoding import encoding
57

68
from ._payload import (
@@ -11,6 +13,9 @@
1113
WithdrawPayload,
1214
)
1315

16+
FLAG_STRIPPED_PUBLIC_KEY = 0x01
17+
FLAG_NOT_SIGNED = 0x02
18+
1419

1520
class Transaction:
1621
def __init__(
@@ -26,6 +31,8 @@ def __init__(
2631
self.version = 1
2732
self.fee = fee
2833
self.payload = payload
34+
self.public_key: PublicKey = None
35+
self.signature: Signature = None
2936

3037
@classmethod
3138
def create_transfer_tx(
@@ -129,4 +136,8 @@ def sign(self, private_key: PrivateKey) -> bytes:
129136
encoding.append_fixed_bytes(buf, sig.raw_bytes())
130137
encoding.append_fixed_bytes(buf, pub.raw_bytes())
131138

139+
self.public_key = pub
140+
self.signature = sig
141+
self.flags |= FLAG_NOT_SIGNED
142+
132143
return buf

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ ignore = [
5151
"D107",
5252
"D203",
5353
"D212",
54+
"TC001",
5455
"COM812",
5556
"ISC001",
5657
"D205",

0 commit comments

Comments
 (0)