ソースを参照

Fix "main" chain handling.

Shomari 1 ヶ月 前
コミット
557507b137

+ 1 - 1
web/components/Wallet/Welcome.vue

@@ -78,7 +78,7 @@ const init = () => {
 
     for (let i = 0; i < 20; i++) {
         // HUSH == 0x48555348 == 1,213,551,432
-        // address = await Wallet.getBchAddress(HUSH_PROTOCOL_ID, 0, i)
+        // address = Wallet.getBchAddress(HUSH_PROTOCOL_ID, 0, i)
         address = Wallet.keychain[HUSH_PROTOCOL_ID][i].address
         // console.log('GET BCH ADDRESS', i, address)
         hushAddresses.value.push(address)

+ 11 - 11
web/handlers/buildSharedTx.ts

@@ -25,7 +25,7 @@ console.log('BUILD SHARED TX', _sessionid, _inputs, _outputs)
     let childNode
     let data
     let ecPair
-    let ownedInputs
+    // let ownedInputs
     let protocolId
     let msg
     let rawTx
@@ -51,30 +51,30 @@ console.log('DO WE HAVE FUSION INPUTS??', this.fusionInputs)
     console.log('OUR ADDRESSES', ourAddresses)
 
     /* Initialize address index. */
-    addressIdx = 0
+    // addressIdx = 0
 
     /* Initialize owned inputs. */
-    ownedInputs = []
+    // ownedInputs = []
 
     /* Handle inputs. */
     _inputs.forEach(_input => {
         /* Add input. */
         transactionBuilder.addInput(_input.tx_hash, _input.tx_pos)
 
-console.log('VALIDATING (SELF) INPUT', _input)
-        /* Validate (our) address. */
-        if (ourAddresses.includes(_input.address)) {
-            ownedInputs.push(addressIdx)
-        }
-        addressIdx++
+// console.log('VALIDATING (SELF) INPUT', _input)
+//         /* Validate (our) address. */
+//         if (ourAddresses.includes(_input.address)) {
+//             ownedInputs.push(addressIdx)
+//         }
+//         addressIdx++
     })
-    console.log('OUR INPUTS INDEX', ownedInputs)
+    // console.log('OUR INPUTS INDEX', ownedInputs)
 
     /* Set protocol ID. */
     protocolId = '1337'
 
     /* Set protocol message. */
-    msg = 'mvp!'
+    msg = 'FINAL!'
 
     script = [
         utf8ToBin(protocolId),

+ 41 - 31
web/handlers/signSharedTx.ts

@@ -17,19 +17,19 @@ const HUSH_PROTOCOL_ID = 0x48555348
  *
  * Combine all participating inputs and outputs into one (signed) transaction.
  */
-export default function (_sessionid, _mnemonic, _inputs, _outputs) {
+export default function (_sessionid, _inputs, _outputs) {
 console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
     /* Initialize locals. */
-    let accountIdx
-    let addressIdx
-    let changeIdx
-    let childNode
+    // let accountIdx
+    // let addressIdx
+    // let changeIdx
+    // let childNode
     let data
     let ecPair
-    let ownedInputs
+    // let ownedInputs
     let protocolId
     let msg
-    let rawTx
+    // let rawTx
     let redeemScript
     let script
     let wif
@@ -52,30 +52,30 @@ console.log('DO WE HAVE FUSION INPUTS??', this.fusionInputs)
     console.log('OUR ADDRESSES', ourAddresses)
 
     /* Initialize address index. */
-    addressIdx = 0
+    // addressIdx = 0
 
     /* Initialize owned inputs. */
-    ownedInputs = []
+    // ownedInputs = []
 
     /* Handle inputs. */
     _inputs.forEach(_input => {
         /* Add input. */
         transactionBuilder.addInput(_input.tx_hash, _input.tx_pos)
 
-console.log('VALIDATING (SELF) INPUT', _input)
-        /* Validate (our) address. */
-        if (ourAddresses.includes(_input.address)) {
-            ownedInputs.push(addressIdx)
-        }
-        addressIdx++
+// console.log('VALIDATING (SELF) INPUT', _input)
+//         /* Validate (our) address. */
+//         if (ourAddresses.includes(_input.address)) {
+//             ownedInputs.push(addressIdx)
+//         }
+//         addressIdx++
     })
-    console.log('OUR INPUTS INDEX', ownedInputs)
+    // console.log('OUR INPUTS INDEX', ownedInputs)
 
     /* Set protocol ID. */
     protocolId = '1337'
 
     /* Set protocol message. */
-    msg = 'mvp!'
+    msg = 'FINAL!'
 
     script = [
         utf8ToBin(protocolId),
@@ -100,51 +100,61 @@ console.log('VALIDATING (SELF) INPUT', _input)
     })
 
     /* Convert mnemonic to seed. */
-    const seed = mnemonicToSeed(_mnemonic)
+    // const seed = mnemonicToSeed(_mnemonic)
 
     /* Conver to seed buffer. */
     // FIXME Migrate to TypedArrays.
-    const seedBuffer = Buffer.from(seed, 'hex')
+    // const seedBuffer = Buffer.from(seed, 'hex')
 
     /* Generate master node. */
-    const masterNode = bchjs.HDNode.fromSeed(seedBuffer)
+    // const masterNode = bchjs.HDNode.fromSeed(seedBuffer)
 
 // FIXME Identify our owned inputs.
-ownedInputs = [ 0, 1, 2, 3, 4, 5, 6 ]
+// ownedInputs = [ 0, 1, 2, 3, 4, 5, 6 ]
 
     for (let i = 0; i < _inputs.length; i++) {
+        const input = _inputs[i]
+        console.log('AN INPUT---', input)
+
+        const address = input.address
+        console.log('INPUT ADDR', address)
         /* Verify input ownership. */
-        if (!ownedInputs.includes(i)) {
+        // if (!ownedInputs.includes(i)) {
+        // if (typeof input.wif === 'undefined' || input.wif === null) {
+        if (!ourAddresses.includes(address)) {
             continue
         }
 
         /* Set account index. */
-        accountIdx = 0
+        // accountIdx = 0
         // accountIdx = HUSH_PROTOCOL_ID
         /* Set change index. */
-        changeIdx = 0
+        // changeIdx = 0
         /* Set address index. */
-        addressIdx = _inputs[i].address_idx
+        // addressIdx = _inputs[i].address_idx
         // console.log('ADDRESS IDX')
 
         /* Generate child node. */
-        childNode = masterNode
-            .derivePath(`m/44'/145'/${accountIdx}'/${changeIdx}/${addressIdx}`)
+        // 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)
+        // wif = bchjs.HDNode.toWIF(childNode)
+
+        /* Set WIF. */
+        wif = this.getWifForAddress(address)
+        console.log('BCH WIF', wif)
 
         /* Generate elliptic pair. */
         ecPair = bchjs.ECPair.fromWIF(wif)
-console.log('PARAMS', i, redeemScript, _inputs[i].value)
+console.log('SIGNING!', i, redeemScript, input.value)
         /* Sign (our own) input. */
         transactionBuilder.sign(
             i,
             ecPair,
             redeemScript,
             Transaction.SIGHASH_ALL,
-            _inputs[i].value,
+            input.value,
         )
     }
 

+ 23 - 25
web/pages/sponsors/[...pageid].vue

@@ -21,6 +21,7 @@ console.log('CAMPAIGN ID', campaignid)
 
 const campaigns = ref(null)
 const campaignTitle = ref(null)
+const donationAddress = ref(null)
 const networkTitle = ref(null)
 
 const init = () => {
@@ -62,9 +63,10 @@ const init = () => {
     /* Initialize Gurus handler. */
     campaigns.value['gurus'] = {}
 
-    /* Initialize Gurus (Bitcoin) handler. */
+    /* Initialize Seekers (Bitcoin) handler. */
     campaigns.value['seekers']['btc'] = {
         address: 'bitcoin:3Gwb2zrg64REcnDFdGHeAUMuibfpk932Kr',
+        round: 1,
         goal: 0,
         donated: 0,
         hasAirdrop: true,
@@ -72,9 +74,10 @@ const init = () => {
         updatedAt: 0,
     }
 
-    /* Initialize Gurus (Bitcoin Cash) handler. */
+    /* Initialize Seekers (Bitcoin Cash) handler. */
     campaigns.value['seekers']['bch'] = {
         address: 'bitcoincash:qrqdff68n8wm757kvfumylxs47tud3fuvqz3h6depv',
+        round: 1,
         goal: 0,
         donated: 0,
         hasAirdrop: true,
@@ -82,9 +85,10 @@ const init = () => {
         updatedAt: 0,
     }
 
-    /* Initialize Gurus (Nexa) handler. */
+    /* Initialize Seekers (Nexa) handler. */
     campaigns.value['seekers']['nexa'] = {
         address: 'nexa:nqtsq5g5fxezfwrhc323dm9npzy77lay7p26x903hkk4u8zu',
+        round: 1,
         goal: 0,
         donated: 0,
         hasAirdrop: true,
@@ -94,6 +98,7 @@ const init = () => {
 
     campaigns.value['masters']['btc'] = {
         address: 'bitcoin:3EQELPmJREqt3gVyELGYFnzLxV6Lnthudk',
+        round: 1,
         goal: 0,
         donated: 0,
         hasAirdrop: true,
@@ -105,6 +110,7 @@ const init = () => {
 
     campaigns.value['gurus']['btc'] = {
         address: 'bitcoin:34XzM3e3Tsf4wLGGcFCPRyoTP1NF6HF53z',
+        round: 1,
         goal: 0,
         donated: 0,
         hasAirdrop: true,
@@ -113,6 +119,10 @@ const init = () => {
     }
     campaigns.value['gurus']['bch'] = {}
     campaigns.value['gurus']['nexa'] = {}
+
+    /* Set donation address. */
+    donationAddress.value = campaigns.value[campaignid][networkid].address
+
 }
 
 onMounted(() => {
@@ -144,6 +154,10 @@ onMounted(() => {
                 Rewards tokens, ie $HUSH, will be sent directly to Sponsors point of delivery.
                 In order to GUARANTEE receipt of ALL rewards, please send from a Wallet/Address that you HOLD THE KEYS.
             </p>
+
+            <div>
+                0 of 27 campaigns completed for $HUSH airdrop!
+            </div>
         </section>
 
         <section>
@@ -162,33 +176,17 @@ onMounted(() => {
             Lorem, ipsum dolor sit amet consectetur adipisicing elit. Id eius voluptatem minus natus at eveniet dolorum eos mollitia, maxime animi excepturi harum omnis illum odit recusandae pariatur! Unde, explicabo molestias.
         </p>
 
-        <section v-if="campaigns" class="flex flex-col gap-3">
+        <NuxtLink :to="donationAddress" v-if="campaigns" class="p-5 flex flex-col gap-3 bg-sky-100 border-4 border-sky-300 rounded-3xl shadow">
             <h3 class="text-gray-500 text-3xl font-light tracking-widest">
-                Bitcoin
+                {{networkTitle}}
             </h3>
 
             <h3 class="font-medium tracking-tight">
-                {{campaigns.seekers.btc.address}}
-            </h3>
-
-            <h3>
-                Bitcoin Cash
-            </h3>
-
-            <h3>
-                {{campaigns.seekers.bch.address}}
+                {{donationAddress}}
             </h3>
+        </NuxtLink>
 
-            <h3>
-                Nexa
-            </h3>
-
-            <h3>
-                {{campaigns.seekers.nexa.address}}
-            </h3>
-        </section>
-
-        <section>
+        <section v-if="campaigns">
             <h2>
                 Privacy Masters
             </h2>
@@ -218,7 +216,7 @@ onMounted(() => {
             </h3>
         </section>
 
-        <section>
+        <section v-if="campaigns">
             <h2>
                 Privacy Gurus
             </h2>

+ 19 - 10
web/stores/wallet.ts

@@ -4,7 +4,10 @@ import moment from 'moment'
 
 import BCHJS from '@psf/bch-js'
 import { sha256 } from '@nexajs/crypto'
-import { mnemonicToEntropy } from '@nexajs/hdnode'
+import {
+    mnemonicToEntropy,
+    mnemonicToSeed,
+} from '@nexajs/hdnode'
 import { Wallet } from '@nexajs/wallet'
 
 import _broadcast from './wallet/broadcast.ts'
@@ -271,17 +274,17 @@ _setupHushKeychain.bind(this)()
          * Will return the "child" address of a master node,
          * based on the account index, change flag and address index.
          */
-        async getBchAddress(
+        getBchAddress(
             _accountIdx = 0,
             _isChange = 0, // NOTE: 0 = false, 1 = true
             _addressIdx = 0,
         ) {
             /* Set root seed. */
-            const rootSeed = await bchjs.Mnemonic.toSeed(this.mnemonic)
+            const rootSeed = mnemonicToSeed(this.mnemonic)
             // console.log('rootSeed', rootSeed)
 
             /* Set HD master node. */
-            const masterHdnode = bchjs.HDNode.fromSeed(rootSeed)
+            const masterHdnode = bchjs.HDNode.fromSeed(Buffer.from(rootSeed, 'hex') )
             // console.log('masterHdnode', masterHdnode);
 
             /* Set child node. */
@@ -391,16 +394,13 @@ _setupHushKeychain.bind(this)()
                 let bchAddress2
                 let bchAddress3
 
-                bchAddress1 = await this.getBchAddress(0, 0, 0)
-                    .catch(err => console.error(err))
+                bchAddress1 = this.getBchAddress(0, 0, 0)
                 // console.log('BCH ADDRESS-1', bchAddress1)
 
-                bchAddress2 = await this.getBchAddress(0, 0, 1)
-                    .catch(err => console.error(err))
+                bchAddress2 = this.getBchAddress(0, 0, 1)
                 // console.log('BCH ADDRESS-2', bchAddress2)
 
-                bchAddress3 = await this.getBchAddress(0, 0, 2)
-                    .catch(err => console.error(err))
+                bchAddress3 = this.getBchAddress(0, 0, 2)
                 // console.log('BCH ADDRESS-3', bchAddress3)
 
                 data = await $fetch('/api/electrum', {
@@ -474,6 +474,15 @@ _setupHushKeychain.bind(this)()
             }
         },
 
+        /**
+         * Get WIF for Address
+         *
+         * TBD..
+         */
+        getWifForAddress(_address) {
+            return _getWifForAddress.bind(this)(_address)
+        },
+
         async startFusion() {
             /* Start fusions. */
             return _startFusion.bind(this)()

+ 26 - 14
web/stores/wallet/completeFusion.ts

@@ -26,14 +26,14 @@ export default async function () {
 
     /* Set inputs. */
     inputs = session.inputs
-    // console.log('INPUTS', inputs)
+    console.log('INPUTS', inputs)
 
     /* Initialize keys. */
     keys = []
 
     /* Handle (input) keys. */
-    Object.keys(inputs).forEach(_inputid => {
-        keys.push(_inputid)
+    Object.keys(inputs).forEach(_outpoint => {
+        keys.push(_outpoint)
     })
     // console.log('KEYS', keys)
 
@@ -51,13 +51,13 @@ export default async function () {
         console.log('HANDLE INPUT', input)
 
         /* Find address index for input. */
-        for (let i = 0; i < this.fusionInputs.length; i++) {
-            if (this.fusionInputs[i].address === input.address) {
-                input.address_idx = i
-                console.log('ADDRESS INDEX', i)
-                break
-            }
-        }
+        // for (let i = 0; i < this.fusionInputs.length; i++) {
+        //     if (this.fusionInputs[i].address === input.address) {
+        //         input.address_idx = i
+        //         console.log('ADDRESS INDEX', i)
+        //         break
+        //     }
+        // }
 
         /* Add input. */
         sortedInputs.push(input)
@@ -95,9 +95,6 @@ export default async function () {
     /* Sign shared transaction. */
     const transactionBuilder = buildSharedTx.bind(this)(
         sessionid, sortedInputs, sortedOutputs)
-
-const script = Buffer.from(hexToBin(inputs['185ad6a10ea70d977d943a910f54dc446163a16771017c6df35c7893c1db0c35'].unlocking))
-console.log('SCRIPT', script)
 // transactionBuilder.transaction.tx.ins[0].script = script
     console.log('FINALIZED TRANSACTION', transactionBuilder)
 
@@ -107,7 +104,22 @@ console.log('SCRIPT', script)
     const transaction = transactionBuilder.transaction.buildIncomplete()
     // const transaction = transactionBuilder.transaction
     console.log('TRANSACTION', transaction)
-    transaction.ins[0].script = script
+
+const scripts = []
+Object.keys(inputs).forEach(_outpoint => {
+    const input = inputs[_outpoint]
+    console.log('SCRIPT INPUT', input)
+
+    scripts.push({
+        id: _outpoint,
+        script: Buffer.from(input.unlocking, 'hex')
+    })
+})
+console.log('SCRIPTS', scripts)
+// const script = Buffer.from(hexToBin(inputs['185ad6a10ea70d977d943a910f54dc446163a16771017c6df35c7893c1db0c35'].unlocking))
+// console.log('SCRIPT', script)
+    // transaction.ins[0].script = script
+    transaction.ins[0].script = scripts[0].script
     console.log('TRANSACTION (hex)', transaction.toHex())
 
     // transaction.ins[0].script = Buffer.from(hexToBin(inputs['185ad6a10ea70d977d943a910f54dc446163a16771017c6df35c7893c1db0c35'].unlocking))

+ 32 - 16
web/stores/wallet/getWifForAddress.ts

@@ -20,24 +20,40 @@ export default function (_address) {
     let addressIdx
     let changeIdx
     let childNode
+    let mainAddress
     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 main (wallet) address. */
+    mainAddress = this.getBchAddress(0, 0, 0)
+
+    if (mainAddress === _address) {
+        /* Set account index. */
+        accountIdx = 0 // NOTE: This is the Main chain
+
+        /* Set address index. */
+        addressIdx = 0
+    } else {
+        /* Set account index. */
+        accountIdx = HUSH_PROTOCOL_ID // NOTE: This is the Hush chain
+
+        /* 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('ACCOUNT IDX', accountIdx)
+    // console.log('ADDRESS IDX', addressIdx)
+
+    if (typeof addressIdx === 'undefined' || addressIdx === null) {
+        throw new Error(`Oops! There is NO private key for [ ${_address} ]`)
+    }
 
     /* Set change index. */
     changeIdx = 0

+ 1 - 1
web/stores/wallet/setupHushKeychain.ts

@@ -18,7 +18,7 @@ export default async function () {
         /* Set address index. */
         addressIdx = i
 
-        address = await this.getBchAddress(HUSH_PROTOCOL_ID, CHANGE_IDX, addressIdx)
+        address = this.getBchAddress(HUSH_PROTOCOL_ID, CHANGE_IDX, addressIdx)
 
         pkg = {
             address,

+ 1 - 1
web/stores/wallet/setupKeychain.ts

@@ -18,7 +18,7 @@ export default async function () {
         /* Set address index. */
         addressIdx = i
 
-        address = await this.getBchAddress(PROTOCOL_ID, CHANGE_IDX, addressIdx)
+        address = this.getBchAddress(PROTOCOL_ID, CHANGE_IDX, addressIdx)
 
         pkg = {
             address,

+ 17 - 17
web/stores/wallet/signFusion.ts

@@ -54,22 +54,22 @@ export default async function () {
         const input = inputs[_keyid]
         console.log('HANDLE INPUT', input)
 
-        let addressIdx = 0
-
-        /* Find address index for input. */
-        Object.keys(this.fusionInputs).forEach(_outpoint => {
-            /* Set fusion input. */
-            const fusionInput = this.fusionInputs[_outpoint]
-            console.log('FUSION INPUT', fusionInput)
-
-            if (fusionInput.address === input.address) {
-                input.address_idx = addressIdx
-                console.log('ADDED ADDRESS INDEX', addressIdx)
-                // break
-            }
+        // let addressIdx = 0
+
+        // /* Find address index for input. */
+        // Object.keys(this.fusionInputs).forEach(_outpoint => {
+        //     /* Set fusion input. */
+        //     const fusionInput = this.fusionInputs[_outpoint]
+        //     console.log('FUSION INPUT', fusionInput)
+
+        //     if (fusionInput.address === input.address) {
+        //         input.address_idx = addressIdx
+        //         console.log('ADDED ADDRESS INDEX', addressIdx)
+        //         // break
+        //     }
 
-            addressIdx++
-        })
+        //     addressIdx++
+        // })
 
         /* Add input. */
         sortedInputs.push(input)
@@ -106,8 +106,8 @@ export default async function () {
 
     /* Sign shared transaction. */
     transaction = signSharedTx.bind(this)(
-        sessionid, this.mnemonic, sortedInputs, sortedOutputs)
-    console.log('SIGNED TRANSACTION', transaction)
+        sessionid, sortedInputs, sortedOutputs)
+    console.log('(partially) SIGNED TRANSACTION', transaction)
     // txObj.ins[1].script = txObj2.ins[1].script
 
     /* Initailize unlocked. */

+ 9 - 1
web/stores/wallet/startFusion.ts

@@ -112,9 +112,17 @@ export default async function () {
 // return
 
     /* Initialize components. */
-    // NOTE: Automatically add ALL fusion inputs.
+    // NOTE: Automatically clone + add ALL fusion inputs.
     components = [ ...fusionInputs ]
 
+    /* Sanitize (cloned input) components. */
+    Object.keys(components).forEach(_outpoint => {
+        const component = components[_outpoint]
+
+        /* Delete WIF. */
+        delete component.wif
+    })
+
     /* Set tier ID. */
     tierid = bestTiers.tierid