Skip to content

Commit

Permalink
checkpoint: into main from release/2.4.2 @ bcbc7a7 (#18289)
Browse files Browse the repository at this point in the history
Source hash: bcbc7a7
Remaining commits: 4
  • Loading branch information
Starttoaster authored Jul 11, 2024
2 parents 9aed61d + d46076e commit d88ef3b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 38 deletions.
121 changes: 84 additions & 37 deletions chia/_tests/core/util/test_keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import random
from dataclasses import replace
from dataclasses import dataclass, replace
from typing import Callable, List, Optional, Tuple

import importlib_resources
Expand Down Expand Up @@ -32,21 +32,55 @@
mnemonic_to_seed,
)

mnemonic = (
"rapid this oven common drive ribbon bulb urban uncover napkin kitten usage enforce uncle unveil scene "
"apart wire mystery torch peanut august flee fantasy"

@dataclass
class KeyInfo:
mnemonic: str
entropy: bytes
private_key: PrivateKey
fingerprint: uint32
public_key: G1Element
bech32: str


_24keyinfo = KeyInfo(
mnemonic=(
"rapid this oven common drive ribbon bulb urban uncover napkin kitten usage enforce uncle unveil scene "
"apart wire mystery torch peanut august flee fantasy"
),
entropy=bytes.fromhex("b1fc1a7717343572077f7aecb25ded77c4a3d93b9e040a5f8649f2aa1e1e5632"),
private_key=PrivateKey.from_bytes(
bytes.fromhex("6c6bb4cc3dae03b8d0b327dd6765834464a883f7ca7df134970842055efe8afc")
),
fingerprint=uint32(1310648153),
public_key=G1Element.from_bytes(
bytes.fromhex(
"b5acf3599bc5fa5da1c00f6cc3d5bcf1560def67778b7f50a8c373a83f78761505b6250ab776e38a292e26628009aec4"
)
),
bech32="bls12381kkk0xkvmcha9mgwqpakv84du79tqmmm8w79h759gcde6s0mcwc2std39p2mhdcu29yhzvc5qpxhvgmknyl7",
)
entropy = bytes.fromhex("b1fc1a7717343572077f7aecb25ded77c4a3d93b9e040a5f8649f2aa1e1e5632")
private_key = PrivateKey.from_bytes(bytes.fromhex("6c6bb4cc3dae03b8d0b327dd6765834464a883f7ca7df134970842055efe8afc"))
fingerprint = uint32(1310648153)
public_key = G1Element.from_bytes(
bytes.fromhex("b5acf3599bc5fa5da1c00f6cc3d5bcf1560def67778b7f50a8c373a83f78761505b6250ab776e38a292e26628009aec4")
_12keyinfo = KeyInfo(
mnemonic=("steak rely trumpet cake banner easy consider cream marriage harvest truly shrimp"),
entropy=bytes.fromhex("d516afa61021248b8bd197884d2fa5e3"),
private_key=PrivateKey.from_bytes(
bytes.fromhex("3aaec6598281320c4918a2d6ebf4c2bacabad5f85a45569fc3ba5159e13f94bf")
),
fingerprint=uint32(688295223),
public_key=G1Element.from_bytes(
bytes.fromhex(
"a9e652cb551d5978a9ee4b7aa52a4e826078a54b08a3d903c38611cb8a804a9a29c926e4f8549314a079e04ecde10cc1"
)
),
bech32="bls12381148n99j64r4vh320wfda222jwsfs83f2tpz3ajq7rscguhz5qf2dznjfxunu9fyc55pu7qnkduyxvzqskawt",
)
bech32_pubkey = "bls12381kkk0xkvmcha9mgwqpakv84du79tqmmm8w79h759gcde6s0mcwc2std39p2mhdcu29yhzvc5qpxhvgmknyl7"


class TestKeychain:
def test_basic_add_delete(self, empty_temp_file_keyring: TempKeyring, seeded_random: random.Random):
@pytest.mark.parametrize("key_info", [_24keyinfo, _12keyinfo])
def test_basic_add_delete(
self, key_info: KeyInfo, empty_temp_file_keyring: TempKeyring, seeded_random: random.Random
):
kc: Keychain = Keychain(user="testing-1.8.0", service="chia-testing-1.8.0")
kc.delete_all_keys()

Expand All @@ -55,7 +89,7 @@ def test_basic_add_delete(self, empty_temp_file_keyring: TempKeyring, seeded_ran
assert kc.get_first_private_key() is None
assert kc.get_first_public_key() is None

mnemonic = generate_mnemonic()
mnemonic = key_info.mnemonic
entropy = bytes_from_mnemonic(mnemonic)
assert bytes_to_mnemonic(entropy) == mnemonic
mnemonic_2 = generate_mnemonic()
Expand Down Expand Up @@ -96,10 +130,10 @@ def test_basic_add_delete(self, empty_temp_file_keyring: TempKeyring, seeded_ran
assert kc._get_free_private_key_index() == 0
assert len(kc.get_all_private_keys()) == 0

kc.add_key(bech32_pubkey, label=None, private=False)
kc.add_key(key_info.bech32, label=None, private=False)
all_pks = kc.get_all_public_keys()
assert len(all_pks) == 1
assert all_pks[0] == public_key
assert all_pks[0] == key_info.public_key
kc.delete_all_keys()

kc.add_key(bytes_to_mnemonic(bytes32.random(seeded_random)))
Expand Down Expand Up @@ -219,14 +253,17 @@ def test_key_data_secrets_generate() -> None:


@pytest.mark.parametrize(
"input_data, from_method", [(mnemonic, KeyDataSecrets.from_mnemonic), (entropy, KeyDataSecrets.from_entropy)]
"get_item, from_method", [("mnemonic", KeyDataSecrets.from_mnemonic), ("entropy", KeyDataSecrets.from_entropy)]
)
def test_key_data_secrets_creation(input_data: object, from_method: Callable[..., KeyDataSecrets]) -> None:
secrets = from_method(input_data)
assert secrets.mnemonic == mnemonic.split()
assert secrets.mnemonic_str() == mnemonic
assert secrets.entropy == entropy
assert secrets.private_key == private_key
@pytest.mark.parametrize("key_info", [_24keyinfo, _12keyinfo])
def test_key_data_secrets_creation(
key_info: KeyInfo, get_item: str, from_method: Callable[..., KeyDataSecrets]
) -> None:
secrets = from_method(getattr(key_info, get_item))
assert secrets.mnemonic == key_info.mnemonic.split()
assert secrets.mnemonic_str() == key_info.mnemonic
assert secrets.entropy == key_info.entropy
assert secrets.private_key == key_info.private_key


@pytest.mark.parametrize("label", [None, "key"])
Expand All @@ -241,21 +278,23 @@ def test_key_data_generate(label: Optional[str]) -> None:

@pytest.mark.parametrize("label", [None, "key"])
@pytest.mark.parametrize(
"input_data, from_method", [(mnemonic, KeyData.from_mnemonic), (entropy, KeyData.from_entropy)]
"get_item, from_method", [("mnemonic", KeyData.from_mnemonic), ("entropy", KeyData.from_entropy)]
)
def test_key_data_creation(input_data: object, from_method: Callable[..., KeyData], label: Optional[str]) -> None:
key_data = from_method(input_data, label)
assert key_data.fingerprint == fingerprint
assert key_data.public_key == public_key
assert key_data.mnemonic == mnemonic.split()
assert key_data.mnemonic_str() == mnemonic
assert key_data.entropy == entropy
assert key_data.private_key == private_key
@pytest.mark.parametrize("key_info", [_24keyinfo, _12keyinfo])
def test_key_data_creation(label: str, key_info: KeyInfo, get_item: str, from_method: Callable[..., KeyData]) -> None:
key_data = from_method(getattr(key_info, get_item), label)
assert key_data.fingerprint == key_info.fingerprint
assert key_data.public_key == key_info.public_key
assert key_data.mnemonic == key_info.mnemonic.split()
assert key_data.mnemonic_str() == key_info.mnemonic
assert key_data.entropy == key_info.entropy
assert key_data.private_key == key_info.private_key
assert key_data.label == label


def test_key_data_without_secrets() -> None:
key_data = KeyData(fingerprint, public_key, None, None)
@pytest.mark.parametrize("key_info", [_24keyinfo, _12keyinfo])
def test_key_data_without_secrets(key_info: KeyInfo) -> None:
key_data = KeyData(key_info.fingerprint, key_info.public_key, None, None)
assert key_data.secrets is None

with pytest.raises(KeychainSecretsMissing):
Expand All @@ -274,9 +313,9 @@ def test_key_data_without_secrets() -> None:
@pytest.mark.parametrize(
"input_data, data_type",
[
((mnemonic.split()[:-1], entropy, private_key), "mnemonic"),
((mnemonic.split(), KeyDataSecrets.generate().entropy, private_key), "entropy"),
((mnemonic.split(), entropy, KeyDataSecrets.generate().private_key), "private_key"),
((_24keyinfo.mnemonic.split()[:-1], _24keyinfo.entropy, _24keyinfo.private_key), "mnemonic"),
((_24keyinfo.mnemonic.split(), KeyDataSecrets.generate().entropy, _24keyinfo.private_key), "entropy"),
((_24keyinfo.mnemonic.split(), _24keyinfo.entropy, KeyDataSecrets.generate().private_key), "private_key"),
],
)
def test_key_data_secrets_post_init(input_data: Tuple[List[str], bytes, PrivateKey], data_type: str) -> None:
Expand All @@ -287,8 +326,16 @@ def test_key_data_secrets_post_init(input_data: Tuple[List[str], bytes, PrivateK
@pytest.mark.parametrize(
"input_data, data_type",
[
((fingerprint, G1Element(), None, KeyDataSecrets(mnemonic.split(), entropy, private_key)), "public_key"),
((fingerprint, G1Element(), None, None), "fingerprint"),
(
(
_24keyinfo.fingerprint,
G1Element(),
None,
KeyDataSecrets(_24keyinfo.mnemonic.split(), _24keyinfo.entropy, _24keyinfo.private_key),
),
"public_key",
),
((_24keyinfo.fingerprint, G1Element(), None, None), "fingerprint"),
],
)
def test_key_data_post_init(
Expand Down
2 changes: 1 addition & 1 deletion chia/util/keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def _get_key_data(self, index: int, include_secrets: bool = True) -> KeyData:

public_key = G1Element.from_bytes(str_bytes[: G1Element.SIZE])
fingerprint = public_key.get_fingerprint()
if len(str_bytes) == G1Element.SIZE + 32:
if len(str_bytes) > G1Element.SIZE:
entropy = str_bytes[G1Element.SIZE : G1Element.SIZE + 32]
else:
entropy = None
Expand Down

0 comments on commit d88ef3b

Please sign in to comment.