def make_card(general_config, character_options, config_file, nickname): emitter = setup_emitter(general_config) BOB = character_options.create_character(emitter, config_file, json_ipc=False) card = Card.from_character(BOB) if nickname: card.nickname = nickname card.save(overwrite=True) emitter.message(f"Saved new character card to {card.filepath}", color='green') paint_single_card(card=card, emitter=emitter)
def make_card(general_config, character_options, config_file, nickname): """Create a character card file for public key sharing""" emitter = setup_emitter(general_config) ALICE = character_options.create_character(emitter, config_file, general_config.json_ipc, load_seednodes=False) card = Card.from_character(ALICE) if nickname: card.nickname = nickname card.save(overwrite=True) emitter.message(f"Saved new character card to {card.filepath}", color='green') paint_single_card(card=card, emitter=emitter)
def show(query, qrcode): """ Lookup and view existing character card QUERY can be either the card id or nickname. """ emitter = StdoutEmitter() try: card = select_card(emitter=emitter, card_identifier=query) except Card.UnknownCard as e: return emitter.error(str(e)) paint_single_card(emitter=emitter, card=card, qrcode=qrcode)
def create(character_flag, verifying_key, encrypting_key, nickname, force): """Store a new character card""" emitter = StdoutEmitter() # Validate if not all((character_flag, verifying_key, encrypting_key)) and force: emitter.error( f'--verifying-key, --encrypting-key, and --type are required with --force enabled.' ) # Card type from constant_sorrow.constants import ALICE, BOB flags = {'a': ALICE, 'b': BOB} if not character_flag: choice = click.prompt('Enter Card Type - (A)lice or (B)ob', type=click.Choice(['a', 'b'], case_sensitive=False)) character_flag = flags[choice] else: character_flag = flags[character_flag] # Verifying Key if not verifying_key: verifying_key = click.prompt('Enter Verifying Key', type=UMBRAL_PUBLIC_KEY_HEX) verifying_key = bytes.fromhex(verifying_key) # TODO: Move / Validate # Encrypting Key if character_flag is BOB: if not encrypting_key: encrypting_key = click.prompt('Enter Encrypting Key', type=UMBRAL_PUBLIC_KEY_HEX) encrypting_key = bytes.fromhex(encrypting_key) # TODO: Move / Validate # Init new_card = Card(character_flag=character_flag, verifying_key=verifying_key, encrypting_key=encrypting_key, nickname=nickname) # Nickname if not force and not nickname: card_id_hex = new_card.id.hex() nickname = click.prompt('Enter nickname for card', default=card_id_hex) if nickname != card_id_hex: # not the default nickname = nickname.strip() new_card.nickname = nickname # Save new_card.save() emitter.message(f'Saved new card {new_card}', color='green') paint_single_card(emitter=emitter, card=new_card)
def collect_keys_from_card(emitter: StdoutEmitter, card_identifier: str, force: bool): emitter.message(f"Searching contacts for {card_identifier}\n", color='yellow') card = Card.load(identifier=card_identifier) if card.character is not Bob: emitter.error('Grantee card is not a Bob.') raise click.Abort paint_single_card(emitter=emitter, card=card) if not force: click.confirm('Is this the correct grantee (Bob)?', abort=True) bob_encrypting_key = bytes(card.encrypting_key).hex() bob_verifying_key = bytes(card.verifying_key).hex() public_keys = PublicKeys(encrypting_key=bob_encrypting_key, verifying_key=bob_verifying_key) return public_keys
def grant(general_config, bob, bob_encrypting_key, bob_verifying_key, label, value, rate, expiration, m, n, character_options, config_file, force): """Create and enact an access policy for some Bob. """ # Setup emitter = setup_emitter(general_config) ALICE = character_options.create_character(emitter, config_file, general_config.json_ipc) # Policy option validation if ALICE.federated_only: if any((value, rate)): message = "Can't use --value or --rate with a federated Alice." raise click.BadOptionUsage(option_name="--value, --rate", message=message) elif bool(value) and bool(rate): raise click.BadOptionUsage(option_name="--rate", message="Can't use --value if using --rate") # Grantee selection if bob and any((bob_encrypting_key, bob_verifying_key)): message = '--bob cannot be used with --bob-encrypting-key or --bob-veryfying key' raise click.BadOptionUsage(option_name='--bob', message=message) if bob: card = Card.load(identifier=bob) if card.character is not Bob: emitter.error('Grantee card is not a Bob.') raise click.Abort paint_single_card(emitter=emitter, card=card) if not force: click.confirm('Is this the correct grantee (Bob)?', abort=True) bob_encrypting_key = card.encrypting_key.hex() bob_verifying_key = card.verifying_key.hex() # Interactive collection follows: # TODO: Extricate to support modules # - Disclaimer # - Label # - Expiration Date & Time # - M of N # - Policy Value (ETH) # Policy Expiration # TODO: Remove this line when the time is right. paint_probationary_period_disclaimer(emitter) # Label if not label: label = click.prompt(f'Enter label to grant Bob {bob_verifying_key[:8]}', type=click.STRING) if not force and not expiration: if ALICE.duration_periods: # TODO: use a default in days or periods? expiration = maya.now() + timedelta(days=ALICE.duration_periods) # default if not click.confirm(f'Use default policy duration (expires {expiration})?'): expiration = click.prompt('Enter policy expiration datetime', type=click.DateTime()) else: # No policy duration default default available; Go interactive expiration = click.prompt('Enter policy expiration datetime', type=click.DateTime()) # TODO: Remove this line when the time is right. enforce_probationary_period(emitter=emitter, expiration=expiration) # Policy Threshold and Shares if not n: n = ALICE.n if not force and not click.confirm(f'Use default value for N ({n})?', default=True): n = click.prompt('Enter total number of shares (N)', type=click.INT) if not m: m = ALICE.m if not force and not click.confirm(f'Use default value for M ({m})?', default=True): m = click.prompt('Enter threshold (M)', type=click.IntRange(1, n)) # Policy Value policy_value_provided = bool(value) or bool(rate) if not ALICE.federated_only and not policy_value_provided: rate = ALICE.default_rate # TODO #1709 - Fine tuning and selection of default rates if not force: default_gwei = Web3.fromWei(rate, 'gwei') prompt = "Confirm rate of {node_rate} gwei ({total_rate} gwei per period)?" if not click.confirm(prompt.format(node_rate=default_gwei, total_rate=default_gwei*n), default=True): interactive_rate = click.prompt('Enter rate per period in gwei', type=GWEI) # TODO: Validate interactively collected rate (#1709) click.confirm(prompt.format(node_rate=rate, total_rate=rate*n), default=True, abort=True) rate = Web3.toWei(interactive_rate, 'gwei') # Request grant_request = { 'bob_encrypting_key': bob_encrypting_key, 'bob_verifying_key': bob_verifying_key, 'label': label, 'm': m, 'n': n, 'expiration': expiration, } if not ALICE.federated_only: if value: grant_request['value'] = value elif rate: grant_request['rate'] = rate # in wei if not force and not general_config.json_ipc: confirm_staged_grant(emitter=emitter, grant_request=grant_request) emitter.echo(f'Granting Access to {bob_verifying_key[:8]}', color='yellow') return ALICE.controller.grant(request=grant_request)