async def create_account(request):
    """Creates a new Account and corresponding authorization token"""
    required_fields = ['email', 'password']
    common.validate_fields(required_fields, request.json)

    private_key = request.app.config.CONTEXT.new_random_private_key()
    signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key)
    public_key = signer.get_public_key().as_hex()

    auth_entry = _create_auth_dict(request, public_key, private_key.as_hex())
    await auth_query.create_auth_entry(request.app.config.DB_CONN, auth_entry)

    account = _create_account_dict(request.json, public_key)

    batches, batch_id = transaction_creation.create_account(
        txn_key=signer,
        batch_key=request.app.config.SIGNER,
        label=account.get('label'),
        description=account.get('description'))

    await messaging.send(request.app.config.VAL_CONN,
                         request.app.config.TIMEOUT, batches)

    try:
        await messaging.check_batch_status(request.app.config.VAL_CONN,
                                           batch_id)
    except (ApiBadRequest, ApiInternalError) as err:
        await auth_query.remove_auth_entry(request.app.config.DB_CONN,
                                           request.json.get('email'))
        raise err

    token = common.generate_auth_token(request.app.config.SECRET_KEY,
                                       account.get('email'), public_key)

    return response.json({'authorization': token, 'account': account})
async def update_account_info(request):
    """Updates auth information for the authorized account"""
    token = common.deserialize_auth_token(request.app.config.SECRET_KEY,
                                          request.token)

    update = {}
    if request.json.get('password'):
        update['hashed_password'] = bcrypt.hashpw(
            bytes(request.json.get('password'), 'utf-8'), bcrypt.gensalt())
    if request.json.get('email'):
        update['email'] = request.json.get('email')

    if update:
        updated_auth_info = await auth_query.update_auth_info(
            request.app.config.DB_CONN, token.get('email'),
            token.get('public_key'), update)
        new_token = common.generate_auth_token(
            request.app.config.SECRET_KEY, updated_auth_info.get('email'),
            updated_auth_info.get('publicKey'))
    else:
        updated_auth_info = await accounts_query.fetch_account_resource(
            request.app.config.DB_CONN, token.get('public_key'),
            token.get('public_key'))
        new_token = request.token

    return response.json({
        'authorization': new_token,
        'account': updated_auth_info
    })
Esempio n. 3
0
async def update_account_info(request):
    """Updates auth information for the authorized account"""
    token = common.deserialize_auth_token(
        request.app.config.SECRET_KEY, request.token)

    update = {}
    if request.json.get('password'):
        update['hashed_password'] = bcrypt.hashpw(
            bytes(request.json.get('password'), 'utf-8'), bcrypt.gensalt())
    if request.json.get('email'):
        update['email'] = request.json.get('email')

    if update:
        updated_auth_info = await auth_query.update_auth_info(
            request.app.config.DB_CONN,
            token.get('email'),
            token.get('public_key'),
            update)
        new_token = common.generate_auth_token(
            request.app.config.SECRET_KEY,
            updated_auth_info.get('email'),
            updated_auth_info.get('publicKey'))
    else:
        updated_auth_info = await accounts_query.fetch_account_resource(
            request.app.config.DB_CONN,
            token.get('public_key'),
            token.get('public_key'))
        new_token = request.token

    return response.json(
        {
            'authorization': new_token,
            'account': updated_auth_info
        })
Esempio n. 4
0
async def generate_asset_address(request):

    required_fields = ["user_id", "password"]

    common.validate_fields(required_fields, request.json)

    mnemonic, address, account = await accounts_utils.get_account_details(
        request)
    # print(type(account))
    childkey_index = common.gen_childkey_index()

    if "keys" in account.keys():
        while childkey_index in account["keys"]:
            childkey_index = common.gen_childkey_index()

    else:
        childkey_index = common.gen_childkey_index()

    child_private_key, child_public_key = await remote_api.get_child_key(
        mnemonic, childkey_index)

    private_hex_key, public_hex_key = await remote_api.get_child_key(
        mnemonic, request.app.config.ROOT_KEY_INDEX)

    private_key = Secp256k1PrivateKey.from_hex(private_hex_key)
    # private_key = request.app.config.CONTEXT.new_random_private_key()
    signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key)
    public_key = signer.get_public_key().as_hex()

    batches, batch_id, batch_list_bytes = transaction_creation.create_empty_asset(
        txn_key=signer,
        batch_key=request.app.config.SIGNER,
        key_index=childkey_index,
        child_public_key=child_public_key,
        is_empty_asset=True)

    # print(batch_list_bytes)

    data = await messaging.send(batch_list_bytes, request)

    try:
        await messaging.wait_for_status(batch_id, 300, request)
    except (ApiBadRequest, ApiInternalError) as err:
        raise err

    token = common.generate_auth_token(request.app.config.SECRET_KEY,
                                       child_private_key, public_key)
    # print(token)
    return response.json({'authorization': token, 'key_index': childkey_index})
async def authorize(request):
    """Requests an authorization token for a registered Account"""
    required_fields = ['email', 'password']
    common.validate_fields(required_fields, request.json)
    password = bytes(request.json.get('password'), 'utf-8')
    auth_info = await auth_query.fetch_info_by_email(
        request.app.config.DB_CONN, request.json.get('email'))
    if auth_info is None:
        raise ApiUnauthorized("No user with that email exists")
    hashed_password = auth_info.get('hashed_password')
    if not bcrypt.checkpw(password, hashed_password):
        raise ApiUnauthorized("Incorrect email or password")
    token = common.generate_auth_token(request.app.config.SECRET_KEY,
                                       auth_info.get('email'),
                                       auth_info.get('public_key'))
    return json({'authorization': token})
Esempio n. 6
0
async def create_account(request):
    """Creates a new Account and corresponding authorization token"""
    required_fields = ['email', 'password']
    common.validate_fields(required_fields, request.json)

    private_key = request.app.config.CONTEXT.new_random_private_key()
    signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key)
    public_key = signer.get_public_key().as_hex()

    auth_entry = _create_auth_dict(
        request, public_key, private_key.as_hex())
    await auth_query.create_auth_entry(request.app.config.DB_CONN, auth_entry)

    account = _create_account_dict(request.json, public_key)

    batches, batch_id = transaction_creation.create_account(
        txn_key=signer,
        batch_key=request.app.config.SIGNER,
        label=account.get('label'),
        description=account.get('description'))

    await messaging.send(
        request.app.config.VAL_CONN,
        request.app.config.TIMEOUT,
        batches)

    try:
        await messaging.check_batch_status(
            request.app.config.VAL_CONN, batch_id)
    except (ApiBadRequest, ApiInternalError) as err:
        await auth_query.remove_auth_entry(
            request.app.config.DB_CONN, request.json.get('email'))
        raise err

    token = common.generate_auth_token(
        request.app.config.SECRET_KEY,
        account.get('email'),
        public_key)

    return response.json(
        {
            'authorization': token,
            'account': account
        })
async def authorize(request):
    """Requests an authorization token for a registered Account"""
    required_fields = ['email', 'password']
    common.validate_fields(required_fields, request.json)
    password = bytes(request.json.get('password'), 'utf-8')
    auth_info = await auth_query.fetch_info_by_email(
        request.app.config.DB_CONN, request.json.get('email'))
    if auth_info is None:
        raise ApiUnauthorized("No user with that email exists")
    hashed_password = auth_info.get('hashed_password')
    if not bcrypt.checkpw(password, hashed_password):
        raise ApiUnauthorized("Incorrect email or password")
    token = common.generate_auth_token(
        request.app.config.SECRET_KEY,
        auth_info.get('email'),
        auth_info.get('public_key'))
    return json(
        {
            'authorization': token
        })
Esempio n. 8
0
async def create_account(request):

    # print(request.app.config)
    # print(request.json)
    required_fields = [
        'email', 'phone_number', "adhaar", "pancard", "first_name",
        "last_name", "user_type"
    ]
    # print(request.json)

    common.validate_fields(required_fields, request.json)

    user_id, password, secrets = await remote_api.registration(request)
    # print(secrets)
    print(user_id)
    print(password)
    mnemonic = encryption.recover_mnemonic(password, secrets)

    # print(mnemonic)

    private_hex_key, public_hex_key = await remote_api.get_child_key(
        mnemonic, request.app.config.ROOT_KEY_INDEX)

    private_key = Secp256k1PrivateKey.from_hex(private_hex_key)
    # private_key = request.app.config.CONTEXT.new_random_private_key()
    signer = CryptoFactory(request.app.config.CONTEXT).new_signer(private_key)
    public_key = signer.get_public_key().as_hex()
    # print(public_key)

    # return json({"main":1})
    auth_entry = _create_auth_dict(request, public_key, private_key.as_hex(),
                                   secrets, user_id)
    # print(auth_entry)
    # print(request.app.config.DB_CONN)
    database_entry = await auth_query.create_auth_entry(
        request.app.config.DB_CONN, auth_entry)
    # details = await auth_query.fetch_info_by_email(request.app.config.DB_CONN,user_id)
    # print(request.app.config.DB_CONN)
    # print(details)

    # type(email_details)
    account = _create_account_dict(request.json, public_key, user_id)

    # print(account)
    batches, batch_id, batch_list_bytes = transaction_creation.create_account(
        txn_key=signer,
        batch_key=request.app.config.SIGNER,
        email=account['email'],
        phone_number=account["phone_number"],
        adhaar=account["adhaar"],
        pancard=account["pancard"],
        first_name=account["first_name"],
        last_name=account["last_name"],
        user_type=account["user_type"],
        user_id=account["user_id"],
        keys=[])
    # print(batch_list_bytes)
    # print(batches)
    # print(type(batch_list_bytes))
    # print(type(batch_id))
    # print(batches)
    # await messaging.send(
    #     request.app.config.VAL_CONN,
    #     request.app.config.TIMEOUT,
    #     batches)

    data = await messaging.send(batch_list_bytes, request)
    # print(data)
    # try:
    #     await messaging.check_batch_status(
    #         request.app.config.VAL_CONN, batch_id)
    # except (ApiBadRequest, ApiInternalError) as err:
    #     await auth_query.remove_auth_entry(
    #         request.app.config.DB_CONN, request.json.get('email'))
    #     raise err
    try:
        await messaging.wait_for_status(batch_id, 300, request)
    except (ApiBadRequest, ApiInternalError) as err:
        raise err

    token = common.generate_auth_token(request.app.config.SECRET_KEY,
                                       account.get('email'), public_key)
    # print(token)
    return response.json({'authorization': token, 'account': account})