Esempio n. 1
0
 def load_convex(self, url, default_url=None):
     if url is None:
         url = default_url
     if url is None:
         url = DEFAULT_CONVEX_URL
     self._convex = API(url)
     return self._convex
Esempio n. 2
0
def test_convex_api_deploy_contract(convex_url, test_account):
    convex = API(convex_url)

    # create a contract object
    contract = Contract(convex)

    # set the default owner to the test_account
    owner_account = test_account

    # see if it has already been created by another test account
    owner_address = contract.resolve_owner_address(TEST_CONTRACT_NAME)
    if owner_address:
        # if so then rebuild the owner account using the same key_pair
        owner_account = Account(test_account.key_pair, owner_address)
    else:
        owner_address = test_account.address

    # deploy the contract
    contract_address = contract.deploy(owner_account,
                                       filename=TEST_CONTRACT_FILENAME,
                                       name=TEST_CONTRACT_NAME)
    assert (contract_address)
    # load the contract - should be the same address
    new_address = contract.load(TEST_CONTRACT_NAME)
    assert (contract_address == new_address)
    assert (contract_address == contract.address)
    assert (owner_address == contract.owner_address)

    contract = convex.load_contract(TEST_CONTRACT_NAME)
    assert (contract)
    assert (contract_address == contract.address)
Esempio n. 3
0
def test_account_setup_account(convex_url, test_key_pair_info):
    convex = API(convex_url)
    import_key_pair = KeyPair.import_from_bytes(test_key_pair_info['private_bytes'])
    account = convex.setup_account(TEST_ACCOUNT_NAME, import_key_pair)
    assert(account.address)
    assert(account.name)
    assert(account.name == TEST_ACCOUNT_NAME)
    assert(convex.resolve_account_name(TEST_ACCOUNT_NAME) == account.address)
Esempio n. 4
0
def test_account_api_multi_create_account(convex_url):
    convex = API(convex_url)
    key_pair = KeyPair()
    account_1 = convex.create_account(key_pair)
    assert(account_1)
    account_2 = convex.create_account(key_pair)
    assert(account_2)

    assert(account_1.public_key == account_1.public_key)
    assert(account_1.public_key == account_2.public_key)
    assert(account_1.is_address)
    assert(account_1.address != account_2.address)
Esempio n. 5
0
def test_account_name(convex_url, test_key_pair_info):
    convex = API(convex_url)
    import_key_pair = KeyPair.import_from_bytes(test_key_pair_info['private_bytes'])
    if convex.resolve_account_name(TEST_ACCOUNT_NAME):
        account = convex.load_account(TEST_ACCOUNT_NAME, import_key_pair)
    else:
        account = convex.create_account(import_key_pair)
        convex.topup_account(account)
        account = convex.register_account_name(TEST_ACCOUNT_NAME, account)
    assert(account.address)
    assert(account.name)
    assert(account.name == TEST_ACCOUNT_NAME)
    assert(convex.resolve_account_name(TEST_ACCOUNT_NAME) == account.address)
Esempio n. 6
0
def topup_accounts(convex: API, account: Union[Account, AccountList], min_balance=None):
    if isinstance(account, (list, tuple)):
        for account_item in account:
            topup_accounts(convex, account_item, min_balance)
        return
    if min_balance is None:
        min_balance = TOPUP_AMOUNT
    return convex.topup_account(account, min_balance)
 def __init__(self, url: str, contract_names=None) -> None:
     NetworkBase.__init__(self, url)
     self._convex = API(url)
     self._contract_names = contract_names or {}
     from starfish.network.convex.contract.contract_manager import ContractManager
     self._manager = ContractManager(self._convex, DEFAULT_PACKAGE_NAME)
class ConvexNetwork(NetworkBase):
    def __init__(self, url: str, contract_names=None) -> None:
        NetworkBase.__init__(self, url)
        self._convex = API(url)
        self._contract_names = contract_names or {}
        from starfish.network.convex.contract.contract_manager import ContractManager
        self._manager = ContractManager(self._convex, DEFAULT_PACKAGE_NAME)

    def create_account(self, key_pair: KeyPair = None) -> ConvexAccount:
        return self._convex.create_account(key_pair)

    def load_account(self, name, key_pair: KeyPair) -> ConvexAccount:
        return self._convex.setup_account(name, key_pair)

    def setup_account(self, name, key_pair: KeyPair) -> ConvexAccount:
        return self._convex.setup_account(name, key_pair)

    def register_account_name(self, name,
                              account: ConvexAccount) -> ConvexAccount:
        return self._convex.setup_account(name, account)

    """

    Register DID with a DDO and resolve DID to a DDO

    """

    def register_did(self, account: ConvexAccount, did: str,
                     ddo_text: str) -> bool:
        ddo_registry_contract = self._manager.load(
            'DIDContract',
            self._contract_names.get('DIDContract', DID_CONTRACT_NAME))
        if ddo_registry_contract:
            return ddo_registry_contract.register_did(did, ddo_text, account)

    def resolve_did(self,
                    did: str,
                    account_address: AccountAddress = None) -> str:
        ddo_registry_contract = self._manager.load(
            'DIDContract',
            self._contract_names.get('DIDContract', DID_CONTRACT_NAME))
        if account_address is None:
            account_address = ddo_registry_contract.address
        if ddo_registry_contract:
            return ddo_registry_contract.resolve(did, account_address)
        return None

    """

    Register Provenance

    """

    def register_provenance(self, account: ConvexAccount, asset_id: str,
                            data: str):
        provenance_contract = self._manager.load(
            'ProvenanceContract',
            self._contract_names.get('ProvenanceContract',
                                     PROVENANCE_CONTRACT_NAME))
        result = provenance_contract.register(asset_id, data, account)
        return result

    def get_provenance_event_list(self, asset_id: str):
        provenance_contract = self._manager.load(
            'ProvenanceContract',
            self._contract_names.get('ProvenanceContract',
                                     PROVENANCE_CONTRACT_NAME))
        return provenance_contract.event_list(asset_id,
                                              provenance_contract.address)

    @property
    def convex(self):
        return self._convex
Esempio n. 9
0
def test_account_api_create_account(convex_url):

    convex = API(convex_url)
    key_pair = KeyPair()
    result = convex.create_account(key_pair)
    assert(result)
Esempio n. 10
0
def convex(config):
    convex = API(config['network']['url'])
    return convex
Esempio n. 11
0
class CommandBase(ABC):
    def __init__(self, name, sub_parser=None):
        self._name = name
        self._convex = None
        self._sub_parser = sub_parser
        if sub_parser:
            self.create_parser(sub_parser)

    def is_command(self, name):
        return self._name == name

    def load_convex(self, url, default_url=None):
        if url is None:
            url = default_url
        if url is None:
            url = DEFAULT_CONVEX_URL
        self._convex = API(url)
        return self._convex

    def process_sub_command(self, args, output, command):
        is_found = False
        for command_item in self._command_list:
            if command_item.is_command(command):
                command_item.execute(args, output)
                is_found = True
                break

        if not is_found:
            self.print_help()

    def print_help(self):
        self._sub_parser.choices[self._name].print_help()

    def resolve_to_name_address(self, name_address, output):
        name = None
        address = None
        if name_address:
            address = self._convex.resolve_account_name(name_address)
            name = name_address

        if not address:
            address = name_address

        if not self.is_address(address):
            output.add_error(f'{address} is not an convex account address')
            return
        return {'name': name, 'address': address}

    def import_key_pair(self, args):
        key_pair = None
        if args.keyfile and args.password:
            logger.debug(f'importing keyfile {args.keyfile}')
            key_pair = KeyPair.import_from_file(args.keyfile, args.password)
        elif args.keywords:
            logger.debug('importing key from mnemonic')
            key_pair = KeyPair.import_from_mnemonic(args.keywords)
        elif args.keytext and args.password:
            logger.debug('importing keytext')
            key_pair = KeyPair.import_from_text(args.keytext, args.password)

        return key_pair

    def load_account(self, args, name_address, output):

        info = self.resolve_to_name_address(name_address, output)
        if not info:
            return

        key_pair = self.import_key_pair(args)
        if not key_pair:
            output.add_error(
                'you need to set the "--keywords" or "--password" and "--keyfile/--keytext" to a valid account'
            )
            return

        return Account(key_pair, info['address'], name=info['name'])

    def is_address(self, value):
        return is_address(value)

    @abstractmethod
    def create_parser(self, sub_parser):
        pass

    @abstractmethod
    def execute(self, args, output):
        pass

    @property
    def name(self) -> str:
        return self._name