Skip to content

Commit 68b5859

Browse files
committed
feat: add signature and public key for transactions
1 parent 9937704 commit 68b5859

3 files changed

Lines changed: 23 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,12 +18,17 @@ 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 pub.verify(bytes(tx.sign_bytes()), tx.signature):
27+
print("Signature verification succeeded")
28+
else:
29+
print("Signature verification failed")
2630

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

2833
if __name__ == "__main__":
2934
main()

examples/example_transfer_transaction_ed25519.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ 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 pub.verify(bytes(tx.sign_bytes()), tx.signature):
27+
print("Signature verification succeeded")
28+
else:
29+
print("Signature verification failed")
30+
2531
print(f"Signed transaction hex: {signed_tx.hex()}")
2632

2733

pactus/transaction/transaction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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
45
from pactus.encoding import encoding
6+
from pactus.crypto.signature import Signature
57

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

16+
FLAG_STRIPPED_PUBLIC_KEY = 0x01
17+
FLAG_NOT_SIGNED = 0x02
1418

1519
class Transaction:
1620
def __init__(
@@ -26,6 +30,8 @@ def __init__(
2630
self.version = 1
2731
self.fee = fee
2832
self.payload = payload
33+
self.public_key: PublicKey = None
34+
self.signature: Signature = None
2935

3036
@classmethod
3137
def create_transfer_tx(
@@ -129,4 +135,8 @@ def sign(self, private_key: PrivateKey) -> bytes:
129135
encoding.append_fixed_bytes(buf, sig.raw_bytes())
130136
encoding.append_fixed_bytes(buf, pub.raw_bytes())
131137

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

0 commit comments

Comments
 (0)