Bladeren bron

Add WIF to fusion inputs.

Shomari 1 maand geleden
bovenliggende
commit
dc0d10861d
3 gewijzigde bestanden met toevoegingen van 70 en 22 verwijderingen
  1. 1 0
      web/handlers/signSharedTx.ts
  2. 4 22
      web/stores/wallet.ts
  3. 65 0
      web/stores/wallet/getWifForAddress.ts

+ 1 - 0
web/handlers/signSharedTx.ts

@@ -5,6 +5,7 @@ import { mnemonicToSeed } from '@nexajs/hdnode'
 import { encodeNullData } from '@nexajs/script'
 import { utf8ToBin } from '@nexajs/utils'
 
+/* Initialize BCHJS. */
 const bchjs = new BCHJS()
 
 /* Initialize constants. */

+ 4 - 22
web/stores/wallet.ts

@@ -9,6 +9,7 @@ import { Wallet } from '@nexajs/wallet'
 
 import _broadcast from './wallet/broadcast.ts'
 import _completeFusion from './wallet/completeFusion.ts'
+import _getWifForAddress from './wallet/getWifForAddress.ts'
 import _setEntropy from './wallet/setEntropy.ts'
 import _setupKeychain from './wallet/setupKeychain.ts'
 import _setupHushKeychain from './wallet/setupHushKeychain.ts'
@@ -159,29 +160,8 @@ export const useWalletStore = defineStore('wallet', {
             if (_state._utxos[HUSH_PROTOCOL_ID]) {
                 inputs = { ...inputs, ..._state._utxos[HUSH_PROTOCOL_ID] }
             }
-
             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
         },
@@ -357,7 +337,7 @@ _setupHushKeychain.bind(this)()
             /* Handle unspent outputs. */
             data.forEach(_unspent => {
                 _unspent.utxos.forEach(_utxo => {
-                    console.log('ADDING HUSH UTXO...', _utxo)
+                    // console.log('ADDING HUSH UTXO...', _utxo)
 
                     /* Generate outpoint (hash). */
                     const outpoint = sha256(_utxo.tx_hash + ':' + _utxo.tx_pos)
@@ -366,6 +346,7 @@ _setupHushKeychain.bind(this)()
                     this._utxos[HUSH_PROTOCOL_ID][outpoint] = {
                         address: _unspent.address,
                         ..._utxo,
+                        wif: _getWifForAddress.bind(this)(_unspent.address),
                     }
                 })
             })
@@ -451,6 +432,7 @@ _setupHushKeychain.bind(this)()
                         this._utxos[0][outpoint] = {
                             address: _unspent.address,
                             ..._utxo,
+                            wif: _getWifForAddress.bind(this)(_unspent.address),
                         }
                     })
                 })

+ 65 - 0
web/stores/wallet/getWifForAddress.ts

@@ -0,0 +1,65 @@
+/* Import modules. */
+import BCHJS from '@psf/bch-js'
+import { mnemonicToSeed } from '@nexajs/hdnode'
+
+/* Initialize BCHJS. */
+const bchjs = new BCHJS()
+
+/* Initialize constants. */
+const HUSH_PROTOCOL_ID = 0x48555348
+
+
+/**
+ * Get WIF For An Address
+ *
+ * Will return the wallet import format (WIF) for a specific address.
+ */
+export default function (_address) {
+    /* Initialize locals. */
+    let accountIdx
+    let addressIdx
+    let changeIdx
+    let childNode
+    let wif
+
+    /* Handle fusion addresses. */
+    Object.keys(this.fusionAddrs).forEach(_addressIdx => {
+        /* Set fusion address. */
+        const fusionAddress = this.fusionAddrs[_addressIdx]
+
+        /* Validate address. */
+        if (_address === fusionAddress.address) {
+            /* Set address index. */
+            addressIdx = _addressIdx
+        }
+    })
+    // console.log('ADDRESS IDX')
+
+    /* Set account index. */
+// FIXME Detect account type.
+    accountIdx = 0
+
+    /* Set change index. */
+    changeIdx = 0
+
+    /* Convert mnemonic to seed. */
+    const seed = mnemonicToSeed(this.mnemonic)
+
+    /* Conver to seed buffer. */
+    // FIXME Migrate to TypedArrays.
+    const seedBuffer = Buffer.from(seed, 'hex')
+
+    /* Generate master node. */
+    const masterNode = bchjs.HDNode.fromSeed(seedBuffer)
+
+    /* Generate child node. */
+    childNode = masterNode
+        .derivePath(`m/44'/145'/${accountIdx}'/${changeIdx}/${addressIdx}`)
+
+    /* Generate wallet import format (WIF). */
+    wif = bchjs.HDNode.toWIF(childNode)
+    // console.log('BCH WIF', i, wif)
+
+    /* Return WIF. */
+    return wif
+}