Move checksum address from filepath utility to classmethod on character config where is wants to be scoped in the wild

pull/1989/head
Kieran Prasch 2020-05-15 12:42:57 -07:00
parent 98e6a71740
commit 3272b64003
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
3 changed files with 40 additions and 57 deletions

View File

@ -20,12 +20,8 @@ along with nucypher. If not, see <https://www.gnu.org/licenses/>.
import json
import click
import os
import re
from eth_utils.address import is_checksum_address
from json.decoder import JSONDecodeError
from nucypher.blockchain.eth.clients import NuCypherGethGoerliProcess
from nucypher.characters.control.emitters import StdoutEmitter
from nucypher.cli.literature import (
CHARACTER_DESTRUCTION,
@ -36,8 +32,6 @@ from nucypher.cli.literature import (
SUCCESSFUL_FORGET_NODES,
SUCCESSFUL_UPDATE_CONFIGURATION_VALUES
)
from nucypher.config.characters import UrsulaConfiguration
from nucypher.config.node import CharacterConfiguration
def get_or_update_configuration(emitter: StdoutEmitter,
@ -102,49 +96,3 @@ def handle_missing_configuration_file(character_config_class,
f'\'nucypher {init_command}\''
raise click.FileError(filename=config_file_location, hint=message)
def get_provider_process(start_now: bool = False) -> NuCypherGethGoerliProcess:
"""Stage integrated ethereum node process"""
# TODO: Support domains and non-geth clients
process = NuCypherGethGoerliProcess()
if start_now:
process.start()
return process
def extract_checksum_address_from_filepath(filepath: str, config_class: CharacterConfiguration) -> str:
pattern = re.compile(r'''
(^\w+)-
(0x{1} # Then, 0x the start of the string, exactly once
[0-9a-fA-F]{40}) # Followed by exactly 40 hex chars
''',
re.VERBOSE)
filename = os.path.basename(filepath)
match = pattern.match(filename)
if match:
character_name, checksum_address = match.groups()
else:
# Extract from default by "peeking" inside the configuration file.
default_name = config_class.generate_filename()
if filename == default_name:
checksum_address = config_class.peek(filepath=filepath, field='checksum_address')
###########
# TODO: Cleanup and deprecate worker_address in config files, leaving only checksum_address
if config_class == UrsulaConfiguration:
federated = bool(config_class.peek(filepath=filepath, field='federated_only'))
if not federated:
checksum_address = config_class.peek(filepath=filepath, field='worker_address')
###########
else:
raise ValueError(f"Cannot extract checksum from filepath '{filepath}'")
if not is_checksum_address(checksum_address):
raise RuntimeError(f"Invalid checksum address detected in configuration file at '{filepath}'.")
return checksum_address

View File

@ -32,7 +32,6 @@ from nucypher.blockchain.eth.registry import InMemoryContractRegistry, Individua
from nucypher.blockchain.eth.signers import Signer
from nucypher.blockchain.eth.token import NU, Stake
from nucypher.characters.control.emitters import StdoutEmitter
from nucypher.cli.actions.config import extract_checksum_address_from_filepath
from nucypher.cli.literature import (
GENERIC_SELECT_ACCOUNT,
IS_THIS_CORRECT,
@ -239,8 +238,7 @@ def select_config_file(emitter: StdoutEmitter,
# Manual
#
parsed_addresses = {extract_checksum_address_from_filepath(fp, config_class=config_class): fp
for fp in config_files}
parsed_addresses = {config_class.checksum_address_from_filepath(fp): fp for fp in config_files}
try:
config_file = parsed_addresses[checksum_address]
except KeyError:
@ -252,8 +250,7 @@ def select_config_file(emitter: StdoutEmitter,
# Interactive
#
parsed_addresses = tuple([extract_checksum_address_from_filepath(fp, config_class=config_class)]
for fp in config_files)
parsed_addresses = tuple([config_class.checksum_address_from_filepath(fp)] for fp in config_files)
# Display account info
headers = ['Account']

View File

@ -226,6 +226,44 @@ class CharacterConfiguration(BaseConfiguration):
def __call__(self, **character_kwargs):
return self.produce(**character_kwargs)
@classmethod
def checksum_address_from_filepath(cls, filepath: str) -> str:
pattern = re.compile(r'''
(^\w+)-
(0x{1} # Then, 0x the start of the string, exactly once
[0-9a-fA-F]{40}) # Followed by exactly 40 hex chars
''',
re.VERBOSE)
filename = os.path.basename(filepath)
match = pattern.match(filename)
if match:
character_name, checksum_address = match.groups()
else:
# Extract from default by "peeking" inside the configuration file.
default_name = cls.generate_filename()
if filename == default_name:
checksum_address = cls.peek(filepath=filepath, field='checksum_address')
###########
# TODO: Cleanup and deprecate worker_address in config files, leaving only checksum_address
from nucypher.config.characters import UrsulaConfiguration
if isinstance(cls, UrsulaConfiguration):
federated = bool(cls.peek(filepath=filepath, field='federated_only'))
if not federated:
checksum_address = cls.peek(filepath=cls.filepath, field='worker_address')
###########
else:
raise ValueError(f"Cannot extract checksum from filepath '{filepath}'")
if not is_checksum_address(checksum_address):
raise RuntimeError(f"Invalid checksum address detected in configuration file at '{filepath}'.")
return checksum_address
def update(self, **kwargs) -> None:
"""
A facility for updating existing attributes on existing configuration instances.