feat: add network for wallet accounts

This commit is contained in:
Vlad Stan 2022-08-01 11:51:10 +03:00
parent 4671954896
commit 6150b767e4
9 changed files with 57 additions and 23 deletions

View File

@ -22,9 +22,10 @@ async def create_watch_wallet(w: WalletAccount) -> WalletAccount:
title,
type,
address_no,
balance
balance,
network
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
wallet_id,
@ -35,6 +36,7 @@ async def create_watch_wallet(w: WalletAccount) -> WalletAccount:
w.type,
w.address_no,
w.balance,
w.network,
),
)
@ -48,9 +50,10 @@ async def get_watch_wallet(wallet_id: str) -> Optional[WalletAccount]:
return WalletAccount.from_row(row) if row else None
async def get_watch_wallets(user: str) -> List[WalletAccount]:
async def get_watch_wallets(user: str, network: str) -> List[WalletAccount]:
rows = await db.fetchall(
"""SELECT * FROM watchonly.wallets WHERE "user" = ?""", (user,)
"""SELECT * FROM watchonly.wallets WHERE "user" = ? AND network = ?""",
(user, network),
)
return [WalletAccount(**row) for row in rows]

View File

@ -77,6 +77,16 @@ async def m004_create_config_table(db):
);"""
)
async def m005_add_network_column_to_wallets(db):
"""
Add network' column to the 'wallets' table
"""
await db.execute(
"ALTER TABLE watchonly.wallets ADD COLUMN network TEXT DEFAULT 'Mainnet';"
)
### TODO: fix statspay dependcy first
# await db.execute(
# "DROP TABLE watchonly.wallets;"

View File

@ -7,6 +7,7 @@ from pydantic import BaseModel
class CreateWallet(BaseModel):
masterpub: str = Query("")
title: str = Query("")
network: str = "Mainnet"
class WalletAccount(BaseModel):

View File

@ -65,7 +65,7 @@
<div>
<a
style="color: unset"
:href="mempoolEndpoint + '/address/' + props.row.address"
:href="'https://'+ mempoolEndpoint + '/address/' + props.row.address"
target="_blank"
>
{{props.row.address}}</a

View File

@ -8,11 +8,7 @@ async function walletConfig(path) {
data: function () {
return {
networOptions: ['Mainnet', 'Testnet'],
internalConfig: {
mempool_endpoint: 'https://mempool.space',
receive_gap_limit: 20,
change_gap_limit: 5
},
internalConfig: {},
show: false
}
},

View File

@ -4,7 +4,7 @@ async function walletList(path) {
name: 'wallet-list',
template,
props: ['adminkey', 'inkey', 'sats-denominated', 'addresses'],
props: ['adminkey', 'inkey', 'sats-denominated', 'addresses', 'network'],
data: function () {
return {
walletAccounts: [],
@ -47,6 +47,15 @@ async function walletList(path) {
}
}
},
watch: {
immediate: true,
async network(newNet, oldNet) {
console.log('### newNet, oldNet', newNet, oldNet)
if (newNet !== oldNet) {
await this.refreshWalletAccounts()
}
}
},
methods: {
satBtc(val, showUnit = true) {
@ -55,6 +64,7 @@ async function walletList(path) {
addWalletAccount: async function () {
const data = _.omit(this.formDialog.data, 'wallet')
data.network = this.network
await this.createWalletAccount(data)
},
createWalletAccount: async function (data) {
@ -106,7 +116,7 @@ async function walletList(path) {
try {
const {data} = await LNbits.api.request(
'GET',
'/watchonly/api/v1/wallet',
`/watchonly/api/v1/wallet/?network=${this.network}`,
this.inkey
)
return data

View File

@ -47,14 +47,15 @@ const watchOnly = async () => {
showAddress: false,
addressNote: '',
showPayment: false,
fetchedUtxos: false
fetchedUtxos: false,
network: null
}
},
computed: {
mempoolHostname() {
mempoolHostname: function () {
if (!this.config.isLoaded) return
const hostname = new URL(this.config.mempool_endpoint).hostname
if (this.config.network === 'testnet') {
let hostname = new URL(this.config.mempool_endpoint).hostname
if (this.config.network === 'Testnet') {
hostname += '/testnet'
}
return hostname

View File

@ -17,9 +17,11 @@
</wallet-config>
<wallet-list
v-if="config.isLoaded"
:adminkey="g.user.wallets[0].adminkey"
:inkey="g.user.wallets[0].inkey"
:sats-denominated="config.sats_denominated"
:network="config.network"
:addresses="addresses"
@accounts-update="updateAccounts"
@new-receive-address="showAddressDetails"

View File

@ -49,12 +49,17 @@ from .helpers import parse_key
@watchonly_ext.get("/api/v1/wallet")
async def api_wallets_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
async def api_wallets_retrieve(
network: str = Query("Mainnet"), wallet: WalletTypeInfo = Depends(get_key_type)
):
try:
return [wallet.dict() for wallet in await get_watch_wallets(wallet.wallet.user)]
return [
wallet.dict()
for wallet in await get_watch_wallets(wallet.wallet.user, network)
]
except:
return ""
return []
@watchonly_ext.get("/api/v1/wallet/{wallet_id}")
@ -76,7 +81,13 @@ async def api_wallet_create_or_update(
data: CreateWallet, w: WalletTypeInfo = Depends(require_admin_key)
):
try:
(descriptor, _) = parse_key(data.masterpub)
(descriptor, network) = parse_key(data.masterpub)
if data.network != network["name"]:
raise ValueError(
"Account network error. This account is for '{}'".format(
network["name"]
)
)
new_wallet = WalletAccount(
id="none",
@ -87,9 +98,10 @@ async def api_wallet_create_or_update(
title=data.title,
address_no=-1, # so fresh address on empty wallet can get address with index 0
balance=0,
network=network["name"],
)
wallets = await get_watch_wallets(w.wallet.user)
wallets = await get_watch_wallets(w.wallet.user, network["name"])
existing_wallet = next(
(ew for ew in wallets if ew.fingerprint == new_wallet.fingerprint), None
)
@ -233,7 +245,7 @@ async def api_psbt_create(
descriptors[masterpub.fingerprint] = parse_key(masterpub.public_key)
inputs_extra = []
for i, inp in enumerate(data.inputs):
bip32_derivations = {}
descriptor = descriptors[inp.masterpub_fingerprint][0]
@ -292,7 +304,6 @@ async def api_psbt_extract_tx(
if not final_psbt:
raise ValueError("PSBT cannot be finalized!")
res.tx_hex = final_psbt.to_string()
print('### hex', res.tx_hex)
transaction = Transaction.from_string(res.tx_hex)
tx = {