[#1] Add method get_private_key_from_wif
Signed-off-by: ilyas585 <niyazov2023@gmail.com>
This commit is contained in:
parent
480d288e2a
commit
97bc53c6f4
6 changed files with 68 additions and 0 deletions
0
frostfs_api/__init__.py
Normal file
0
frostfs_api/__init__.py
Normal file
11
frostfs_api/crypto/key_extension.py
Normal file
11
frostfs_api/crypto/key_extension.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
import base58
|
||||
|
||||
|
||||
class KeyExtension:
|
||||
def get_private_key_from_wif(self, wif: str):
|
||||
decoded = base58.b58decode_check(wif)
|
||||
if len(decoded) != 34 or decoded[0] != 0x80 or decoded[-1] != 0x01:
|
||||
raise ValueError("Incorrect WIF private key")
|
||||
|
||||
private_key = decoded[1:-1]
|
||||
return private_key
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
base58==2.1.1
|
||||
ecdsa==0.19.0
|
||||
pytest==8.3.4
|
20
tests/conftest.py
Normal file
20
tests/conftest.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
import pytest
|
||||
|
||||
from frostfs_api.crypto.key_extension import KeyExtension
|
||||
from frostfs_api.client.verifier import Verifier
|
||||
from frostfs_api.client.signer import Signer
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client_key_extension():
|
||||
return KeyExtension()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client_signer():
|
||||
return Signer()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client_verifier():
|
||||
return Verifier()
|
16
tests/crypto/test_key_extension.py
Normal file
16
tests/crypto/test_key_extension.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
from frostfs_api.crypto.key_extension import KeyExtension
|
||||
from tests.helpers.convert import convert_bytes_format_127_to_256
|
||||
|
||||
|
||||
class TestKeyExtension:
|
||||
WIF = "L1YS4myg3xHPvi3FHeLaEt7G8upwJaWL5YLV7huviuUtXFpzBMqZ"
|
||||
PRIVATE_KEY = [
|
||||
-128, -5, 30, -36, -118, 85, -67, -6, 81, 43, 93, -38, 106, 21, -88, 127, 15, 125, -79, -17, -40, 77, -15,
|
||||
122, -88, 72, 109, -47, 125, -80, -40, -38
|
||||
]
|
||||
|
||||
def test_get_private_key_from_wif_success(self, client_key_extension: KeyExtension):
|
||||
private_key = client_key_extension.get_private_key_from_wif(self.WIF)
|
||||
|
||||
assert len(private_key) == 32, f"Not correct len of private key, expected: 32 Actual: {len(private_key)} "
|
||||
assert private_key == bytes(convert_bytes_format_127_to_256(self.PRIVATE_KEY)), f"Not match private key, Expected: {self.PRIVATE_KEY}, Actual: {private_key}"
|
18
tests/helpers/convert.py
Normal file
18
tests/helpers/convert.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
def convert_bytes_format_127_to_256(numbers: list[int]):
|
||||
new_list = []
|
||||
for num in numbers:
|
||||
if num > 0:
|
||||
new_list.append(num)
|
||||
else:
|
||||
new_list.append(num + 256)
|
||||
return new_list
|
||||
|
||||
|
||||
def convert_bytes_format_256_to_127(numbers: list[int]):
|
||||
new_list = []
|
||||
for num in numbers:
|
||||
if num < 128:
|
||||
new_list.append(num)
|
||||
else:
|
||||
new_list.append(num - 256)
|
||||
return new_list
|
Loading…
Add table
Reference in a new issue