async def create_satsdice_withdraw(*, payment_hash: str, satsdice_pay: str, value: int, used: int) -> satsdiceWithdraw: await db.execute( """ INSERT INTO satsdice.satsdice_withdraw ( id, satsdice_pay, value, unique_hash, k1, open_time, used ) VALUES (?, ?, ?, ?, ?, ?, ?) """, ( payment_hash, satsdice_pay, value, urlsafe_short_hash(), urlsafe_short_hash(), int(datetime.now().timestamp()), used, ), ) withdraw = await get_satsdice_withdraw(payment_hash, 0) assert withdraw, "Newly created withdraw couldn't be retrieved" return withdraw
def create_paywall(*, wallet_id: str, url: str, memo: str, amount: int) -> Paywall: with open_ext_db("paywall") as db: paywall_id = urlsafe_short_hash() db.execute( """ INSERT INTO paywalls (id, wallet, secret, url, memo, amount) VALUES (?, ?, ?, ?, ?, ?) """, (paywall_id, wallet_id, urlsafe_short_hash(), url, memo, amount), ) return get_paywall(paywall_id)
def create_withdraw_link( *, wallet_id: str, title: str, min_withdrawable: int, max_withdrawable: int, uses: int, wait_time: int, is_unique: bool, usescsv: str, ) -> WithdrawLink: with open_ext_db("withdraw") as db: link_id = urlsafe_short_hash() db.execute( """ INSERT INTO withdraw_link ( id, wallet, title, min_withdrawable, max_withdrawable, uses, wait_time, is_unique, unique_hash, k1, open_time, usescsv ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( link_id, wallet_id, title, min_withdrawable, max_withdrawable, uses, wait_time, int(is_unique), urlsafe_short_hash(), urlsafe_short_hash(), int(datetime.now().timestamp()) + wait_time, usescsv, ), ) return get_withdraw_link(link_id, 0)
async def create_withdraw_link( *, wallet_id: str, title: str, min_withdrawable: int, max_withdrawable: int, uses: int, wait_time: int, is_unique: bool, usescsv: str, ) -> WithdrawLink: link_id = urlsafe_short_hash() await db.execute( """ INSERT INTO withdraw_link ( id, wallet, title, min_withdrawable, max_withdrawable, uses, wait_time, is_unique, unique_hash, k1, open_time, usescsv ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( link_id, wallet_id, title, min_withdrawable, max_withdrawable, uses, wait_time, int(is_unique), urlsafe_short_hash(), urlsafe_short_hash(), int(datetime.now().timestamp()) + wait_time, usescsv, ), ) link = await get_withdraw_link(link_id, 0) assert link, "Newly created link couldn't be retrieved" return link
def create_event( *, wallet: str, name: str, info: str, closing_date: str, event_start_date: str, event_end_date: str, amount_tickets: int, price_per_ticket: int, ) -> Events: with open_ext_db("events") as db: event_id = urlsafe_short_hash() db.execute( """ INSERT INTO events (id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, sold) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( event_id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, 0, ), ) print(event_id) return get_event(event_id)
async def create_event( *, wallet: str, name: str, info: str, closing_date: str, event_start_date: str, event_end_date: str, amount_tickets: int, price_per_ticket: int, ) -> Events: event_id = urlsafe_short_hash() await db.execute( """ INSERT INTO events.events (id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, sold) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( event_id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, 0, ), ) event = await get_event(event_id) assert event, "Newly created event couldn't be retrieved" return event
async def create_domain( *, wallet: str, domain: str, cf_token: str, cf_zone_id: str, webhook: Optional[str] = None, description: str, cost: int, allowed_record_types: str, ) -> Domains: domain_id = urlsafe_short_hash() await db.execute( """ INSERT INTO domain (id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, amountmade, allowed_record_types) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( domain_id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, 0, allowed_record_types, ), ) domain = await get_domain(domain_id) assert domain, "Newly created domain couldn't be retrieved" return domain
def api_lnurl_response(unique_hash): link = get_withdraw_link_by_hash(unique_hash) if not link: return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), Status.OK link = update_withdraw_link(link.id, k1=urlsafe_short_hash()) return jsonify(link.lnurl_response.dict()), Status.OK
def api_lnurl_callback(unique_hash): link = get_withdraw_link_by_hash(unique_hash) k1 = request.args.get("k1", type=str) payment_request = request.args.get("pr", type=str) now = int(datetime.now().timestamp()) if not link: return jsonify({ "status": "ERROR", "reason": "LNURL-withdraw not found." }), HTTPStatus.OK if link.is_spent: return jsonify({ "status": "ERROR", "reason": "Withdraw is spent." }), HTTPStatus.OK if link.k1 != k1: return jsonify({ "status": "ERROR", "reason": "Bad request." }), HTTPStatus.OK if now < link.open_time: return jsonify({ "status": "ERROR", "reason": f"Wait {link.open_time - now} seconds." }), HTTPStatus.OK try: pay_invoice(wallet_id=link.wallet, bolt11=payment_request, max_sat=link.max_withdrawable) changes = { "used": link.used + 1, "open_time": link.wait_time + now, } if link.is_unique: changes["unique_hash"] = urlsafe_short_hash() update_withdraw_link(link.id, **changes) except ValueError as e: return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK except PermissionError: return jsonify({ "status": "ERROR", "reason": "Withdraw link is empty." }), HTTPStatus.OK except Exception as e: return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK return jsonify({"status": "OK"}), HTTPStatus.OK
async def create_lnurlpos( title: str, wallet: Optional[str] = None, currency: Optional[str] = None, ) -> lnurlposs: lnurlpos_id = urlsafe_short_hash() lnurlpos_key = urlsafe_short_hash() await db.execute( """ INSERT INTO lnurlpos.lnurlposs ( id, key, title, wallet, currency ) VALUES (?, ?, ?, ?, ?) """, (lnurlpos_id, lnurlpos_key, title, wallet, currency), ) return await get_lnurlpos(lnurlpos_id)
def create_tpos(*, wallet_id: str, name: str, currency: str) -> TPoS: with open_ext_db("tpos") as db: tpos_id = urlsafe_short_hash() db.execute( """ INSERT INTO tposs (id, wallet, name, currency) VALUES (?, ?, ?, ?) """, (tpos_id, wallet_id, name, currency), ) return get_tpos(tpos_id)
def create_form(*, wallet: str, name: str, description: str, costpword: int) -> Forms: with open_ext_db("lnticket") as db: form_id = urlsafe_short_hash() db.execute( """ INSERT INTO forms (id, wallet, name, description, costpword, amountmade) VALUES (?, ?, ?, ?, ?, ?) """, (form_id, wallet, name, description, costpword, 0), ) return get_form(form_id)
async def create_tpos(*, wallet_id: str, name: str, currency: str) -> TPoS: tpos_id = urlsafe_short_hash() await db.execute( """ INSERT INTO tposs (id, wallet, name, currency) VALUES (?, ?, ?, ?) """, (tpos_id, wallet_id, name, currency), ) tpos = await get_tpos(tpos_id) assert tpos, "Newly created tpos couldn't be retrieved" return tpos
async def create_captcha( *, wallet_id: str, url: str, memo: str, description: Optional[str] = None, amount: int = 0, remembers: bool = True ) -> Captcha: captcha_id = urlsafe_short_hash() await db.execute( """ INSERT INTO captchas (id, wallet, url, memo, description, amount, remembers) VALUES (?, ?, ?, ?, ?, ?, ?) """, (captcha_id, wallet_id, url, memo, description, amount, int(remembers)), ) captcha = await get_captcha(captcha_id) assert captcha, "Newly created captcha couldn't be retrieved" return captcha
async def create_service( twitchuser: str, client_id: str, client_secret: str, wallet: str, servicename: str, state: str = None, onchain: str = None, ) -> Service: """Create a new Service""" returning = "" if db.type == SQLITE else "RETURNING ID" method = db.execute if db.type == SQLITE else db.fetchone result = await (method)( f""" INSERT INTO streamalerts.Services ( twitchuser, client_id, client_secret, wallet, servicename, authenticated, state, onchain ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) {returning} """, ( twitchuser, client_id, client_secret, wallet, servicename, False, urlsafe_short_hash(), onchain, ), ) if db.type == SQLITE: service_id = result._result_proxy.lastrowid else: service_id = result[0] service = await get_service(service_id) assert service return service
async def create_satsdice_pay( *, wallet_id: str, title: str, base_url: str, min_bet: str, max_bet: str, multiplier: int = 0, chance: float = 0, haircut: int = 0, ) -> satsdiceLink: satsdice_id = urlsafe_short_hash() await db.execute( """ INSERT INTO satsdice.satsdice_pay ( id, wallet, title, base_url, min_bet, max_bet, amount, served_meta, served_pr, multiplier, chance, haircut, open_time ) VALUES (?, ?, ?, ?, ?, ?, 0, 0, 0, ?, ?, ?, ?) """, ( satsdice_id, wallet_id, title, base_url, min_bet, max_bet, multiplier, chance, haircut, int(datetime.now().timestamp()), ), ) link = await get_satsdice_pay(satsdice_id) assert link, "Newly created link couldn't be retrieved" return link
async def create_form(*, wallet: str, name: str, webhook: Optional[str] = None, description: str, costpword: int) -> Forms: form_id = urlsafe_short_hash() await db.execute( """ INSERT INTO form (id, wallet, name, webhook, description, costpword, amountmade) VALUES (?, ?, ?, ?, ?, ?, ?) """, (form_id, wallet, name, webhook, description, costpword, 0), ) form = await get_form(form_id) assert form, "Newly created form couldn't be retrieved" return form
def create_paywall(*, wallet_id: str, url: str, memo: str, description: Optional[str] = None, amount: int = 0, remembers: bool = True) -> Paywall: with open_ext_db("paywall") as db: paywall_id = urlsafe_short_hash() db.execute( """ INSERT INTO paywalls (id, wallet, url, memo, description, amount, remembers) VALUES (?, ?, ?, ?, ?, ?, ?) """, (paywall_id, wallet_id, url, memo, description, amount, int(remembers)), ) return get_paywall(paywall_id)
async def create_paywall(*, wallet_id: str, url: str, memo: str, description: Optional[str] = None, amount: int = 0, remembers: bool = True) -> Paywall: paywall_id = urlsafe_short_hash() await db.execute( """ INSERT INTO paywalls (id, wallet, url, memo, description, amount, remembers) VALUES (?, ?, ?, ?, ?, ?, ?) """, (paywall_id, wallet_id, url, memo, description, amount, int(remembers)), ) paywall = await get_paywall(paywall_id) assert paywall, "Newly created paywall couldn't be retrieved" return paywall
async def get_fresh_address(wallet_id: str) -> Addresses: wallet = await get_watch_wallet(wallet_id) address = await get_derive_address(wallet_id, wallet[4] + 1) await update_watch_wallet(wallet_id=wallet_id, address_no=wallet[4] + 1) masterpub_id = urlsafe_short_hash() await db.execute( """ INSERT INTO addresses ( id, address, wallet, amount ) VALUES (?, ?, ?, ?) """, (masterpub_id, address, wallet_id, 0), ) return await get_address(address)
async def create_lnurlpospayment( posid: str, payload: Optional[str] = None, pin: Optional[str] = None, sats: Optional[int] = 0, ) -> lnurlpospayment: lnurlpospayment_id = urlsafe_short_hash() await db.execute( """ INSERT INTO lnurlpos.lnurlpospayment ( id, posid, payload, pin, sats ) VALUES (?, ?, ?, ?, ?) """, (lnurlpospayment_id, posid, payload, pin, sats), ) return await get_lnurlpospayment(lnurlpospayment_id)
def create_ticket(wallet: str, event: str, name: str, email: str) -> Tickets: with open_ext_db("events") as db: eventdata = get_event(event) sold = eventdata.sold + 1 amount_tickets = eventdata.amount_tickets - 1 ticket_id = urlsafe_short_hash() db.execute( """ INSERT INTO tickets (id, wallet, event, name, email, registered) VALUES (?, ?, ?, ?, ?, ?) """, (ticket_id, wallet, event, name, email, False), ) db.execute( """ UPDATE events SET sold = ?, amount_tickets = ? WHERE id = ? """, (sold, amount_tickets, event), ) return get_ticket(ticket_id)
async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Wallets: # check the masterpub is fine, it will raise an exception if not parse_key(masterpub) wallet_id = urlsafe_short_hash() await db.execute( """ INSERT INTO watchonly.wallets ( id, "user", masterpub, title, address_no, balance ) VALUES (?, ?, ?, ?, ?, ?) """, # address_no is -1 so fresh address on empty wallet can get address with index 0 (wallet_id, user, masterpub, title, -1, 0), ) return await get_watch_wallet(wallet_id)
async def create_jukebox( inkey: str, user: str, wallet: str, title: str, price: int, sp_user: str, sp_secret: str, sp_access_token: Optional[str] = "", sp_refresh_token: Optional[str] = "", sp_device: Optional[str] = "", sp_playlists: Optional[str] = "", ) -> Jukebox: juke_id = urlsafe_short_hash() result = await db.execute( """ INSERT INTO jukebox.jukebox (id, user, title, wallet, sp_user, sp_secret, sp_access_token, sp_refresh_token, sp_device, sp_playlists, price, profit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( juke_id, user, title, wallet, sp_user, sp_secret, sp_access_token, sp_refresh_token, sp_device, sp_playlists, int(price), 0, ), ) jukebox = await get_jukebox(juke_id) assert jukebox, "Newly created Jukebox couldn't be retrieved" return jukebox
def m002_change_withdraw_table(db): """ Creates an improved withdraw table and migrates the existing data. """ db.execute(""" CREATE TABLE IF NOT EXISTS withdraw_links ( id TEXT PRIMARY KEY, wallet TEXT, title TEXT, min_withdrawable INTEGER DEFAULT 1, max_withdrawable INTEGER DEFAULT 1, uses INTEGER DEFAULT 1, wait_time INTEGER, is_unique INTEGER DEFAULT 0, unique_hash TEXT UNIQUE, k1 TEXT, open_time INTEGER, used INTEGER DEFAULT 0 ); """) db.execute( "CREATE INDEX IF NOT EXISTS wallet_idx ON withdraw_links (wallet)") db.execute( "CREATE UNIQUE INDEX IF NOT EXISTS unique_hash_idx ON withdraw_links (unique_hash)" ) for row in [list(row) for row in db.fetchall("SELECT * FROM withdraws")]: db.execute( """ INSERT INTO withdraw_links ( id, wallet, title, min_withdrawable, max_withdrawable, uses, wait_time, is_unique, unique_hash, k1, open_time, used ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( row[5], # uni row[2], # wal row[6], # tit row[8], # minamt row[7], # maxamt row[10], # inc row[11], # tme row[12], # uniq urlsafe_short_hash(), urlsafe_short_hash(), int(datetime.now().timestamp()) + row[11], row[9], # spent ), ) db.execute("DROP TABLE withdraws")
async def create_copilot( title: str, user: str, lnurl_toggle: Optional[int] = 0, wallet: Optional[str] = None, animation1: Optional[str] = None, animation2: Optional[str] = None, animation3: Optional[str] = None, animation1threshold: Optional[int] = None, animation2threshold: Optional[int] = None, animation3threshold: Optional[int] = None, animation1webhook: Optional[str] = None, animation2webhook: Optional[str] = None, animation3webhook: Optional[str] = None, lnurl_title: Optional[str] = None, show_message: Optional[int] = 0, show_ack: Optional[int] = 0, show_price: Optional[str] = None, amount_made: Optional[int] = None, ) -> Copilots: copilot_id = urlsafe_short_hash() await db.execute( """ INSERT INTO copilot.copilots ( id, "user", lnurl_toggle, wallet, title, animation1, animation2, animation3, animation1threshold, animation2threshold, animation3threshold, animation1webhook, animation2webhook, animation3webhook, lnurl_title, show_message, show_ack, show_price, amount_made ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( copilot_id, user, int(lnurl_toggle), wallet, title, animation1, animation2, animation3, animation1threshold, animation2threshold, animation3threshold, animation1webhook, animation2webhook, animation3webhook, lnurl_title, int(show_message), int(show_ack), show_price, 0, ), ) return await get_copilot(copilot_id)
async def create_charge( user: str, description: str = None, onchainwallet: Optional[str] = None, lnbitswallet: Optional[str] = None, webhook: Optional[str] = None, completelink: Optional[str] = None, completelinktext: Optional[str] = "Back to Merchant", time: Optional[int] = None, amount: Optional[int] = None, ) -> Charges: charge_id = urlsafe_short_hash() if onchainwallet: wallet = await get_watch_wallet(onchainwallet) onchain = await get_fresh_address(onchainwallet) onchainaddress = onchain.address else: onchainaddress = None if lnbitswallet: payment_hash, payment_request = await create_invoice( wallet_id=lnbitswallet, amount=amount, memo=charge_id) else: payment_hash = None payment_request = None await db.execute( """ INSERT INTO satspay.charges ( id, "user", description, onchainwallet, onchainaddress, lnbitswallet, payment_request, payment_hash, webhook, completelink, completelinktext, time, amount, balance ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( charge_id, user, description, onchainwallet, onchainaddress, lnbitswallet, payment_request, payment_hash, webhook, completelink, completelinktext, time, amount, 0, ), ) return await get_charge(charge_id)