ソースを参照

Fix wallet creation issues?

Shomari 1 ヶ月 前
コミット
240622d9b8
3 ファイル変更84 行追加38 行削除
  1. 22 5
      web/components/Wallet/Welcome.vue
  2. 4 10
      web/handlers/signSharedTx.ts
  3. 58 23
      web/stores/wallet.ts

+ 22 - 5
web/components/Wallet/Welcome.vue

@@ -27,7 +27,13 @@ const balance = computed(() => {
         return 0
     }
 
-    const totalValue = Wallet.fusionInputs.reduce(
+    const inputs = []
+
+    Object.keys(Wallet.fusionInputs).forEach(_outpoint => {
+        inputs.push(Wallet.fusionInputs[_outpoint])
+    })
+
+    const totalValue = inputs.reduce(
         (acc, utxo) => acc + utxo.value, 0
     )
     // console.log('TOTAL VALUE', totalValue)
@@ -55,6 +61,14 @@ const init = () => {
     /* Initialize locals. */
     let address
 
+    if (typeof props.cashAddress === 'undefined' || !props.cashAddress || props.cashAddress === '') {
+        console.log('RELOAD PAGE FOR CASH ADDRESS!!')
+        const router = useRouter()
+        // router.push({ path: "/admin/liquidity?setup-complete" })
+        router.push({ path: "/admin" })
+        return
+    }
+
     /* Initialize #? Hush addresses. */
     hushAddresses.value = []
 
@@ -66,11 +80,14 @@ const init = () => {
         hushAddresses.value.push(address)
     }
 
+
 }
 
 onMounted(() => {
     // console.log('KEYCHAIN', Wallet.keychain)
     // init()
+
+    // FIXME Race conditions with loading time and reloading...
     setTimeout(init, 1000)
 })
 
@@ -97,7 +114,7 @@ onMounted(() => {
                         Bitcoin Cash Address
                     </h2>
 
-                    <NuxtLink :to="'https://3xpl.com/bitcoin-cash/address/' + props.cashAddress.slice(12)" target="_blank" class="col-span-2 text-base sm:text-xl text-blue-500 truncate hover:underline">
+                    <NuxtLink v-if="props.cashAddress" :to="'https://explorer.melroy.org/address/' + props.cashAddress.slice(12)" target="_blank" class="col-span-2 text-base sm:text-xl text-blue-500 truncate hover:underline">
                         {{props.cashAddress.slice(12)}}
                     </NuxtLink>
 
@@ -105,7 +122,7 @@ onMounted(() => {
                         Current Balance
                     </h3>
 
-                    <h3 class="text-green-900 text-xl font-medium">
+                    <h3 v-if="balance" class="text-green-900 text-xl font-medium">
                         {{balance}}
                         <small class="text-sm">sats</small>
                     </h3>
@@ -114,7 +131,7 @@ onMounted(() => {
                 <div v-for="(address, index) of hushAddresses" :key="address" class="text-xs">
                     <div class="grid grid-cols-4 items-center gap-3">
                         <span class="text-right">Hush #{{(index + 1)}}</span>
-                        <NuxtLink :to="'https://3xpl.com/bitcoin-cash/address/' + address.slice(12)" target="_blank" class="col-span-3 text-blue-500 hover:underline">{{address}}</NuxtLink>
+                        <NuxtLink :to="'https://explorer.melroy.org/address/' + address.slice(12)" target="_blank" class="col-span-3 text-blue-500 hover:underline">{{address}}</NuxtLink>
                     </div>
                 </div>
             </div>
@@ -141,7 +158,7 @@ onMounted(() => {
                 <!-- <div v-for="(address, index) of hushAddresses" :key="address" class="text-xs">
                     <div class="grid grid-cols-4 items-center gap-3">
                         <span class="text-right">Hush #{{(index + 1)}}</span>
-                        <NuxtLink :to="'https://3xpl.com/bitcoin-cash/address/' + address.slice(12)" target="_blank" class="col-span-3 text-blue-500 hover:underline">{{address}}</NuxtLink>
+                        <NuxtLink :to="'https://explorer.melroy.org/address/' + address.slice(12)" target="_blank" class="col-span-3 text-blue-500 hover:underline">{{address}}</NuxtLink>
                     </div>
                 </div> -->
             </div>

+ 4 - 10
web/handlers/signSharedTx.ts

@@ -17,7 +17,7 @@ const HUSH_PROTOCOL_ID = 0x48555348
  * Combine all participating inputs and outputs into one (signed) transaction.
  */
 export default function (_sessionid, _mnemonic, _inputs, _outputs) {
-console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
+// console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
     /* Initialize locals. */
     let accountIdx
     let addressIdx
@@ -25,7 +25,6 @@ console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
     let childNode
     let data
     let ecPair
-    let hushIndexes
     let ownedInputs
     let protocolId
     let msg
@@ -47,7 +46,7 @@ console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
     protocolId = '1337'
 
     /* Set protocol message. */
-    msg = 'finalization...'
+    msg = 're-finalization...'
 
     script = [
         utf8ToBin(protocolId),
@@ -85,10 +84,7 @@ console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
 
 // FIXME Identify our owned inputs.
 ownedInputs = [ 0, 1, 2, 3, 4, 5, 6 ]
-// hushIndexes = [ 0, 1, 2, 3, 4, 5, 6 ]
-// 0 - L58m5XW3cYG84ZN5Cbqqap7Mvx1TihNNMPSq7Vw9W7t3oHeuS36q
-// 1 - L2qsjy4xprtF2hocseepEtKM3V6y5hHjvkX5K7zedeBjmd6forbb
-// 2 - L4FmiWogy4THbosEUHbfLgxgTQpZ1e4dFDrAoik2fvoQ9TNKCsRe
+
     for (let i = 0; i < _inputs.length; i++) {
         /* Verify input ownership. */
         if (!ownedInputs.includes(i)) {
@@ -101,10 +97,8 @@ ownedInputs = [ 0, 1, 2, 3, 4, 5, 6 ]
         /* Set change index. */
         changeIdx = 0
         /* Set address index. */
-        // addressIdx = 0
-        // addressIdx = hushIndexes[i]
         addressIdx = _inputs[i].address_idx
-        console.log('ADDRESS IDX')
+        // console.log('ADDRESS IDX')
 
         /* Generate child node. */
         childNode = masterNode

+ 58 - 23
web/stores/wallet.ts

@@ -3,6 +3,7 @@ import { defineStore } from 'pinia'
 import moment from 'moment'
 
 import BCHJS from '@psf/bch-js'
+import { sha256 } from '@nexajs/crypto'
 import { mnemonicToEntropy } from '@nexajs/hdnode'
 import { Wallet } from '@nexajs/wallet'
 
@@ -142,32 +143,46 @@ export const useWalletStore = defineStore('wallet', {
                 return null
             }
 
-            const collection = _state._utxos
-            // console.log('COLLECTION (fusionInputs)', collection)
+            // const collection = _state._utxos
+            console.log('STATE (utxos)', _state._utxos)
 
-            const mainList = []
+            /* Initialize inputs (collection). */
+            let inputs = {}
 
-            collection[0]?.forEach(_account => {
-                // console.log('ACCOUNT (0)', _account)
-                _account.utxos.forEach(_utxo => {
-                    mainList.push({
-                        address: _account.address,
-                        ..._utxo,
-                    })
-                })
-            })
+            /* Verify Main chain. */
+            if (_state._utxos[0]) {
+                inputs = { ...inputs, ..._state._utxos[0] }
+            }
 
-            collection[HUSH_PROTOCOL_ID]?.forEach(_account => {
-                // console.log('ACCOUNT (1213551432)', _account)
-                _account.utxos.forEach(_utxo => {
-                    mainList.push({
-                        address: _account.address,
-                        ..._utxo,
-                    })
-                })
-            })
+            /* Verify Hush chain. */
+            if (_state._utxos[HUSH_PROTOCOL_ID]) {
+                inputs = { ...inputs, ..._state._utxos[HUSH_PROTOCOL_ID] }
+            }
 
-            return mainList
+            console.log('FUSION (inputs)', inputs)
+
+            // collection[0]?.forEach(_utxo => {
+            //     // console.log('ACCOUNT (0)', _account)
+            //     _account.utxos.forEach(_utxo => {
+            //         mainList.push({
+            //             address: _account.address,
+            //             ..._utxo,
+            //         })
+            //     })
+            // })
+
+            // collection[HUSH_PROTOCOL_ID]?.forEach(_account => {
+            //     // console.log('ACCOUNT (1213551432)', _account)
+            //     _account.utxos.forEach(_utxo => {
+            //         mainList.push({
+            //             address: _account.address,
+            //             ..._utxo,
+            //         })
+            //     })
+            // })
+
+            /* Return inputs. */
+            return inputs
         },
 
         fusionAddrs() {
@@ -402,8 +417,28 @@ _setupHushKeychain.bind(this)()
                 })
                 console.log('MAIN WALLET DATA', data)
 
+                // if (!this._utxos[0]) {
+                    this._utxos[0] = {}
+                // }
+
+                /* Handle unspent outputs. */
+                data.forEach(_unspent => {
+                    _unspent.utxos.forEach(_utxo => {
+                        console.log('ADDING UTXO...', _utxo)
+
+                        /* Generate outpoint (hash). */
+                        const outpoint = sha256(_utxo.tx_hash + ':' + _utxo.tx_pos)
+
+                        /* Add to UTXOs. */
+                        this._utxos[0][outpoint] = {
+                            address: _unspent.address,
+                            ..._utxo,
+                        }
+                    })
+                })
+
                 // FIXME Update the delta ONLY!
-                this._utxos[0] = data?.utxos
+                // this._utxos[0] = data?.utxos
             }
 
             return true