Skip to content

Commit c75ed0a

Browse files
authored
feat: Update PublicKey To Key In Node Create And Update (#2457)
Signed-off-by: aceppaluni <aceppaluni@gmail.com>
1 parent be42552 commit c75ed0a

4 files changed

Lines changed: 84 additions & 16 deletions

File tree

src/hiero_sdk_python/nodes/node_create_transaction.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from hiero_sdk_python.account.account_id import AccountId
88
from hiero_sdk_python.address_book.endpoint import Endpoint
99
from hiero_sdk_python.channels import _Channel
10-
from hiero_sdk_python.crypto.public_key import PublicKey
10+
from hiero_sdk_python.crypto.key import Key
1111
from hiero_sdk_python.executable import _Method
1212
from hiero_sdk_python.hapi.services.node_create_pb2 import NodeCreateTransactionBody
1313
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
@@ -29,7 +29,7 @@ class NodeCreateParams:
2929
service_endpoints (list[Endpoint]): The service endpoints of the node.
3030
gossip_ca_certificate (bytes, optional): The gossip ca certificate of the node.
3131
grpc_certificate_hash (bytes, optional): The grpc certificate hash of the node.
32-
admin_key (PublicKey, optional): The admin key of the node.
32+
admin_key (Key, optional): The admin key of the node.
3333
decline_reward (bool, optional): The decline reward of the node.
3434
grpc_web_proxy_endpoint (Endpoint, optional): The grpc web proxy endpoint of the node.
3535
"""
@@ -40,7 +40,7 @@ class NodeCreateParams:
4040
service_endpoints: list[Endpoint] = field(default_factory=list)
4141
gossip_ca_certificate: bytes | None = None
4242
grpc_certificate_hash: bytes | None = None
43-
admin_key: PublicKey | None = None
43+
admin_key: Key | None = None
4444
decline_reward: bool | None = None
4545
grpc_web_proxy_endpoint: Endpoint | None = None
4646
associated_registered_nodes: list[int] = field(default_factory=list)
@@ -73,7 +73,7 @@ def __init__(self, node_create_params: NodeCreateParams | None = None):
7373
self.service_endpoints: list[Endpoint] = node_create_params.service_endpoints
7474
self.gossip_ca_certificate: bytes | None = node_create_params.gossip_ca_certificate
7575
self.grpc_certificate_hash: bytes | None = node_create_params.grpc_certificate_hash
76-
self.admin_key: PublicKey | None = node_create_params.admin_key
76+
self.admin_key: Key | None = node_create_params.admin_key
7777
self.decline_reward: bool | None = node_create_params.decline_reward
7878
self.grpc_web_proxy_endpoint: Endpoint | None = node_create_params.grpc_web_proxy_endpoint
7979
self.associated_registered_nodes: list[int] = node_create_params.associated_registered_nodes
@@ -168,12 +168,12 @@ def set_grpc_certificate_hash(self, grpc_certificate_hash: bytes | None) -> Node
168168
self.grpc_certificate_hash = grpc_certificate_hash
169169
return self
170170

171-
def set_admin_key(self, admin_key: PublicKey | None) -> NodeCreateTransaction:
171+
def set_admin_key(self, admin_key: Key | None) -> NodeCreateTransaction:
172172
"""
173173
Sets the admin key for this node create transaction.
174174
175175
Args:
176-
admin_key (PublicKey):
176+
admin_key (Key):
177177
The admin key of the node.
178178
179179
Returns:
@@ -255,7 +255,7 @@ def _build_proto_body(self) -> NodeCreateTransactionBody:
255255
service_endpoint=[endpoint._to_proto() for endpoint in self.service_endpoints or []],
256256
gossip_ca_certificate=self.gossip_ca_certificate,
257257
grpc_certificate_hash=self.grpc_certificate_hash,
258-
admin_key=self.admin_key._to_proto() if self.admin_key else None,
258+
admin_key=self.admin_key.to_proto_key() if self.admin_key else None,
259259
decline_reward=self.decline_reward,
260260
grpc_proxy_endpoint=(self.grpc_web_proxy_endpoint._to_proto() if self.grpc_web_proxy_endpoint else None),
261261
associated_registered_node=self.associated_registered_nodes,
@@ -317,7 +317,7 @@ def _from_protobuf(cls, transaction_body, body_bytes: bytes, sig_map):
317317
if pb.grpc_certificate_hash:
318318
transaction.grpc_certificate_hash = pb.grpc_certificate_hash
319319
if pb.HasField("admin_key"):
320-
transaction.admin_key = PublicKey._from_proto(pb.admin_key)
320+
transaction.admin_key = Key.from_proto_key(pb.admin_key)
321321
if pb.decline_reward:
322322
transaction.decline_reward = pb.decline_reward
323323
if pb.HasField("grpc_proxy_endpoint"):

src/hiero_sdk_python/nodes/node_update_transaction.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from hiero_sdk_python.account.account_id import AccountId
1111
from hiero_sdk_python.address_book.endpoint import Endpoint
1212
from hiero_sdk_python.channels import _Channel
13-
from hiero_sdk_python.crypto.public_key import PublicKey
13+
from hiero_sdk_python.crypto.key import Key
1414
from hiero_sdk_python.executable import _Method
1515
from hiero_sdk_python.hapi.services.node_update_pb2 import (
1616
AssociatedRegisteredNodeList,
@@ -36,7 +36,7 @@ class NodeUpdateParams:
3636
service_endpoints (list[Endpoint]): The service endpoints of the node.
3737
gossip_ca_certificate (bytes, None): The gossip ca certificate of the node.
3838
grpc_certificate_hash (bytes, None): The grpc certificate hash of the node.
39-
admin_key (PublicKey, optional): The admin key of the node.
39+
admin_key (Key, optional): The admin key of the node.
4040
decline_reward (bool, optional): The decline reward of the node.
4141
grpc_web_proxy_endpoint (Endpoint, optional): The grpc web proxy endpoint of the node.
4242
"""
@@ -48,7 +48,7 @@ class NodeUpdateParams:
4848
service_endpoints: list[Endpoint] = field(default_factory=list)
4949
gossip_ca_certificate: bytes | None = None
5050
grpc_certificate_hash: bytes | None = None
51-
admin_key: PublicKey | None = None
51+
admin_key: Key | None = None
5252
decline_reward: bool | None = None
5353
grpc_web_proxy_endpoint: Endpoint | None = None
5454
associated_registered_nodes: list[int] | None = None
@@ -82,7 +82,7 @@ def __init__(self, node_update_params: NodeUpdateParams | None = None):
8282
self.service_endpoints: list[Endpoint] = node_update_params.service_endpoints
8383
self.gossip_ca_certificate: bytes | None = node_update_params.gossip_ca_certificate
8484
self.grpc_certificate_hash: bytes | None = node_update_params.grpc_certificate_hash
85-
self.admin_key: PublicKey | None = node_update_params.admin_key
85+
self.admin_key: Key | None = node_update_params.admin_key
8686
self.decline_reward: bool | None = node_update_params.decline_reward
8787
self.grpc_web_proxy_endpoint: Endpoint | None = node_update_params.grpc_web_proxy_endpoint
8888
self.associated_registered_nodes: list[int] | None = node_update_params.associated_registered_nodes
@@ -192,12 +192,12 @@ def set_grpc_certificate_hash(self, grpc_certificate_hash: bytes | None) -> Node
192192
self.grpc_certificate_hash = grpc_certificate_hash
193193
return self
194194

195-
def set_admin_key(self, admin_key: PublicKey | None) -> NodeUpdateTransaction:
195+
def set_admin_key(self, admin_key: Key | None) -> NodeUpdateTransaction:
196196
"""
197197
Sets the admin key for this node update transaction.
198198
199199
Args:
200-
admin_key (PublicKey):
200+
admin_key (Key):
201201
The admin key of the node.
202202
203203
Returns:
@@ -306,7 +306,7 @@ def _build_proto_body(self) -> NodeUpdateTransactionBody:
306306
grpc_certificate_hash=(
307307
BytesValue(value=self.grpc_certificate_hash) if self.grpc_certificate_hash is not None else None
308308
),
309-
admin_key=self._convert_to_proto(self.admin_key),
309+
admin_key=self.admin_key.to_proto_key() if self.admin_key else None,
310310
decline_reward=(BoolValue(value=self.decline_reward) if self.decline_reward is not None else None),
311311
grpc_proxy_endpoint=self._convert_to_proto(self.grpc_web_proxy_endpoint),
312312
)
@@ -377,7 +377,7 @@ def _from_protobuf(cls, transaction_body, body_bytes: bytes, sig_map):
377377
if pb.HasField("grpc_certificate_hash"):
378378
transaction.grpc_certificate_hash = pb.grpc_certificate_hash.value
379379
if pb.HasField("admin_key"):
380-
transaction.admin_key = PublicKey._from_proto(pb.admin_key)
380+
transaction.admin_key = Key.from_proto_key(pb.admin_key)
381381
if pb.HasField("decline_reward"):
382382
transaction.decline_reward = pb.decline_reward.value
383383
if pb.HasField("grpc_proxy_endpoint"):

tests/unit/node_create_transaction_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
1515
SchedulableTransactionBody,
1616
)
17+
from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody
1718
from hiero_sdk_python.nodes.node_create_transaction import (
1819
NodeCreateParams,
1920
NodeCreateTransaction,
@@ -330,3 +331,36 @@ def test_to_proto(mock_client):
330331

331332
assert proto.signedTransactionBytes
332333
assert len(proto.signedTransactionBytes) > 0
334+
335+
336+
def test_node_create_transaction_build_with_private_key():
337+
"""NodeCreateTransaction should accept PrivateKey and serialize the PublicKey in the proto."""
338+
private_key = PrivateKey.generate()
339+
public_key = private_key.public_key()
340+
341+
tx = NodeCreateTransaction().set_admin_key(private_key)
342+
343+
body = tx._build_proto_body()
344+
345+
# In the proto we should have the public key not the private one
346+
assert body.admin_key.ed25519 == public_key.to_bytes_raw()
347+
348+
349+
def test_node_create_transaction_from_protobuf_with_admin_key():
350+
"""NodeCreateTransaction should deserialize the admin_key from the proto correctly."""
351+
private_key = PrivateKey.generate()
352+
353+
transaction = NodeCreateTransaction().set_admin_key(private_key)
354+
body = transaction._build_proto_body()
355+
356+
transaction_body = TransactionBody()
357+
transaction_body.nodeCreate.CopyFrom(body)
358+
359+
parsed_transaction = NodeCreateTransaction._from_protobuf(
360+
transaction_body,
361+
b"",
362+
None,
363+
)
364+
365+
assert parsed_transaction.admin_key is not None
366+
assert parsed_transaction.admin_key.to_proto_key() == body.admin_key

tests/unit/node_update_transaction_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
1515
SchedulableTransactionBody,
1616
)
17+
from hiero_sdk_python.hapi.services.transaction_pb2 import TransactionBody
1718
from hiero_sdk_python.nodes.node_update_transaction import (
1819
NodeUpdateParams,
1920
NodeUpdateTransaction,
@@ -351,3 +352,36 @@ def test_to_proto(mock_client):
351352

352353
assert proto.signedTransactionBytes
353354
assert len(proto.signedTransactionBytes) > 0
355+
356+
357+
def test_node_update_transaction_build_with_private_key():
358+
"""NodeUpdateTransaction should accept PrivateKey and serialize the PublicKey in the proto."""
359+
private_key = PrivateKey.generate()
360+
public_key = private_key.public_key()
361+
362+
tx = NodeUpdateTransaction().set_admin_key(private_key)
363+
364+
body = tx._build_proto_body()
365+
366+
# In the proto we should have the public key not the private one
367+
assert body.admin_key.ed25519 == public_key.to_bytes_raw()
368+
369+
370+
def test_node_update_transaction_from_protobuf_with_admin_key():
371+
"""NodeUpdateTransaction should deserialize the admin_key from the proto correctly."""
372+
private_key = PrivateKey.generate()
373+
374+
transaction = NodeUpdateTransaction().set_admin_key(private_key)
375+
body = transaction._build_proto_body()
376+
377+
transaction_body = TransactionBody()
378+
transaction_body.nodeUpdate.CopyFrom(body)
379+
380+
parsed_transaction = NodeUpdateTransaction._from_protobuf(
381+
transaction_body,
382+
b"",
383+
None,
384+
)
385+
386+
assert parsed_transaction.admin_key is not None
387+
assert parsed_transaction.admin_key.to_proto_key() == body.admin_key

0 commit comments

Comments
 (0)