Skip to content

Commit 2f5a52c

Browse files
committed
fix: BondPayload uses BLSPublicKey type with proper encode/decode
- Replace raw bytes with BLSPublicKey | None for public_key field - Fix encode: write varint size prefix (0 or 96) before key bytes - Fix decode: use BLSPublicKey.decode() when key is present - Update create_bond_tx signature to accept BLSPublicKey | None
1 parent dcadef6 commit 2f5a52c

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

pactus/transaction/payload/bond.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
from pactus.crypto.address import Address
4+
from pactus.crypto.bls.public_key import PublicKey as BLSPublicKey
25
from pactus.encoding import encoding
36
from pactus.types.amount import Amount
47

@@ -10,7 +13,7 @@ def __init__(
1013
self,
1114
sender: Address,
1215
receiver: Address,
13-
public_key: bytes,
16+
public_key: BLSPublicKey | None,
1417
stake: Amount,
1518
) -> None:
1619
self.sender = sender
@@ -21,7 +24,11 @@ def __init__(
2124
def encode(self, buf: bytes) -> bytes:
2225
buf = self.sender.encode(buf)
2326
buf = self.receiver.encode(buf)
24-
buf = encoding.append_fixed_bytes(buf, self.public_key)
27+
if self.public_key is not None:
28+
buf = encoding.append_var_int(buf, 96)
29+
buf = self.public_key.encode(buf)
30+
else:
31+
buf = encoding.append_var_int(buf, 0)
2532
return self.stake.encode(buf)
2633

2734
def get_type(self) -> PayloadType:
@@ -38,7 +45,7 @@ def decode(cls, buf: bytes) -> tuple:
3845
pub_key_size, buf = encoding.read_var_int(buf)
3946
public_key = None
4047
if pub_key_size == 96:
41-
public_key, buf = encoding.read_fixed_bytes(buf, 96)
48+
public_key, buf = BLSPublicKey.decode(buf)
4249
elif pub_key_size != 0:
4350
msg = f"invalid public key size: {pub_key_size}"
4451
raise ValueError(msg)

pactus/transaction/transaction.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pactus.crypto.address import Address, AddressType
24
from pactus.crypto.bls.public_key import PublicKey as BLSPublicKey
35
from pactus.crypto.bls.signature import Signature as BLSSignature
@@ -124,7 +126,7 @@ def create_transfer_tx(
124126
amount: Amount,
125127
fee: Amount,
126128
memo: str = "",
127-
) -> "Transaction":
129+
) -> Transaction:
128130
payload = TransferPayload(sender, receiver, amount)
129131
return cls(lock_time, fee, memo, payload)
130132

@@ -134,11 +136,11 @@ def create_bond_tx(
134136
lock_time: Height,
135137
sender: Address,
136138
receiver: Address,
137-
public_key: bytes,
139+
public_key: BLSPublicKey | None,
138140
fee: Amount,
139141
stake: Amount,
140142
memo: str = "",
141-
) -> "Transaction":
143+
) -> Transaction:
142144
payload = BondPayload(sender, receiver, public_key, stake)
143145
return cls(lock_time, fee, memo, payload)
144146

@@ -148,7 +150,7 @@ def create_unbond_tx(
148150
lock_time: Height,
149151
validator: Address,
150152
memo: str = "",
151-
) -> "Transaction":
153+
) -> Transaction:
152154
payload = UnbondPayload(validator)
153155
return cls(lock_time, 0, memo, payload)
154156

@@ -161,7 +163,7 @@ def create_withdraw_tx(
161163
amount: Amount,
162164
fee: Amount,
163165
memo: str = "",
164-
) -> "Transaction":
166+
) -> Transaction:
165167
payload = WithdrawPayload(from_addr, to_addr, amount)
166168
return cls(lock_time, fee, memo, payload)
167169

0 commit comments

Comments
 (0)