Beispiel #1
0
def build_claims(claims, **kwargs):
    extra = {
        'url': kwargs.get('url'),
        'action': kwargs.get('action'),
        'appInfo': {
            'description': kwargs.get('app_description', "forge-python-app"),
            'icon': kwargs.get('app_icon'),
            'name': kwargs.get('app_name'),
            'subtitle': kwargs.get('app_subtitle'),
        },
        'chainInfo': {
            'chainHost': kwargs.get('chain_host'),
            'chainId': kwargs.get('chain_id'),
            'chain_token': kwargs.get('token_symbol'),
            'chainVersion': kwargs.get('chain_version'),
            'decimals': kwargs.get('decimals'),
        },
        'requestedClaims': claims,
        'workflow': {
            'description': kwargs.get('workflow')
        }
    }
    app_did_type = AbtDid.parse_type_from_did(kwargs.get('app_addr'))
    res = {
        'appPk': utils.multibase_b58encode(kwargs.get('app_pk')),
        'authInfo': app_did_type.gen_and_sign(kwargs.get('app_sk'), extra)
    }

    return json.dumps(res)
Beispiel #2
0
def send_back_signature(middle_req, did, signature=None):
    origin = get_origin(middle_req)
    signature=forge_utils.multibase_b58encode(signature) if signature else None

    requested_claim = {'did': did,
                       'origin': origin,
                       'sig': signature,
                       }

    user_info = {
        'requestedClaims': [requested_claim],
        'iss': 'did:abt:' + user.address,
    }
    data = {
        'userPk': forge_utils.multibase_b58encode(user.pk),
        'userInfo': 'placeholder.' + forge_utils.multibase_b64encode(
            json.dumps(user_info)) + '.placeholder'
    }
    return data
Beispiel #3
0
def require_sig(**kwargs):
    user_did_type = AbtDid.parse_type_from_did(kwargs.get('user_did'))

    claim = {
        'type':
        "signature",
        'data':
        utils.multibase_b58encode(
            user_did_type.hasher.hash(kwargs.get('tx').SerializeToString())),
        'meta': {
            'description': kwargs.get('description')
        },
        'method':
        user_did_type.hash_type,
        'origin':
        utils.multibase_b58encode(kwargs.get('tx').SerializeToString()),
    }

    return build_claims([claim], **kwargs)
Beispiel #4
0
def get_token(endpoint):
    token = secrets.token_hex(8)
    utils.mark_token_status(token, 'created')

    url = forge_utils.did_url(
        url=utils.server_url(f'/api/did/{endpoint}/auth?_t_={token}'),
        action='requestAuth',
        app_pk=forge_utils.multibase_b58encode(env.APP_PK),
        app_addr=env.APP_ADDR)

    return jsonify(token=token, url=url)
Beispiel #5
0
def get_token(endpoint, event_address):
    token = secrets.token_hex(8)
    utils.mark_token_status(token, 'created')

    default = utils.server_url(f'/api/did/{endpoint}/auth?_t_={token}')

    url = forge_utils.did_url(
        url=default if not event_address else urllib.parse.quote(
            utils.server_url(f'/api/did/{endpoint}/auth'
                             f'?_t_={token}&event_address={event_address}')),
        action='requestAuth',
        app_pk=forge_utils.multibase_b58encode(env.APP_PK),
        app_addr=env.APP_ADDR)

    return jsonify(token=token, url=url)
Beispiel #6
0
    def __init__(self, ticket_state, event_state):
        self.title = event_state.title
        self.start_time = event_state.start_time
        self.end_time = event_state.end_time
        self.location = event_state.location
        self.img_url = event_state.img_url
        self.consume_tx = forge_utils.multibase_b58encode(
                event_state.consume_tx.SerializeToString()) if \
            event_state.consume_tx else None

        self.address = ticket_state.address
        self.issuer = ticket_state.issuer

        self.price = forge_utils.from_unit(
            forge_utils.biguint_to_int(event_state.price))
Beispiel #7
0
def consume():
    wallet_res = forge_did.WalletResponse(request.get_json())
    tx = controllers.build_consume_ticket_tx(
        wallet_res.get_origin_tx(),
        wallet_res.get_signature(),
    )
    res = forge.rpc.send_tx(tx)

    base58_tx = forge_utils.multibase_b58encode(tx.SerializeToString())
    if res.hash:
        return jsonify(hash=res.hash,
                       tx=base58_tx,
                       ticket=request.args.get('ticket_address'))
    else:
        return jsonify(error='whoops! Can not consume required ticket')
Beispiel #8
0
def post_handler(**args):
    wallet_res = args.get('wallet_res')
    token = args.get('token')
    tx = wallet_res.get_origin_tx()
    tx.signature = wallet_res.get_signature()

    res = forge.rpc.send_tx(tx)
    if res.hash:
        utils.mark_token_status(token, 'succeed')
        return {'status': 0,
                'hash': res.hash,
                'tx': forge_utils.multibase_b58encode(
                        tx.SerializeToString())}
    else:
        utils.mark_token_status(token, 'error')
        return {'error': f"Oops, error code: {res.code}"}
Beispiel #9
0
    def hash_to_did(self, hash):
        if not isinstance(hash, bytes):
            hash = hash.encode()
        try:
            hash = base64.b16decode(hash, True)
        except Exception:
            hash = hash
        type_bytes = self._type_to_bytes()

        extended_hash = type_bytes + hash[0:20]

        did_bytes = extended_hash + self.hasher.hash(extended_hash)[0:4]
        encoded_did = utils.multibase_b58encode(did_bytes)
        if not self.encode:
            return did_bytes
        elif self.form == 'long':
            return AbtDid.PREFIX + encoded_did
        else:
            return encoded_did
Beispiel #10
0
def post_handler(**args):
    wallet_res = args.get('wallet_res')
    token = args.get('token')

    tx = wallet_res.get_origin_tx()
    logging.debug(f'nonce: {tx.nonce}')
    tx.signature = wallet_res.get_signature()

    res = forge.rpc.send_tx(tx)
    if res.hash:
        utils.mark_token_status(token, 'succeed')
        logging.debug(f'hash: {res.hash}')
        return {'status': 0,
                'hash': res.hash,
                'tx': forge_utils.multibase_b58encode(
                        tx.SerializeToString())}
    else:
        utils.mark_token_status(token, 'error')
        logging.error(res)
        return {'error': f"Oops, something is wrong :("}
Beispiel #11
0
def post_handler(**args):
    wallet_res = args.get('wallet_res')
    token = args.get('token')
    tx = wallet_res.get_origin_tx()
    tx.signature = wallet_res.get_signature()
    parsed_tx = forge_utils.parse_to_proto(tx.itx.value,
                                           forge_protos.AcquireAssetTx)
    ticket_address = parsed_tx.specs[0].address

    res = forge.rpc.send_tx(tx)
    if res.hash:
        utils.mark_token_status(token, 'succeed')
        logger.debug(f"Successfully bought ticket. Hash: " + f"{res.hash}")
        return {
            'status': 0,
            'ticket': ticket_address,
            'hash': res.hash,
            'tx': forge_utils.multibase_b58encode(tx.SerializeToString())
        }
    else:
        utils.mark_token_status(token, 'error')
        logger.debug(f"Fail to buy ticket with error: " + f"{res}")
        return {'error': f"Oops, error code: {res.code}"}
Beispiel #12
0
import json
from time import sleep
import logging

forge= ForgeConn('127.0.0.1:27210')
forge_rpc = forge.rpc


user = forge_rpc.create_wallet(moniker='tester', passphrase='abcd1234').wallet

event = 'zjdkjCT3sS7LfpqhzTTSxKtAZfAogbeSWKAB'

buy_url = f'http://10.1.10.176:5000/api/did/buy-ticket/auth/event_address={event}&_t_=123'
require_asset_url = f'http://10.1.10.176:5000/api/mobile/require-asset/{event}'
get_request_params = {
        'userPk': forge_utils.multibase_b58encode(user.pk),
        'userDid': 'did:abt:' + user.address,
    }
sleep(5)


def buy_ticket_get():
    r = requests.get(url=buy_url, params=get_request_params)
    return r.json()


def buy_ticket_post(middle_user_info):
    origin = get_origin(middle_user_info)
    tx = forge_utils.parse_to_proto(forge_utils.multibase_b58decode(origin),
                                    forge_protos.Transaction)
Beispiel #13
0
def base58_encode_tx(tx):
    b = tx.SerializeToString()
    return forge_utils.multibase_b58encode(b)