mirror of https://github.com/nucypher/nucypher.git
incorporate changes from option-cleanup
parent
44ce4b475f
commit
08dab01f6a
|
@ -14,6 +14,20 @@ from nucypher.crypto.utils import construct_policy_id
|
|||
from nucypher.network.middleware import NotFound
|
||||
|
||||
|
||||
def attach_schema(schema):
|
||||
|
||||
def callable(func):
|
||||
func._schema = schema()
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapped(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
return callable
|
||||
|
||||
|
||||
|
||||
class CharacterPublicInterface:
|
||||
|
||||
specification = NotImplemented
|
||||
|
@ -23,14 +37,14 @@ class CharacterPublicInterface:
|
|||
super().__init__(*args, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def command(cls, action):
|
||||
def connect(cls, action):
|
||||
|
||||
schema = cls.specifications[action]
|
||||
schema = getattr(cls, action)._schema
|
||||
|
||||
def callable(func):
|
||||
c = func
|
||||
for k, f in schema.load_fields.items():
|
||||
c = click.option(*f.click.args, **f.click.kwargs)(c)
|
||||
c = f.click(c)
|
||||
|
||||
@functools.wraps(func)
|
||||
def wrapped(*args, **kwargs):
|
||||
|
@ -49,6 +63,7 @@ class AliceInterface(CharacterPublicInterface):
|
|||
'public_keys': alice.PublicKeys(),
|
||||
'decrypt': alice.Decrypt()}
|
||||
|
||||
@attach_schema(alice.CreatePolicy)
|
||||
def create_policy(self,
|
||||
bob_encrypting_key: bytes,
|
||||
bob_verifying_key: bytes,
|
||||
|
@ -74,11 +89,13 @@ class AliceInterface(CharacterPublicInterface):
|
|||
response_data = {'label': new_policy.label, 'policy_encrypting_key': new_policy.public_key}
|
||||
return response_data
|
||||
|
||||
@attach_schema(alice.DerivePolicyEncryptionKey)
|
||||
def derive_policy_encrypting_key(self, label: bytes) -> dict:
|
||||
policy_encrypting_key = self.character.get_policy_encrypting_key_from_label(label)
|
||||
response_data = {'policy_encrypting_key': policy_encrypting_key, 'label': label}
|
||||
return response_data
|
||||
|
||||
@attach_schema(alice.GrantPolicy)
|
||||
def grant(self,
|
||||
bob_encrypting_key: bytes,
|
||||
bob_verifying_key: bytes,
|
||||
|
@ -108,6 +125,7 @@ class AliceInterface(CharacterPublicInterface):
|
|||
'alice_verifying_key': new_policy.alice.stamp}
|
||||
return response_data
|
||||
|
||||
@attach_schema(alice.Revoke)
|
||||
def revoke(self, label: bytes, bob_verifying_key: bytes) -> dict:
|
||||
|
||||
# TODO: Move deeper into characters
|
||||
|
@ -126,6 +144,7 @@ class AliceInterface(CharacterPublicInterface):
|
|||
response_data = {'failed_revocations': len(failed_revocations)}
|
||||
return response_data
|
||||
|
||||
@attach_schema(alice.Decrypt)
|
||||
def decrypt(self, label: bytes, message_kit: bytes) -> dict:
|
||||
"""
|
||||
Character control endpoint to allow Alice to decrypt her own data.
|
||||
|
|
|
@ -1,36 +1,37 @@
|
|||
from nucypher.characters.control.specifications import fields
|
||||
from nucypher.characters.control.specifications.base import BaseSchema
|
||||
import click
|
||||
from nucypher.cli import common_options
|
||||
from nucypher.cli.common_options import option_m
|
||||
|
||||
|
||||
class PolicyBaseSchema(BaseSchema):
|
||||
|
||||
bob_encrypting_key = fields.Key(
|
||||
required=True, load_only=True,
|
||||
click=fields.click(
|
||||
click=click.option(
|
||||
'--bob-encrypting-key',
|
||||
help="Bob's encrypting key as a hexadecimal string"))
|
||||
bob_verifying_key = fields.Key(
|
||||
required=True, load_only=True,
|
||||
click=fields.click(
|
||||
'--bob-verifying-key',
|
||||
help="Bob's verifying key as a hexadecimal string"))
|
||||
click=click.option(
|
||||
'--bob-verifying-key', help="Bob's verifying key as a hexadecimal string"))
|
||||
m = fields.M(
|
||||
required=True, load_only=True,
|
||||
click=fields.click(
|
||||
'--m', help="M-Threshold KFrags"))
|
||||
click=common_options.option_m)
|
||||
n = fields.N(
|
||||
required=True, load_only=True,
|
||||
click=fields.click(
|
||||
'--n', help="N-Total KFrags"))
|
||||
click=common_options.option_n)
|
||||
expiration = fields.DateTime(
|
||||
required=True, load_only=True,
|
||||
click=fields.click(
|
||||
'--expiration', help="Expiration Datetime of a policy"))
|
||||
click=click.option(
|
||||
'--expiration',
|
||||
help="Expiration Datetime of a policy"))
|
||||
|
||||
# optional input
|
||||
value = fields.Wei(
|
||||
load_only=True,
|
||||
click=fields.click('--value', help="Total policy value (in Wei)"))
|
||||
click=click.option('--value', help="Total policy value (in Wei)"))
|
||||
|
||||
# output
|
||||
policy_encrypting_key = fields.Key(dump_only=True)
|
||||
|
@ -40,16 +41,14 @@ class CreatePolicy(PolicyBaseSchema):
|
|||
|
||||
label = fields.Label(
|
||||
required=True,
|
||||
click=fields.click(
|
||||
'--label', help="The label for a policy"))
|
||||
click=common_options.option_label)
|
||||
|
||||
|
||||
class GrantPolicy(PolicyBaseSchema):
|
||||
|
||||
label = fields.Label(
|
||||
load_only=True, required=True,
|
||||
click=fields.click(
|
||||
'--label', help="The label for a policy"))
|
||||
click=common_options.option_label)
|
||||
|
||||
# output fields
|
||||
treasure_map = fields.TreasureMap(dump_only=True)
|
||||
|
@ -58,20 +57,29 @@ class GrantPolicy(PolicyBaseSchema):
|
|||
|
||||
class DerivePolicyEncryptionKey(BaseSchema):
|
||||
|
||||
label = fields.Label(required=True)
|
||||
label = fields.Label(
|
||||
required=True,
|
||||
click=common_options.option_label)
|
||||
policy_encrypting_key = fields.Key(dump_only=True)
|
||||
|
||||
|
||||
class Revoke(BaseSchema):
|
||||
|
||||
label = fields.Label(required=True, load_only=True)
|
||||
bob_verifying_key = fields.Key(required=True, load_only=True)
|
||||
label = fields.Label(
|
||||
required=True, load_only=True,
|
||||
click=common_options.option_label)
|
||||
bob_verifying_key = fields.Key(
|
||||
required=True, load_only=True,
|
||||
click=click.option(
|
||||
'--bob-verifying-key', help="Bob's verifying key as a hexadecimal string"))
|
||||
|
||||
failed_revocations = fields.Integer(dump_only=True)
|
||||
|
||||
|
||||
class Decrypt(BaseSchema):
|
||||
label = fields.Label(required=True, load_only=True)
|
||||
label = fields.Label(
|
||||
required=True, load_only=True,
|
||||
click=common_options.option_label)
|
||||
message_kit = fields.UmbralMessageKit(load_only=True)
|
||||
cleartexts = fields.List(fields.Cleartext(), dump_only=True)
|
||||
|
||||
|
@ -79,5 +87,3 @@ class Decrypt(BaseSchema):
|
|||
class PublicKeys(BaseSchema):
|
||||
|
||||
alice_verifying_key = fields.Key(dump_only=True)
|
||||
|
||||
|
||||
|
|
|
@ -4,15 +4,6 @@ from marshmallow import Schema, INCLUDE, EXCLUDE
|
|||
from nucypher.characters.control.specifications.exceptions import InvalidInputField
|
||||
|
||||
|
||||
def nucypher_command(func, *args, **kwargs):
|
||||
|
||||
@wraps(func)
|
||||
def wrapped():
|
||||
result = func(*args, **kwargs)
|
||||
return result
|
||||
return wrapped
|
||||
|
||||
|
||||
class BaseSchema(Schema):
|
||||
|
||||
class Meta:
|
||||
|
@ -21,10 +12,3 @@ class BaseSchema(Schema):
|
|||
|
||||
def handle_error(self, error, data, many, **kwargs):
|
||||
raise InvalidInputField(error)
|
||||
|
||||
def specify_commands(self, *args, **kwargs):
|
||||
print (self)
|
||||
print (args)
|
||||
print (kwargs)
|
||||
for k, f in self.load_fields.items():
|
||||
print (k, f.click.args, f.click.kwargs)
|
||||
|
|
|
@ -8,6 +8,6 @@ class BaseField:
|
|||
def __init__(self, *args, **kwargs):
|
||||
self.click = kwargs.pop('click', None)
|
||||
if self.click:
|
||||
self.click.kwargs['required'] = kwargs.get('required', False)
|
||||
self.click.kwargs['type'] = self.click_type
|
||||
self.click.required = kwargs.get('required', False)
|
||||
self.click.type = click.types.convert_type(self.click_type)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
|
|
@ -22,6 +22,7 @@ import click
|
|||
from constant_sorrow.constants import NO_BLOCKCHAIN_CONNECTION, NO_PASSWORD
|
||||
|
||||
from nucypher.characters.banners import ALICE_BANNER
|
||||
from nucypher.characters.control.interfaces import AliceInterface
|
||||
from nucypher.cli import actions, painting, types
|
||||
from nucypher.cli.actions import get_nucypher_password, select_client_account, get_client_password
|
||||
from nucypher.cli.config import group_general_config
|
||||
|
@ -359,15 +360,7 @@ def derive_policy_pubkey(general_config, label, character_options, config_file):
|
|||
|
||||
|
||||
@alice.command()
|
||||
@click.option('--bob-encrypting-key', help="Bob's encrypting key as a hexadecimal string", type=click.STRING,
|
||||
required=True)
|
||||
@option_bob_verifying_key
|
||||
@option_label(required=True)
|
||||
@option_m
|
||||
@option_n
|
||||
@option_rate
|
||||
@click.option('--expiration', help="Expiration Datetime of a policy", type=click.STRING) # TODO: click.DateTime()
|
||||
@click.option('--value', help="Total policy value (in Wei)", type=types.WEI)
|
||||
@AliceInterface.connect('grant')
|
||||
@group_character_options
|
||||
@option_config_file
|
||||
@group_general_config
|
||||
|
@ -412,8 +405,7 @@ def grant(general_config,
|
|||
|
||||
|
||||
@alice.command()
|
||||
@option_bob_verifying_key
|
||||
@option_label(required=True)
|
||||
@AliceInterface.connect('revoke')
|
||||
@group_character_options
|
||||
@option_config_file
|
||||
@group_general_config
|
||||
|
|
|
@ -26,8 +26,6 @@ from nucypher.cli.types import (
|
|||
EXISTING_READABLE_FILE,
|
||||
NETWORK_PORT,
|
||||
)
|
||||
from nucypher.network.middleware import RestMiddleware
|
||||
from nucypher.utilities.sandbox.middleware import MockRestMiddleware
|
||||
|
||||
|
||||
# Alphabetical
|
||||
|
@ -170,6 +168,9 @@ def wrap_option(handler, **options):
|
|||
|
||||
|
||||
def process_middleware(mock_networking):
|
||||
print ('process_middleware')
|
||||
from nucypher.network.middleware import RestMiddleware
|
||||
from nucypher.utilities.sandbox.middleware import MockRestMiddleware
|
||||
if mock_networking:
|
||||
middleware = MockRestMiddleware()
|
||||
else:
|
||||
|
@ -178,4 +179,7 @@ def process_middleware(mock_networking):
|
|||
return 'middleware', middleware
|
||||
|
||||
|
||||
option_middleware = wrap_option(process_middleware, mock_networking=_option_middleware)
|
||||
option_middleware = wrap_option(
|
||||
process_middleware,
|
||||
mock_networking=click.option('-Z', '--mock-networking', help="Use in-memory transport instead of networking", count=True),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue