|
@@ -10,9 +10,11 @@ import { binToHex } from '@nexajs/utils'
|
|
|
import { Wallet } from '@nexajs/wallet'
|
|
|
|
|
|
import _broadcast from './wallet/broadcast.ts'
|
|
|
+import _completeFusion from './wallet/completeFusion.ts'
|
|
|
import _setEntropy from './wallet/setEntropy.ts'
|
|
|
import _setupKeychain from './wallet/setupKeychain.ts'
|
|
|
import _setupHushKeychain from './wallet/setupHushKeychain.ts'
|
|
|
+import _startFusions from './wallet/startFusions.ts'
|
|
|
|
|
|
/* Initialize constants. */
|
|
|
const HUSH_PROTOCOL_ID = 0x48555348
|
|
@@ -26,7 +28,9 @@ const bchjs = new BCHJS({
|
|
|
})
|
|
|
|
|
|
/* Set constants. */
|
|
|
-const UPDATE_UTXOS_INTERVAL = 8000 // Allows for ~7 (REST) requests per minute.
|
|
|
+// const UPDATE_UTXOS_INTERVAL = 8000 // Allows for ~7 (REST) requests per minute.
|
|
|
+const UPDATE_UTXOS_INTERVAL = 15000 // Allows for ~8 (Double REST) requests per minute.
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Wallet Store
|
|
@@ -142,6 +146,7 @@ export const useWalletStore = defineStore('wallet', {
|
|
|
}
|
|
|
|
|
|
const collection = _state._utxos
|
|
|
+ // console.log('COLLECTION', collection)
|
|
|
|
|
|
const mainList = []
|
|
|
|
|
@@ -237,7 +242,7 @@ _setupHushKeychain.bind(this)()
|
|
|
})
|
|
|
|
|
|
// FIXME ADDED FOR BITCOIN CASH SUPPORT
|
|
|
- setInterval(this.updateUtxos, UPDATE_UTXOS_INTERVAL)
|
|
|
+ // setInterval(this.updateUtxos, UPDATE_UTXOS_INTERVAL)
|
|
|
|
|
|
/* Update ALL chains. */
|
|
|
this.updateUtxos(true)
|
|
@@ -303,6 +308,7 @@ _setupHushKeychain.bind(this)()
|
|
|
let coins
|
|
|
let data
|
|
|
let hushAddresses
|
|
|
+ let usedAddresses
|
|
|
let utxos
|
|
|
let walletAddresses
|
|
|
|
|
@@ -319,15 +325,37 @@ _setupHushKeychain.bind(this)()
|
|
|
const coin = coins[_coinid]
|
|
|
return coin.address
|
|
|
})
|
|
|
- // console.log('SAVED ADDRESSES', hushAddresses)
|
|
|
+ // console.log('HUSH ADDRESSES', hushAddresses)
|
|
|
|
|
|
+ /* Request UTXO data. */
|
|
|
data = await bchjs.Electrumx.utxo(hushAddresses.slice(0, 20))
|
|
|
- // console.log('HUSH DATA', data)
|
|
|
- // const utxos = data.utxos
|
|
|
+ .catch(err => console.error(err))
|
|
|
+ console.log('HUSH UTXOS', data)
|
|
|
+
|
|
|
+ // FIXME Update the deltas ONLY!
|
|
|
+ // this._utxos[HUSH_PROTOCOL_ID] = data?.utxos
|
|
|
+
|
|
|
+ /* Request history data. */
|
|
|
+ // data = await bchjs.Electrumx.transactions(hushAddresses.slice(0, 5))
|
|
|
+ // .catch(err => console.error(err))
|
|
|
+ // console.log('HUSH TX HISTORY', data)
|
|
|
+
|
|
|
+ // usedAddresses = data?.utxos.map(_utxo => {
|
|
|
+ // if (_utxo.utxos.length > 0) {
|
|
|
+ // return _utxo.address
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // console.log('USED ADDRESSES', usedAddresses)
|
|
|
|
|
|
- // FIXME Update the delata ONLY!
|
|
|
- this._utxos[HUSH_PROTOCOL_ID] = data?.utxos
|
|
|
+ // Object.keys(coins).forEach(_coinid => {
|
|
|
+ // const coin = coins[_coinid]
|
|
|
|
|
|
+ // if (usedAddresses.includes(coin.address)) {
|
|
|
+ // coin.isUsed = true
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+
|
|
|
+ /* Validate chain handler flag. */
|
|
|
if (_allChains) {
|
|
|
let bchAddress1
|
|
|
let bchAddress2
|
|
@@ -390,134 +418,13 @@ _setupHushKeychain.bind(this)()
|
|
|
},
|
|
|
|
|
|
async startFusions() {
|
|
|
- console.log('Starting fusions...')
|
|
|
-
|
|
|
- /* Initialize locals. */
|
|
|
- let blindComponents
|
|
|
- let components
|
|
|
- let cipherTokens
|
|
|
- let fusionInputs
|
|
|
- let inputAmount
|
|
|
- let publicKey
|
|
|
- let rawTx
|
|
|
- let inputs
|
|
|
- let outputs
|
|
|
- let response
|
|
|
- let clubWallet
|
|
|
- let tierScale
|
|
|
-
|
|
|
- const feeOffset = 1034//10034
|
|
|
- const maxOutputCount = 17
|
|
|
-
|
|
|
- /* Calculate input amount. */
|
|
|
- // inputAmount = this.fusionInputs.reduce(
|
|
|
- // (acc, utxo) => (utxo.value > 10000) ? acc + utxo.value : 0, 0
|
|
|
- // )
|
|
|
- // console.log('INPUT AMOUNT', inputAmount)
|
|
|
-
|
|
|
- /* Clone fusion inputs. */
|
|
|
- fusionInputs = [ ...this.fusionInputs ]
|
|
|
-
|
|
|
- /* Add inputs to components. */
|
|
|
- components = [ ...fusionInputs ]
|
|
|
-
|
|
|
- const tierScales = [
|
|
|
- 10000, 12000, 15000, 18000, 22000, 27000, 33000, 39000, 47000, 56000, 68000, 82000,
|
|
|
- 100000, 120000, 150000, 180000, 220000, 270000, 330000, 390000, 470000, 560000, 680000, 820000,
|
|
|
- 1000000, 1200000, 1500000, 1800000, 2200000, 2700000, 3300000, 3900000, 4700000, 5600000, 6800000, 8200000,
|
|
|
- 10000000, 12000000, 15000000, 18000000, 22000000, 27000000, 33000000, 39000000, 47000000, 56000000, 68000000, 82000000,
|
|
|
- 100000000, 120000000, 150000000, 180000000, 220000000, 270000000, 330000000, 390000000, 470000000, 560000000, 680000000, 820000000,
|
|
|
- 1000000000, 1200000000, 1500000000, 1800000000, 2200000000, 2700000000, 3300000000, 3900000000, 4700000000, 5600000000, 6800000000, 8200000000,
|
|
|
- ]
|
|
|
-
|
|
|
- const bestTiers = {}
|
|
|
-
|
|
|
- for (let i = 0; i < fusionInputs.length; i++) {
|
|
|
- /* Set input amount. */
|
|
|
- inputAmount = fusionInputs[i].value
|
|
|
- console.log('INPUT AMOUNT', inputAmount)
|
|
|
-
|
|
|
- /* Handle ALL tier scales. */
|
|
|
- tierScales.forEach(_tierScale => {
|
|
|
- try {
|
|
|
- /* Request (random) outputs. */
|
|
|
- response = randomOutputsForTier(
|
|
|
- inputAmount,
|
|
|
- _tierScale,
|
|
|
- feeOffset,
|
|
|
- maxOutputCount,
|
|
|
- )
|
|
|
-
|
|
|
- /* Validate tier outputs. */
|
|
|
- if (response && response.length > 1) {
|
|
|
- console.log('TIER', _tierScale, 'INPUT #', i, response)
|
|
|
-
|
|
|
- /* Test for the best tiers. */
|
|
|
- if (typeof bestTiers[i] === 'undefined' || response.length > bestTiers[i]?.outputs.length) {
|
|
|
- const numOutputs = response.length
|
|
|
- // console.log('NUM OUTPUTS', numOutputs)
|
|
|
-
|
|
|
- // const fee = bchjs.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: numOutputs })
|
|
|
- const fee = bchjs.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 })
|
|
|
- // console.log('FEE', fee)
|
|
|
-
|
|
|
- const outputs = response.map(_outputValue => {
|
|
|
- return {
|
|
|
- address: this.getFusionAddress(),
|
|
|
- // value: (_outputValue - Math.ceil(fee / numOutputs)),
|
|
|
- value: (_outputValue - fee),
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- bestTiers[i] = {
|
|
|
- tierid: _tierScale,
|
|
|
- outputs,
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (err) {
|
|
|
- // console.error(err)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- console.log('BEST TIERS', bestTiers)
|
|
|
-// return
|
|
|
-
|
|
|
- /* Add best tiers to components. */
|
|
|
- Object.keys(bestTiers).forEach(_tierid => {
|
|
|
- const tier = bestTiers[_tierid]
|
|
|
-
|
|
|
- /* Add (output) tier. */
|
|
|
- components.push(tier)
|
|
|
- })
|
|
|
-
|
|
|
- /* Prepare components for encryption. */
|
|
|
- components = JSON.stringify(components)
|
|
|
- // console.log('FUSION (components)', components)
|
|
|
-
|
|
|
- // TODO Handle any filtering required BEFORE submitting for fusion.
|
|
|
+ /* Start fusions. */
|
|
|
+ return _startFusions.bind(this)()
|
|
|
+ },
|
|
|
|
|
|
- clubWallet = await $fetch('/api/wallet')
|
|
|
- .catch(err => console.error(err))
|
|
|
- // console.log('CLUB WALLET', clubWallet)
|
|
|
-
|
|
|
- // FIXME Retrieve public key from a "public" endpoint.
|
|
|
- publicKey = clubWallet.publicKey
|
|
|
- // console.log('CLUB PUBLIC KEY', publicKey)
|
|
|
-
|
|
|
- /* Generate blind components. */
|
|
|
- blindComponents = encryptForPubkey(publicKey, components)
|
|
|
- // console.log('BLINDED COMPONENTS', blindComponents)
|
|
|
-
|
|
|
- response = await $fetch('/v1', {
|
|
|
- method: 'POST',
|
|
|
- body: {
|
|
|
- authid: binToHex(this.wallet.publicKey),
|
|
|
- components: blindComponents,
|
|
|
- },
|
|
|
- })
|
|
|
- .catch(err => console.error(err))
|
|
|
- console.log('RESPONSE', response)
|
|
|
+ async completeFusion() {
|
|
|
+ /* Start fusions. */
|
|
|
+ return _completeFusion.bind(this)()
|
|
|
},
|
|
|
|
|
|
async transfer(_receiver, _satoshis) {
|