feat: persist wallet changes

This commit is contained in:
Vlad Stan 2024-06-21 11:39:23 +03:00
parent 9868e9ea52
commit e89a8a65ba
8 changed files with 52 additions and 17 deletions

View File

@ -35,6 +35,7 @@ from .models import (
User, User,
UserConfig, UserConfig,
Wallet, Wallet,
WalletConfig,
WebPushSubscription, WebPushSubscription,
) )
@ -348,7 +349,7 @@ async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[
extensions=[ extensions=[
e for e in extensions if User.is_extension_for_user(e[0], user["id"]) e for e in extensions if User.is_extension_for_user(e[0], user["id"])
], ],
wallets=[Wallet(**w) for w in wallets], wallets=[Wallet.from_row(w) for w in wallets],
admin=user["id"] == settings.super_user admin=user["id"] == settings.super_user
or user["id"] in settings.lnbits_admin_users, or user["id"] in settings.lnbits_admin_users,
super_user=user["id"] == settings.super_user, super_user=user["id"] == settings.super_user,
@ -584,6 +585,7 @@ async def update_wallet(
wallet_id: str, wallet_id: str,
name: Optional[str] = None, name: Optional[str] = None,
currency: Optional[str] = None, currency: Optional[str] = None,
config: Optional[WalletConfig] = None,
conn: Optional[Connection] = None, conn: Optional[Connection] = None,
) -> Optional[Wallet]: ) -> Optional[Wallet]:
set_clause = [] set_clause = []
@ -597,6 +599,11 @@ async def update_wallet(
if currency is not None: if currency is not None:
set_clause.append("currency = ?") set_clause.append("currency = ?")
values.append(currency) values.append(currency)
if config is not None:
set_clause.append("extra = ?")
values.append(json.dumps(dict(config)))
set_clause.append("reverse_funding_enabled = ?")
values.append(config.reverse_funding_access in ["inkey", "adminkey"])
values.append(wallet_id) values.append(wallet_id)
await (conn or db).execute( await (conn or db).execute(
f""" f"""
@ -688,7 +695,7 @@ async def get_wallet(
(wallet_id,), (wallet_id,),
) )
return Wallet(**row) if row else None return Wallet.from_row(row) if row else None
async def get_wallets(user_id: str, conn: Optional[Connection] = None) -> List[Wallet]: async def get_wallets(user_id: str, conn: Optional[Connection] = None) -> List[Wallet]:
@ -700,7 +707,7 @@ async def get_wallets(user_id: str, conn: Optional[Connection] = None) -> List[W
(user_id,), (user_id,),
) )
return [Wallet(**row) for row in rows] return [Wallet.from_row(row) for row in rows]
async def get_wallet_for_key( async def get_wallet_for_key(

View File

@ -515,8 +515,19 @@ async def m019_balances_view_based_on_wallets(db):
) )
async def m020_add_column_column_to_user_extensions(db): async def m020_add_column_extra_to_user_extensions(db):
""" """
Adds extra column to user extensions. Adds extra column to user extensions.
""" """
await db.execute("ALTER TABLE extensions ADD COLUMN extra TEXT") await db.execute("ALTER TABLE extensions ADD COLUMN extra TEXT")
async def m021_add_column_extra_to_wallets(db):
"""
Adds extra column to user extensions.
"""
await db.execute("ALTER TABLE wallets ADD COLUMN extra TEXT")
await db.execute(
"ALTER TABLE wallets "
"ADD COLUMN reverse_funding_enabled BOOLEAN NOT NULL DEFAULT false"
)

View File

@ -21,6 +21,12 @@ from lnbits.wallets import get_funding_source
from lnbits.wallets.base import PaymentPendingStatus, PaymentStatus from lnbits.wallets.base import PaymentPendingStatus, PaymentStatus
class WalletConfig(BaseModel):
reverse_funding_access: Optional[str] = ""
reverse_funding_url: Optional[str] = None
reverse_funding_secret: Optional[str] = None
class BaseWallet(BaseModel): class BaseWallet(BaseModel):
id: str id: str
name: str name: str
@ -33,8 +39,10 @@ class Wallet(BaseWallet):
user: str user: str
currency: Optional[str] currency: Optional[str]
deleted: bool deleted: bool
reverse_funding_enabled: bool
created_at: Optional[int] = None created_at: Optional[int] = None
updated_at: Optional[int] = None updated_at: Optional[int] = None
config: WalletConfig = WalletConfig()
@property @property
def balance(self) -> int: def balance(self) -> int:
@ -67,6 +75,13 @@ class Wallet(BaseWallet):
return await get_standalone_payment(payment_hash) return await get_standalone_payment(payment_hash)
@classmethod
def from_row(cls, row: Row):
wallet = cls(**dict(row))
if row["extra"]:
wallet.config = WalletConfig(**json.loads(row["extra"]))
return wallet
class KeyType(Enum): class KeyType(Enum):
admin = 0 admin = 0

View File

@ -270,8 +270,10 @@
size="sm" size="sm"
style="min-width: 200px" style="min-width: 200px"
filled filled
emit-value
map-options
:options="reverseFundingOptions" :options="reverseFundingOptions"
v-model="reverseFundingConfig.access" v-model="update.config.reverse_funding_access"
class="col-auto" class="col-auto"
></q-select> ></q-select>
</div> </div>
@ -280,7 +282,7 @@
<div class="row q-mt-md"> <div class="row q-mt-md">
<div class="col"> <div class="col">
<q-input <q-input
v-model="reverseFundingConfig.url" v-model="update.config.reverse_funding_url"
filled filled
label="Websocket Address" label="Websocket Address"
placeholder="wss://myinstance.com" placeholder="wss://myinstance.com"
@ -292,7 +294,7 @@
<div class="row q-mt-md"> <div class="row q-mt-md">
<div class="col"> <div class="col">
<q-input <q-input
v-model="reverseFundingConfig.secret" v-model="update.config.reverse_funding_secret"
filled filled
type="password" type="password"
label="Secret" label="Secret"
@ -311,6 +313,7 @@
label="Test Connection" label="Test Connection"
></q-btn> ></q-btn>
<q-btn <q-btn
@click="updateWallet({ config: update.config })"
unelevated unelevated
class="float-right" class="float-right"
color="primary" color="primary"

View File

@ -10,6 +10,7 @@ from lnbits.core.models import (
CreateWallet, CreateWallet,
KeyType, KeyType,
Wallet, Wallet,
WalletConfig,
) )
from lnbits.decorators import ( from lnbits.decorators import (
WalletTypeInfo, WalletTypeInfo,
@ -54,9 +55,10 @@ async def api_update_wallet_name(
async def api_update_wallet( async def api_update_wallet(
name: Optional[str] = Body(None), name: Optional[str] = Body(None),
currency: Optional[str] = Body(None), currency: Optional[str] = Body(None),
config: Optional[WalletConfig] = Body(None),
wallet: WalletTypeInfo = Depends(require_admin_key), wallet: WalletTypeInfo = Depends(require_admin_key),
): ):
return await update_wallet(wallet.wallet.id, name, currency) return await update_wallet(wallet.wallet.id, name, currency, config=config)
@wallet_router.delete("") @wallet_router.delete("")

File diff suppressed because one or more lines are too long

View File

@ -234,7 +234,8 @@ window.LNbits = {
name: data.name, name: data.name,
adminkey: data.adminkey, adminkey: data.adminkey,
inkey: data.inkey, inkey: data.inkey,
currency: data.currency currency: data.currency,
config: data.config || {}
} }
newWallet.msat = data.balance_msat newWallet.msat = data.balance_msat
newWallet.sat = Math.floor(data.balance_msat / 1000) newWallet.sat = Math.floor(data.balance_msat / 1000)

View File

@ -58,15 +58,10 @@ new Vue({
currency: null currency: null
}, },
reverseFundingOptions: [ reverseFundingOptions: [
{label: 'Disabled', value: 'none'}, {label: 'Disabled', value: ''},
{label: 'Allow Receive Only', value: 'inkey'}, {label: 'Allow Receive Only', value: 'inkey'},
{label: 'Allow Receive and Spend ', value: 'adminkey'} {label: 'Allow Receive and Spend ', value: 'adminkey'}
], ]
reverseFundingConfig: {
access: {label: 'Disabled', value: 'none'},
url: '',
secret: ''
}
} }
}, },
computed: { computed: {
@ -569,6 +564,7 @@ new Vue({
} }
this.update.name = this.g.wallet.name this.update.name = this.g.wallet.name
this.update.currency = this.g.wallet.currency this.update.currency = this.g.wallet.currency
this.update.config = this.g.wallet.config
this.receive.units = ['sat', ...window.currencies] this.receive.units = ['sat', ...window.currencies]
this.updateFiatBalance() this.updateFiatBalance()
}, },