lnbits-legend/lnbits/extensions/admin/migrations.py

292 lines
8.1 KiB
Python
Raw Normal View History

2022-03-07 05:03:32 +00:00
from os import getenv
2022-03-12 14:18:09 +00:00
from sqlalchemy.exc import OperationalError # type: ignore
2022-03-18 16:59:06 +00:00
from lnbits.config import conf
2022-03-07 05:03:32 +00:00
from lnbits.helpers import urlsafe_short_hash
2022-05-16 10:49:21 +01:00
async def get_admin_user():
if(conf.admin_users[0]):
return conf.admin_users[0]
from lnbits.core.crud import create_account, get_user
print("Seems like there's no admin users yet. Let's create an account for you!")
account = await create_account()
user = account.id
assert user, "Newly created user couldn't be retrieved"
print(f"Your newly created account/user id is: {user}. This will be the Super Admin user.")
2022-05-16 12:53:47 +01:00
conf.admin_users.insert(0, user)
2022-05-16 10:49:21 +01:00
return user
2022-03-07 05:03:32 +00:00
async def m001_create_admin_table(db):
2022-03-18 16:59:06 +00:00
# users/server
2022-05-16 10:49:21 +01:00
user = await get_admin_user()
2022-03-18 16:59:06 +00:00
admin_users = ",".join(conf.admin_users)
allowed_users = ",".join(conf.allowed_users)
admin_ext = ",".join(conf.admin_ext)
disabled_ext = ",".join(conf.disabled_ext)
funding_source = conf.funding_source
#operational
data_folder = conf.data_folder
database_url = conf.database_url
force_https = conf.force_https
2022-06-08 14:58:41 +01:00
reserve_fee_min = conf.reserve_fee_min
reserve_fee_pct = conf.reserve_fee_pct
2022-03-18 16:59:06 +00:00
service_fee = conf.service_fee
hide_api = conf.hide_api
denomination = conf.denomination
# Theme'ing
site_title = conf.site_title
site_tagline = conf.site_tagline
site_description = conf.site_description
default_wallet_name = conf.default_wallet_name
theme = ",".join(conf.theme)
2022-06-08 11:00:43 +01:00
custom_logo = conf.custom_logo
2022-03-18 16:59:06 +00:00
ad_space = ",".join(conf.ad_space)
2022-03-07 05:03:32 +00:00
await db.execute(
"""
2022-05-16 12:29:58 +01:00
CREATE TABLE IF NOT EXISTS admin.admin (
2022-03-18 16:59:06 +00:00
"user" TEXT PRIMARY KEY,
2022-03-12 14:18:09 +00:00
admin_users TEXT,
2022-03-07 05:03:32 +00:00
allowed_users TEXT,
2022-03-18 16:59:06 +00:00
admin_ext TEXT,
2022-03-07 05:03:32 +00:00
disabled_ext TEXT,
2022-03-18 16:59:06 +00:00
funding_source TEXT,
data_folder TEXT,
database_url TEXT,
2022-03-07 05:03:32 +00:00
force_https BOOLEAN,
2022-06-08 14:58:41 +01:00
reserve_fee_min INT,
reserve_fee_pct REAL,
2022-03-12 14:18:09 +00:00
service_fee REAL,
2022-03-18 16:59:06 +00:00
hide_api BOOLEAN,
denomination TEXT,
site_title TEXT,
site_tagline TEXT,
site_description TEXT,
default_wallet_name TEXT,
theme TEXT,
2022-06-08 11:00:43 +01:00
custom_logo TEXT,
2022-03-18 16:59:06 +00:00
ad_space TEXT
2022-03-07 05:03:32 +00:00
);
"""
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.admin (
2022-03-18 16:59:06 +00:00
"user",
admin_users,
allowed_users,
admin_ext,
disabled_ext,
funding_source,
data_folder,
database_url,
force_https,
2022-06-08 14:58:41 +01:00
reserve_fee_min,
reserve_fee_pct,
2022-03-18 16:59:06 +00:00
service_fee,
hide_api,
denomination,
2022-03-07 05:03:32 +00:00
site_title,
site_tagline,
site_description,
default_wallet_name,
2022-03-18 16:59:06 +00:00
theme,
2022-06-08 11:00:43 +01:00
custom_logo,
2022-03-18 16:59:06 +00:00
ad_space)
2022-06-08 14:58:41 +01:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2022-03-18 16:59:06 +00:00
""",
(
user,
admin_users,
allowed_users,
admin_ext,
2022-03-07 05:03:32 +00:00
disabled_ext,
2022-03-18 16:59:06 +00:00
funding_source,
data_folder,
database_url,
2022-03-07 05:03:32 +00:00
force_https,
2022-06-08 14:58:41 +01:00
reserve_fee_min,
reserve_fee_pct,
2022-03-07 05:03:32 +00:00
service_fee,
2022-03-18 16:59:06 +00:00
hide_api,
denomination,
site_title,
site_tagline,
site_description,
default_wallet_name,
theme,
2022-06-08 11:00:43 +01:00
custom_logo,
2022-03-18 16:59:06 +00:00
ad_space,
2022-03-07 05:03:32 +00:00
),
)
async def m001_create_funding_table(db):
funding_wallet = getenv("LNBITS_BACKEND_WALLET_CLASS")
# Make the funding table, if it does not already exist
await db.execute(
"""
2022-05-16 12:29:58 +01:00
CREATE TABLE IF NOT EXISTS admin.funding (
2022-03-07 05:03:32 +00:00
id TEXT PRIMARY KEY,
backend_wallet TEXT,
endpoint TEXT,
port INT,
read_key TEXT,
invoice_key TEXT,
admin_key TEXT,
cert TEXT,
balance INT,
selected INT
);
"""
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"CLightningWallet",
getenv("CLIGHTNING_RPC"),
1 if funding_wallet == "CLightningWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"SparkWallet",
getenv("SPARK_URL"),
getenv("SPARK_TOKEN"),
1 if funding_wallet == "SparkWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LnbitsWallet",
getenv("LNBITS_ENDPOINT"),
getenv("LNBITS_KEY"),
1 if funding_wallet == "LnbitsWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, port, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LndWallet",
getenv("LND_GRPC_ENDPOINT"),
getenv("LND_GRPC_PORT"),
getenv("LND_GRPC_MACAROON"),
getenv("LND_GRPC_CERT"),
1 if funding_wallet == "LndWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LndRestWallet",
getenv("LND_REST_ENDPOINT"),
getenv("LND_REST_MACAROON"),
getenv("LND_REST_CERT"),
1 if funding_wallet == "LndWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LNPayWallet",
getenv("LNPAY_API_ENDPOINT"),
getenv("LNPAY_WALLET_KEY"),
getenv("LNPAY_API_KEY"), # this is going in as the cert
1 if funding_wallet == "LNPayWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LntxbotWallet",
getenv("LNTXBOT_API_ENDPOINT"),
getenv("LNTXBOT_KEY"),
1 if funding_wallet == "LntxbotWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"OpenNodeWallet",
getenv("OPENNODE_API_ENDPOINT"),
getenv("OPENNODE_KEY"),
1 if funding_wallet == "OpenNodeWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"SparkWallet",
getenv("SPARK_URL"),
getenv("SPARK_TOKEN"),
1 if funding_wallet == "SparkWallet" else 0,
),
)
## PLACEHOLDER FOR ECLAIR WALLET
# await db.execute(
# """
2022-05-16 12:29:58 +01:00
# INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
# VALUES (?, ?, ?, ?, ?)
# """,
# (
# urlsafe_short_hash(),
# "EclairWallet",
# getenv("ECLAIR_URL"),
# getenv("ECLAIR_PASS"),
# 1 if funding_wallet == "EclairWallet" else 0,
# ),
# )