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,
UserConfig,
Wallet,
WalletConfig,
WebPushSubscription,
)
@ -348,7 +349,7 @@ async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[
extensions=[
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
or user["id"] in settings.lnbits_admin_users,
super_user=user["id"] == settings.super_user,
@ -584,6 +585,7 @@ async def update_wallet(
wallet_id: str,
name: Optional[str] = None,
currency: Optional[str] = None,
config: Optional[WalletConfig] = None,
conn: Optional[Connection] = None,
) -> Optional[Wallet]:
set_clause = []
@ -597,6 +599,11 @@ async def update_wallet(
if currency is not None:
set_clause.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)
await (conn or db).execute(
f"""
@ -688,7 +695,7 @@ async def get_wallet(
(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]:
@ -700,7 +707,7 @@ async def get_wallets(user_id: str, conn: Optional[Connection] = None) -> List[W
(user_id,),
)
return [Wallet(**row) for row in rows]
return [Wallet.from_row(row) for row in rows]
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.
"""
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
class WalletConfig(BaseModel):
reverse_funding_access: Optional[str] = ""
reverse_funding_url: Optional[str] = None
reverse_funding_secret: Optional[str] = None
class BaseWallet(BaseModel):
id: str
name: str
@ -33,8 +39,10 @@ class Wallet(BaseWallet):
user: str
currency: Optional[str]
deleted: bool
reverse_funding_enabled: bool
created_at: Optional[int] = None
updated_at: Optional[int] = None
config: WalletConfig = WalletConfig()
@property
def balance(self) -> int:
@ -67,6 +75,13 @@ class Wallet(BaseWallet):
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):
admin = 0

View File

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

View File

@ -10,6 +10,7 @@ from lnbits.core.models import (
CreateWallet,
KeyType,
Wallet,
WalletConfig,
)
from lnbits.decorators import (
WalletTypeInfo,
@ -54,9 +55,10 @@ async def api_update_wallet_name(
async def api_update_wallet(
name: Optional[str] = Body(None),
currency: Optional[str] = Body(None),
config: Optional[WalletConfig] = Body(None),
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("")

File diff suppressed because one or more lines are too long

View File

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

View File

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