Commit 65dbec24 authored by Bitcoin Please's avatar Bitcoin Please
Browse files

Added coin locking to `updateCoins`.

parent 9cb8060d
......@@ -4,24 +4,24 @@ const Nito = require('nitojs')
/**
* Update Status
*/
const updateStatus = (_coins, dispatch) => {
Object.keys(_coins).forEach(async coinId => {
const updateStatus = (_coins, _meta, dispatch) => {
Object.keys(_coins).forEach(async coinid => {
/* Set txid. */
const txid = coinId.split(':')[0]
console.log('UPDATE STATUS (txid)', txid)
const txid = coinid.split(':')[0]
// console.log('UPDATE STATUS (txid)', txid)
/* Set vout. */
const vout = coinId.split(':')[1]
console.log('UPDATE STATUS (vout)', vout)
const vout = coinid.split(':')[1]
// console.log('UPDATE STATUS (vout)', vout)
/* Query spent status. */
const isSpent = await Nito.Blockchain.Query.isSpent(txid, vout)
console.log('UPDATE STATUS (isSpent)', isSpent, txid, vout)
// console.log('UPDATE STATUS (isSpent)', isSpent, txid, vout)
/* Validate spent. */
if (isSpent) {
/* Set coin. */
const coin = _coins[coinId]
const coin = _coins[coinid]
/* Validate status. */
if (coin && coin.status !== 'disabled') {
......@@ -31,7 +31,27 @@ const updateStatus = (_coins, dispatch) => {
/* Request coin update. */
dispatch('updateCoin', coin)
}
} else {
if (!_meta.coins[coinid]) {
return
}
if (_meta.coins[coinid].lock && _meta.coins[coinid].lock.isActive === true) {
/* Set coin. */
const coin = _coins[coinid]
/* Validate status. */
if (coin && coin.status !== 'locked') {
/* Set status. */
coin.status = 'locked'
/* Request coin update. */
dispatch('updateCoin', coin)
}
}
}
})
}
......@@ -42,7 +62,7 @@ const updateStatus = (_coins, dispatch) => {
const updateCoins = async ({ dispatch, getters }) => {
/* Set wallet. */
const wallet = getters.getWallet
console.log('UPDATE COINS (wallet)', wallet)
// console.log('UPDATE COINS (wallet)', wallet)
/* Validate wallet. */
if (!wallet || !wallet.coins) {
......@@ -51,14 +71,18 @@ const updateCoins = async ({ dispatch, getters }) => {
/* Set coins. */
const coins = wallet.coins
console.log('UPDATE COINS (coins)', coins)
// console.log('UPDATE COINS (coins)', coins)
/* Retrieve metadata. */
const meta = getters.getMeta
console.log('UPDATE COINS (meta):', meta)
/* Update status. */
updateStatus(coins, dispatch)
updateStatus(coins, meta, dispatch)
/* Retrieve accounts. */
const accounts = getters.getAccounts
console.log('UPDATE COINS (accounts)', accounts)
// console.log('UPDATE COINS (accounts)', accounts)
/* Validate accounts. */
if (accounts === null) {
......@@ -69,7 +93,7 @@ const updateCoins = async ({ dispatch, getters }) => {
const addresses = accounts.map(obj => {
return obj.address
})
console.log('UPDATE COINS (addresses)', addresses)
// console.log('UPDATE COINS (addresses)', addresses)
/* Initialize search details. */
const searchDetails = []
......@@ -209,7 +233,7 @@ const updateCoins = async ({ dispatch, getters }) => {
console.error(err) // eslint-disable-line no-console
}
} else {
console.error('Coin already exists in the purse.')
// console.error('Coin already exists in the purse.')
}
}
})
......
......@@ -145,6 +145,9 @@ import Header from '@/components/Header.vue'
import Banner from '@/components/Account/Banner.vue'
import Sidenav from '@/components/Account/Sidenav.vue'
const DUST = 546
const FEE = 270
export default {
components: {
Footer,
......@@ -390,9 +393,6 @@ export default {
const changeAddress = this.getAddress('change')
console.log('CHANGE ADDRESS', changeAddress)
const DUST = 546
const FEE = 270
if (_coin.satoshis - _satoshis > DUST + FEE) {
receivers.push(
{
......@@ -487,66 +487,98 @@ export default {
return null
}
const spendable = Object.keys(coins).filter(coinId => {
return coins[coinId].status === 'active'
const spendable = Object.keys(coins).filter(coinid => {
return coins[coinid].status === 'active'
})
console.log('SPENDABLE', spendable)
let isPledged = false
const locked = Object.keys(coins).filter(coinid => {
return coins[coinid].status === 'locked'
})
console.log('LOCKED', locked)
/* Initialized locked flag. */
let isAlreadyLocked = false
/* Initialize source coin. */
let sourceCoin = null
/* Set donation amount. */
const donation = this.userPledge.donation.amount
console.log('DONATION', donation)
/* Loop through all locked. */
locked.forEach(coinid => {
if (coins[coinid].satoshis === donation) {
/* Set flag. */
isAlreadyLocked = true
/* Set source coin. */
sourceCoin = coins[coinid]
}
})
/* Loop through all spendables. */
spendable.forEach(coinId => {
if (coins[coinId].status === 'active' && coins[coinId].satoshis === donation) {
// FIXME FOR DEVELOPMENT ONLY
spendable.forEach(coinid => {
if (coins[coinid].satoshis === donation) {
/* Set flag. */
isPledged = true
isAlreadyLocked = true
/* Set source coin. */
sourceCoin = coins[coinId]
sourceCoin = coins[coinid]
}
})
/* Validate pledge balance. */
if (!isPledged) {
/* Validate pledge availability. */
if (!isAlreadyLocked) {
console.error('MAKE THE DEPOSIT')
/* Set source coin. */
sourceCoin = coins[spendable[0]] // FIXME: Search for minimum sufficient coin(s).
console.log('SOURCE COIN', sourceCoin)
/* Set pledge address. */
const pledgeAddress = this.getAddress('causes')
console.log('PLEDGE ADDRESS', pledgeAddress)
/* Retrieve (address) balances. */
// const pledgeBalance = await Nito.Address.balance(pledgeAddress)
// console.log('PLEDGE BALANCE', pledgeBalance)
// TODO Sort spendables by value low->high.
this.loadFunds(sourceCoin, pledgeAddress, donation)
/* Handle all spendable coins. */
for (let i = 0; i < spendable.length; i++) {
/* Set coin. */
const coin = coins[spendable[i]]
/* Validate coin value. */
if (coin.satoshis > donation + DUST + FEE) {
/* Set source coin. */
sourceCoin = coin // FIXME: Search for minimum sufficient coin(s).
console.log('SOURCE COIN', sourceCoin)
break
}
}
/* Validate source coin. */
if (sourceCoin) {
/* Load funds. */
this.loadFunds(sourceCoin, pledgeAddress, donation)
/* Make pledge. */
this.makePledge(sourceCoin)
} else {
throw new Error(`Could not find a source coin for [ ${donation} ]`)
}
} else {
console.error('RE-USE CURRENT DEPOSIT', sourceCoin)
/* Make pledge. */
this.makePledge(sourceCoin)
}
},
/**
* Make Pledge
*/
makePledge(_coin) {
/* Initialize HD node. */
// const hdNode = this.getHDNode
/* Initialize verification key. */
// const verificationKey = hdNode.deriveChild(_path)
const verificationKey = Nito.Purse.fromWIF(_coin.wif)
// console.log('verificationKey', verificationKey)
......
......@@ -103,21 +103,19 @@
</thead>
<tbody>
<tr v-for="coin of coinsTable" :key="coin.id">
<td>{{coin.status}}</td>
<td v-html="coin.status"></td>
<td v-html="coinLabelDisplay(coin)"></td>
<td class="text-center">{{coinValueDisplay(coin)}}</td>
<td class="actions text-right">
<span v-if="coin.flags">
<a href="javascript://">
View
</a>
<a href="javascript://">
View
</a>
<span v-if="!coin.flags.locked">
|
<a href="javascript://" @click="send(coin.details)">
Send
</a>
</span>
<span v-if="coin.flags.spendable">
|
<a href="javascript://" @click="send(coin.details)">
Send
</a>
<span v-if="coin.flags.shuffled">
|
......@@ -125,30 +123,20 @@
Re-shuffle
</a>
</span>
<span v-if="coin.flags.locked">
<span v-else>
|
<a href="javascript://">
Unlock
Shuffle
</a>
</span>
</span>
<span v-else>
<a href="javascript://">
View
</a>
|
<a href="javascript://" @click="send(coin.details)">
Send
</a>
<span v-if="coin.flags.locked">
|
<a href="javascript://">
Shuffle
<a href="javascript://" @click="unlock(coin.id)">
Unlock
</a>
</span>
</td>
</tr>
</tbody>
......@@ -281,32 +269,25 @@ export default {
/* Set label. */
const label = `${coin.txid.slice(0, 8)} ... ${coin.txid.slice(-8)} : ${coin.vout}`
/* Retrieve metadata. */
const meta = this.getMeta
console.log('WALLET (meta):', meta)
/* Initialize flags. */
let flags = null
if (meta.coins[id]) {
if (meta.coins[id].lock && meta.coins[id].lock.isActive === true) {
/* Validate flags. */
if (flags === null) {
/* Set flag. */
flags = {
locked: true
}
} else {
/* Set flag. */
flags.locked = true
}
}
}
const flags = {}
/* Set status. */
// TODO: Will probably develop a rating scale??
const status = coin.status === 'active' ? '' : ''
let status = null
switch(coin.status) {
case 'active':
status = '<icon class="fa fa-check"></icon>'
flags.spendable = true
break
case 'locked':
status = '<icon class="fa fa-lock text-danger"></icon>'
flags.locked = true
break
default:
status = ''
}
/* Set satoshis. */
const satoshis = coin.satoshis
......@@ -322,7 +303,7 @@ export default {
}
// TODO: Allow display of spent coins.
if (status === '') {
if (status !== '') {
tableData.push(coinData)
}
......@@ -373,7 +354,9 @@ export default {
]),
...mapActions('wallet', [
'updateCoin',
'updateCoins',
'updateMeta',
]),
/**
......@@ -408,12 +391,8 @@ export default {
* Coin Label Display
*/
coinLabelDisplay(_coin) {
if (_coin.flags) {
if (_coin.flags.locked) {
return `${_coin.label} <small class="text-danger">LOCKED</small>`
} else {
return `${_coin.label} (${JSON.stringify(_coin.flags)})`
}
if (_coin.flags.locked) {
return `${_coin.label} <small class="text-danger">LOCKED</small>`
} else {
return _coin.label
}
......@@ -564,7 +543,7 @@ export default {
document.body.removeChild(textArea)
/* Set message. */
const message = `Deposit address has been copied to your clipboard.`
const message = `Deposit address copied to your clipboard.`
/* Display notification. */
this.toast(['Hey!', message, 'info'])
......@@ -578,6 +557,33 @@ export default {
}
},
/**
* Unlock
*/
unlock(_coinid) {
/* Request metadata. */
const meta = this.getMeta
/* Initialize coins. */
const coins = this.getWallet.coins
const coin = coins[_coinid]
/* Update flag. */
coin.status = 'active'
/* Update coin. */
this.updateCoin(coin)
// console.log('COIN', coin);
/* Remove coin info from metadata. */
delete meta.coins[_coinid]
/* Update metadata. */
this.updateMeta(meta)
// console.log('META', meta)
},
},
created: function () {
console.log('WALLET', this.getWallet)
......@@ -606,7 +612,7 @@ export default {
<style>
.my-available-coins small {
font-size: 0.8em;
font-size: 0.7em;
font-weight: 500;
}
</style>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment