예제 #1
0
def create_invoice(
    *,
    wallet_id: str,
    amount: int,
    memo: str,
    description_hash: Optional[bytes] = None,
    extra: Optional[Dict] = None,
) -> Tuple[str, str]:
    invoice_memo = None if description_hash else memo
    storeable_memo = memo

    ok, checking_id, payment_request, error_message = WALLET.create_invoice(
        amount=amount, memo=invoice_memo, description_hash=description_hash)
    if not ok:
        raise Exception(error_message or "Unexpected backend error.")

    invoice = bolt11.decode(payment_request)

    amount_msat = amount * 1000
    create_payment(
        wallet_id=wallet_id,
        checking_id=checking_id,
        payment_request=payment_request,
        payment_hash=invoice.payment_hash,
        amount=amount_msat,
        memo=storeable_memo,
        extra=extra,
    )

    g.db.commit()
    return invoice.payment_hash, payment_request
예제 #2
0
def lnurlwallet():
    memo = "LNbits LNURL funding"

    try:
        withdraw_res = handle_lnurl(request.args.get("lightning"))
        if not withdraw_res.ok:
            abort(
                HTTPStatus.BAD_REQUEST,
                f"Could not process LNURL-withdraw: {withdraw_res.error_msg}")
        if not isinstance(withdraw_res, LnurlWithdrawResponse):
            abort(HTTPStatus.BAD_REQUEST, "Not a valid LNURL-withdraw.")
    except LnurlException:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process LNURL-withdraw.")

    try:
        ok, checking_id, payment_request, error_message = WALLET.create_invoice(
            withdraw_res.max_sats, memo)
    except Exception as e:
        ok, error_message = False, str(e)

    if not ok:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR, error_message)

    r = requests.get(
        withdraw_res.callback.base,
        params={
            **withdraw_res.callback.query_params,
            **{
                "k1": withdraw_res.k1,
                "pr": payment_request
            }
        },
    )

    if not r.ok:
        abort(HTTPStatus.INTERNAL_SERVER_ERROR,
              "Could not process LNURL-withdraw.")

    for i in range(10):
        invoice_status = WALLET.get_invoice_status(checking_id)
        sleep(i)
        if not invoice_status.paid:
            continue
        break

    user = get_user(create_account().id)
    wallet = create_wallet(user_id=user.id)
    create_payment(
        wallet_id=wallet.id,
        checking_id=checking_id,
        amount=withdraw_res.max_sats * 1000,
        memo=memo,
        pending=invoice_status.pending,
    )

    return redirect(url_for("core.wallet", usr=user.id, wal=wallet.id))
예제 #3
0
파일: services.py 프로젝트: sgeisler/lnbits
def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]:
    try:
        ok, checking_id, payment_request, error_message = WALLET.create_invoice(amount=amount, memo=memo)
    except Exception as e:
        ok, error_message = False, str(e)

    if not ok:
        raise Exception(error_message or "Unexpected backend error.")

    amount_msat = amount * 1000
    create_payment(wallet_id=wallet_id, checking_id=checking_id, amount=amount_msat, memo=memo)

    return checking_id, payment_request