def app_get_resource( blockchain_id, app_domain, res_name, app_config=None, data_pubkey=None, proxy=None, config_path=CONFIG_PATH ):
    """
    Get a named application resource from mutable storage

    data_pubkey should be the publisher's public key 

    If app_config is not None, then the driver hints will be honored.
    
    Return {'status': True, 'res': resource} on success
    Return {'error': ...} on error
    """

    proxy = get_default_proxy() if proxy is None else proxy

    urls = None
    if app_config is not None:
        # use driver hints
        driver_hints = app_config['driver_hints']
        urls = storage.get_driver_urls( res_data_id, storage.get_storage_handlers() )

    res = data.get_mutable( res_name, [app_domain], data_pubkey=data_pubkey, proxy=proxy, config_path=config_path, urls=urls, blockchain_id=blockchain_id )
    if 'error' in res:
        log.error("Failed to get resource {}: {}".format(res_name, res['error']))
        return {'error': 'Failed to load resource'}

    return {'status': True, 'res': res['data']}
def app_get_config( blockchain_id, app_domain, data_pubkey=None, proxy=None, config_path=CONFIG_PATH ):
    """
    Get application configuration bundle.
    
    data_pubkey should be the publisher's public key.

    Return {'status': True, 'config': config} on success
    Return {'error': ...} on error
    """

    proxy = get_default_proxy() if proxy is None else proxy

    # go get config
    res = data.get_mutable( ".blockstack", [app_domain], data_pubkey=data_pubkey, proxy=proxy, config_path=config_path, blockchain_id=blockchain_id )
    if 'error' in res:
        log.error("Failed to get application config file {}: {}".format(config_data_id, res['error']))
        return res

    app_cfg = None
    try:
        app_cfg = res['data']
        jsonschema.validate(app_cfg, APP_CONFIG_SCHEMA)
    except ValidationError as ve:
        if BLOCKSTACK_TEST:
            log.exception(ve)

        log.error("Invalid application config file {}".format(config_data_id))
        return {'error': 'Invalid application config'}

    return {'status': True, 'config': app_cfg}
Exemple #3
0
def app_get_config( blockchain_id, app_domain, data_pubkey=None, proxy=None, config_path=CONFIG_PATH ):
    """
    Get application configuration bundle.
    
    data_pubkey should be the publisher's public key.

    Return {'status': True, 'config': config} on success
    Return {'error': ...} on error
    """

    proxy = get_default_proxy() if proxy is None else proxy

    # go get config 
    config_data_id = '{}/.blockstack'.format(app_domain)
    res = data.get_mutable( config_data_id, data_pubkey=data_pubkey, proxy=proxy, config_path=config_path, blockchain_id=blockchain_id, fully_qualified_data_id=True )
    if 'error' in res:
        log.error("Failed to get application config file {}: {}".format(config_data_id, res['error']))
        return res

    app_cfg = None
    try:
        app_cfg = res['data']
        jsonschema.validate(app_cfg, APP_CONFIG_SCHEMA)
    except ValidationError as ve:
        if BLOCKSTACK_TEST:
            log.exception(ve)

        log.error("Invalid application config file {}".format(config_data_id))
        return {'error': 'Invalid application config'}

    return {'status': True, 'config': app_cfg}
Exemple #4
0
def app_get_resource( blockchain_id, app_domain, res_name, app_config=None, data_pubkey=None, proxy=None, config_path=CONFIG_PATH ):
    """
    Get a named application resource from mutable storage

    data_pubkey should be the publisher's public key 

    If app_config is not None, then the driver hints will be honored.
    
    Return {'status': True, 'res': resource} on success
    Return {'error': ...} on error
    """

    proxy = get_default_proxy() if proxy is None else proxy

    res_data_id = '{}/{}'.format(app_domain, res_name)

    urls = None
    if app_config is not None:
        # use driver hints
        driver_hints = app_config['driver_hints']
        urls = storage.get_driver_urls( res_data_id, storage.get_storage_handlers() )

    res = data.get_mutable( res_data_id, data_pubkey=data_pubkey, proxy=proxy, config_path=config_path, urls=urls, blockchain_id=blockchain_id, fully_qualified_data_id=True )
    if 'error' in res:
        log.error("Failed to get resource {}: {}".format(res_data_id, res['error']))
        return {'error': 'Failed to load resource'}

    return {'status': True, 'res': res['data']}
Exemple #5
0
def blockstack_url_fetch(url,
                         proxy=None,
                         config_path=CONFIG_PATH,
                         wallet_keys=None):
    """
    Given a blockstack:// url, fetch its data.
    If the data is an immutable data url, and the hash is not given, then look up the hash first.
    If the data is a mutable data url, and the version is not given, then look up the version as well.

    Data from datastores requires wallet_keys

    Return {"data": data} on success
    Return {"error": error message} on error
    """
    mutable = False
    immutable = False
    blockchain_id = None
    data_id = None
    version = None
    data_hash = None

    url_info = blockstack_data_url_parse(url)
    if url_info is None:
        return {'error': 'Failed to parse {}'.format(url)}

    data_id = url_info['data_id']
    blockchain_id = url_info['blockchain_id']
    url_type = url_info['type']
    fields = url_info['fields']

    if url_type == 'mutable':
        datastore_id = fields.get('datastore_id')
        version = fields.get('version')
        app_domain = fields.get('app_domain')
        mutable = True

    else:
        data_hash = fields.get('data_hash')
        immutable = True

    if mutable:
        if app_domain is not None:
            # get from datastore
            datastore_info = get_datastore_info(datastore_id=datastore_id,
                                                config_path=config_path,
                                                proxy=proxy)
            if 'error' in datastore_info:
                return datastore_info

            datastore = datastore_info['datastore']
            if data.datastore_get_id(datastore['pubkey']) != datastore_id:
                return {'error': 'Invalid datastore ID'}

            # file or directory?
            is_dir = data_id.endswith('/')
            if is_dir:
                return data.datastore_listdir(datastore,
                                              data_id,
                                              config_path=config_path,
                                              proxy=proxy)
            else:
                return data.datastore_getfile(datastore,
                                              data_id,
                                              config_path=config_path,
                                              proxy=proxy)

        elif blockchain_id is not None:
            # get single data
            if version is not None:
                return data.get_mutable(data_id,
                                        proxy=proxy,
                                        ver_min=version,
                                        ver_max=version + 1,
                                        blockchain_id=blockchain_id,
                                        fully_qualified_data_id=True)
            else:
                return data.get_mutable(data_id,
                                        proxy=proxy,
                                        blockchain_id=blockchain_id,
                                        fully_qualified_data_id=True)

        else:
            return {'error': 'Invalid URL'}

    else:
        if data_id is not None:
            # get single data
            if data_hash is not None:
                return data.get_immutable(blockchain_id,
                                          data_hash,
                                          data_id=data_id,
                                          proxy=proxy)

            else:
                return data.get_immutable_by_name(blockchain_id,
                                                  data_id,
                                                  proxy=proxy)

        else:
            # list data
            return data.list_immutable_data(blockchain_id,
                                            proxy=proxy,
                                            config_path=config_path)