Skip to content

Commit dc2db79

Browse files
committed
feat: specific to string and to bytes private key
Signed-off-by: exploreriii <[email protected]>
1 parent a87e4c5 commit dc2db79

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/hiero_sdk_python/crypto/private_key.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,25 @@ def to_bytes_raw(self) -> bytes:
288288
# ECDSA => integer scalar in big-endian
289289
return self._private_key.private_numbers().private_value.to_bytes(32, "big")
290290

291-
def to_string(self) -> str:
291+
def to_bytes_ed25519_raw(self) -> bytes:
292292
"""
293-
Returns the private key as a hex string (raw).
294-
Matches old usage that calls to_string().
293+
Return the raw 32-byte seed (Ed25519).
295294
"""
296-
return self.to_string_raw()
295+
if self.is_ed25519():
296+
return self._private_key.private_bytes(
297+
encoding=serialization.Encoding.Raw,
298+
format=serialization.PrivateFormat.Raw,
299+
encryption_algorithm=serialization.NoEncryption()
300+
)
301+
raise ValueError("Not Ed25519).")
297302

298-
def to_string_raw(self) -> str:
299-
return self.to_bytes_raw().hex()
303+
def to_bytes_ecdsa_raw(self) -> bytes:
304+
"""
305+
Return the raw 32-byte seed (ecdsa).
306+
"""
307+
if self.is_ecdsa():
308+
return self._private_key.private_numbers().private_value.to_bytes(32, "big")
309+
raise ValueError("Not ecdsa).")
300310

301311
def to_bytes_der(self) -> bytes:
302312
"""
@@ -307,10 +317,25 @@ def to_bytes_der(self) -> bytes:
307317
format=serialization.PrivateFormat.PKCS8,
308318
encryption_algorithm=serialization.NoEncryption()
309319
)
320+
321+
def to_string_raw(self) -> str:
322+
return self.to_bytes_raw().hex()
323+
324+
def to_string_ed25519_raw(self) -> str:
325+
return self.to_bytes_ed25519_raw().hex()
310326

327+
def to_string_ecdsa_raw(self) -> str:
328+
return self.to_bytes_ecdsa_raw().hex()
329+
311330
def to_string_der(self) -> str:
312331
return self.to_bytes_der().hex()
313332

333+
def to_string(self) -> str:
334+
"""
335+
Returns the private key as a hex string (raw).
336+
Matches old usage that calls to_string().
337+
"""
338+
return self.to_string_raw()
314339
#
315340
# ---------------------------------
316341
# is_ed25519 / is_ecdsa checks

tests/test_keys_private.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def test_generate_ed25519():
2929
pub.verify(sig, data) # should succeed if signature is valid
3030

3131
# Check round-trip raw bytes
32-
raw_bytes = priv.to_bytes_raw()
32+
raw_bytes = priv.to_bytes_ed25519_raw()
3333
assert len(raw_bytes) == 32
3434

3535
# Check hex string conversion
36-
raw_hex = priv.to_string_raw()
36+
raw_hex = priv.to_string_ed25519_raw()
3737
assert len(raw_hex) == 64
3838
assert raw_hex == raw_bytes.hex()
3939

@@ -68,11 +68,11 @@ def test_generate_ecdsa():
6868
pub.verify(sig, data) # should succeed
6969

7070
# Check raw bytes
71-
raw_bytes = priv.to_bytes_raw()
71+
raw_bytes = priv.to_bytes_ecdsa_raw()
7272
assert len(raw_bytes) == 32
7373

7474
# Check hex string
75-
raw_hex = priv.to_string_raw()
75+
raw_hex = priv.to_string_ecdsa_raw()
7676
assert len(raw_hex) == 64
7777
assert raw_hex == raw_bytes.hex()
7878

@@ -167,7 +167,7 @@ def test_from_string_der_ecdsa_round_trip():
167167
assert loaded.is_ecdsa()
168168

169169
# 4) Confirm the raw scalar round-trips perfectly
170-
assert loaded.to_bytes_raw() == scalar_one
170+
assert loaded.to_bytes_ecdsa_raw() == scalar_one
171171

172172

173173
def test_from_string_der_invalid_hex():
@@ -333,7 +333,7 @@ def test_from_bytes_ambiguity_prefers_ecdsa_when_ed25519_fails(monkeypatch):
333333

334334
assert priv.is_ecdsa(), "from_bytes should have returned an ECDSA key"
335335
# And round-trip raw scalar
336-
assert priv.to_bytes_raw() == ecdsa_scalar_one
336+
assert priv.to_bytes_ecdsa_raw() == ecdsa_scalar_one
337337

338338

339339
def test_type_checks_are_mutually_exclusive():

0 commit comments

Comments
 (0)