lnbits-legend/lnbits/extensions/watchonly/static/components/payment/payment.js

172 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-07-25 17:37:06 +03:00
async function payment(path) {
2022-07-26 10:44:36 +03:00
const t = await loadTemplateAsync(path)
2022-07-25 17:37:06 +03:00
Vue.component('payment', {
name: 'payment',
2022-07-26 10:44:36 +03:00
template: t,
2022-07-25 17:37:06 +03:00
2022-07-26 11:31:23 +03:00
props: [
'accounts',
'addresses',
'utxos',
'mempool_endpoint',
'sats_denominated'
],
watch: {
immediate: true,
accounts() {
this.updateChangeAddress()
},
addresses() {
2022-07-26 11:31:23 +03:00
this.updateChangeAddress()
}
},
2022-07-25 17:37:06 +03:00
data: function () {
2022-07-26 10:44:36 +03:00
return {
DUST_LIMIT: 546,
2022-07-26 10:44:36 +03:00
paymentTab: 'destination',
2022-07-26 11:08:10 +03:00
sendToList: [{address: '', amount: undefined}],
2022-07-26 10:44:36 +03:00
changeWallet: null,
changeAddress: {},
changeAmount: 0,
showCustomFee: false,
2022-07-26 15:54:08 +03:00
showCoinSelect: false,
showChange: false,
2022-07-26 10:44:36 +03:00
feeRate: 1
}
},
computed: {
txSize: function () {
const tx = this.createTx()
return Math.round(txSize(tx))
},
txSizeNoChange: function () {
const tx = this.createTx(true)
return Math.round(txSize(tx))
},
feeValue: function () {
return this.feeRate * this.txSize
2022-07-26 14:09:16 +03:00
},
selectedAmount: function () {
return this.utxos
.filter(utxo => utxo.selected)
.reduce((t, a) => t + (a.amount || 0), 0)
2022-07-26 15:54:08 +03:00
},
balance: function () {
return this.utxos.reduce((t, a) => t + (a.amount || 0), 0)
},
totalPayedAmount: function () {
return this.sendToList.reduce((t, a) => t + (a.amount || 0), 0)
2022-07-26 10:44:36 +03:00
}
2022-07-25 17:37:06 +03:00
},
methods: {
satBtc(val, showUnit = true) {
return satOrBtc(val, showUnit, this['sats_denominated'])
2022-07-26 10:44:36 +03:00
},
createPsbt: async function () {
const wallet = this.g.user.wallets[0]
try {
// this.computeFee(this.feeRate)
const tx = this.createTx()
// txSize(tx)
for (const input of tx.inputs) {
input.tx_hex = await this.fetchTxHex(input.tx_id)
}
this.payment.tx = tx
const {data} = await LNbits.api.request(
'POST',
'/watchonly/api/v1/psbt',
wallet.adminkey,
tx
)
this.payment.psbtBase64 = data
} catch (err) {
LNbits.utils.notifyApiError(err)
}
},
createTx: function (excludeChange = false) {
const tx = {
fee_rate: this.feeRate,
// tx_size: this.payment.txSize, ???
masterpubs: this.accounts.map(w => ({
public_key: w.masterpub,
fingerprint: w.fingerprint
}))
}
2022-07-26 14:09:16 +03:00
tx.inputs = this.utxos
2022-07-26 10:44:36 +03:00
.filter(utxo => utxo.selected)
.map(mapUtxoToPsbtInput)
.sort((a, b) =>
a.tx_id < b.tx_id ? -1 : a.tx_id > b.tx_id ? 1 : a.vout - b.vout
)
tx.outputs = this.sendToList.map(out => ({
address: out.address,
amount: out.amount
}))
if (excludeChange) {
this.changeAmount = 0
} else {
const change = this.createChangeOutput()
this.changeAmount = change.amount // todo: compute separately
if (change.amount >= this.DUST_LIMIT) {
tx.outputs.push(change)
}
}
// Only sort by amount on UI level (no lib for address decode)
// Should sort by scriptPubKey (as byte array) on the backend
// todo: just shuffle
tx.outputs.sort((a, b) => a.amount - b.amount)
return tx
},
createChangeOutput: function () {
const change = this.changeAddress
// const inputAmount = this.getTotalSelectedUtxoAmount() // todo: set amount separately
// const payedAmount = this.getTotalPaymentAmount()
const walletAcount =
this.accounts.find(w => w.id === change.wallet) || {}
return {
address: change.address,
// amount: inputAmount - payedAmount - this.feeValue,
addressIndex: change.addressIndex,
addressIndex: change.addressIndex,
masterpub_fingerprint: walletAcount.fingerprint
}
},
2022-07-26 11:31:23 +03:00
selectChangeAddress: function (account) {
if (!account) this.changeAddress = ''
2022-07-26 10:44:36 +03:00
this.changeAddress =
this.addresses.find(
2022-07-26 11:31:23 +03:00
a => a.wallet === account.id && a.isChange && !a.hasActivity
2022-07-26 10:44:36 +03:00
) || {}
},
2022-07-26 11:31:23 +03:00
updateChangeAddress: function () {
if (this.changeWallet) {
const changeAccount = (this.accounts || []).find(
w => w.id === this.changeWallet.id
)
// change account deleted
if (!changeAccount) {
this.changeWallet = this.accounts[0]
}
} else {
this.changeWallet = this.accounts[0]
}
this.selectChangeAddress(this.changeWallet)
2022-07-26 11:31:23 +03:00
},
2022-07-26 10:44:36 +03:00
getTotalPaymentAmount: function () {
return this.sendToList.reduce((t, a) => t + (a.amount || 0), 0)
2022-07-25 17:37:06 +03:00
}
},
created: async function () {}
})
}