-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support compressed public key format * Add to_dict() and from_dict() to KeyWallet * Introduce public_key_to_address() and convert_public_key_format()
- Loading branch information
1 parent
a90a887
commit 6982c92
Showing
2 changed files
with
149 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from typing import Union, Dict | ||
|
||
import pytest | ||
|
||
from iconsdk.wallet.wallet import KeyWallet, public_key_to_address, convert_public_key_format | ||
|
||
|
||
class TestKeyWallet: | ||
|
||
@pytest.mark.parametrize( | ||
"compressed,hexadecimal,ret_type,size", | ||
( | ||
(True, True, str, 33), | ||
(True, False, bytes, 33), | ||
(False, True, str, 65), | ||
(False, False, bytes, 65), | ||
) | ||
) | ||
def test_get_public_key(self, compressed: bool, hexadecimal: bool, ret_type: type, size: int): | ||
wallet: KeyWallet = KeyWallet.create() | ||
public_key: Union[str, bytes] = wallet.get_public_key(compressed, hexadecimal) | ||
assert isinstance(public_key, ret_type) | ||
if hexadecimal: | ||
print(public_key) | ||
pub_key: bytes = bytes.fromhex(public_key) | ||
assert len(pub_key) == size | ||
else: | ||
assert len(public_key) == size | ||
|
||
def test_get_private_key(self): | ||
wallet: KeyWallet = KeyWallet.create() | ||
private_key: str = wallet.get_private_key() | ||
assert isinstance(private_key, str) | ||
assert not private_key.startswith("0x") | ||
|
||
private_key: bytes = wallet.get_private_key(hexadecimal=False) | ||
assert isinstance(private_key, bytes) | ||
assert wallet.private_key == private_key | ||
|
||
def test_private_key(self): | ||
wallet: KeyWallet = KeyWallet.create() | ||
wallet2: KeyWallet = KeyWallet.load(wallet.private_key) | ||
assert wallet == wallet2 | ||
|
||
wallet3 = KeyWallet.create() | ||
assert wallet != wallet3 | ||
|
||
def test_public_key(self): | ||
wallet: KeyWallet = KeyWallet.create() | ||
public_key: bytes = wallet.public_key | ||
assert isinstance(public_key, bytes) | ||
assert len(public_key) == 65 | ||
|
||
def test_to_dict(self): | ||
password = "1234" | ||
wallet: KeyWallet = KeyWallet.create() | ||
jso: Dict[str, str] = wallet.to_dict(password) | ||
assert jso["address"] == wallet.get_address() | ||
assert jso["coinType"] == "icx" | ||
|
||
wallet2 = KeyWallet.from_dict(jso, password) | ||
assert wallet2 == wallet | ||
|
||
|
||
def test_public_key_to_address(): | ||
wallet: KeyWallet = KeyWallet.create() | ||
address: str = public_key_to_address(wallet.public_key) | ||
assert address == wallet.get_address() | ||
|
||
compressed_public_key: bytes = wallet.get_public_key(compressed=True, hexadecimal=False) | ||
address2: str = public_key_to_address(compressed_public_key) | ||
assert address2 == wallet.get_address() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"iformat,oformat,size", | ||
( | ||
(True, True, 33), | ||
(True, False, 65), | ||
(False, True, 33), | ||
(False, False, 65), | ||
) | ||
) | ||
def test_convert_public_key_format(iformat: bool, oformat: bool, size: int): | ||
wallet: KeyWallet = KeyWallet.create() | ||
|
||
public_key: bytes = wallet.get_public_key(compressed=iformat, hexadecimal=False) | ||
ret: bytes = convert_public_key_format(public_key, compressed=oformat) | ||
if iformat == oformat: | ||
assert ret == public_key | ||
assert len(ret) == size | ||
assert public_key_to_address(public_key) == public_key_to_address(ret) |