Explorar el Código

Fix fee (bug?)

Shomari hace 1 mes
padre
commit
5d9f3d3cf0

+ 2 - 3
web/components/Wallet/Welcome.vue

@@ -1,7 +1,6 @@
 <script setup lang="ts">
 /* Import modules. */
 import numeral from 'numeral'
-import signSharedTx from '../handlers/signSharedTx.ts'
 
 /* Define properties. */
 // https://vuejs.org/guide/components/props.html#props-declaration
@@ -21,7 +20,7 @@ const hushAddresses = ref(null)
 const nexaAddresses = ref(null)
 
 const HUSH_PROTOCOL_ID = 0x48555348
-const DUST_VAL = 546
+
 
 const balance = computed(() => {
     if (!Wallet.fusionInputs) {
@@ -39,7 +38,7 @@ const balance = computed(() => {
 const cashout = async () => {
     // alert('WIP?? sorry...')
 
-    const response = Wallet.completeFusion()
+    const response = await Wallet.completeFusion()
         .catch(err => console.error(err))
     console.log('COMPLETE FUSION', response)
 }

+ 3 - 3
web/handlers/initFusions.ts

@@ -12,8 +12,8 @@ const init = async (_fusionsDb) => {
 
     fusionsDb['4e9654f9-3de9-4f9a-8169-3834f40847f5'] = {
         tierid: 888000,
+        sessionid: null,
         progress: 0.1,
-        buildHash: null,
         numGuests: 0,
         numInputs: 0,
         numOutputs: 0,
@@ -29,8 +29,8 @@ const init = async (_fusionsDb) => {
 
     fusionsDb['6f765750-2267-4601-87be-80a416143a28'] = {
         tierid: 820000,
+        sessionid: '61c13c42-a961-4ad5-8f9b-f71bea2c343c',
         progress: 100.0,
-        buildHash: '9e3e7d52e195827b9061ceaa5b1a79d710ad4a8a',
         numGuests: 20,
         numInputs: 36,
         numOutputs: 68,
@@ -46,8 +46,8 @@ const init = async (_fusionsDb) => {
 
     fusionsDb['4eff6293-60e9-4a5a-83de-4b91da1f7de4'] = {
         tierid: 1800000,
+        sessionid: 'c759b697-9c24-4f24-bc72-d3c08abd20d1',
         progress: 100.0,
-        buildHash: '9e3e7d52e195827b9061ceaa5b1a79d710ad4a8a',
         numGuests: 22,
         numInputs: 51,
         numOutputs: 181,

+ 2 - 2
web/handlers/signSharedTx.ts

@@ -42,7 +42,7 @@ console.log('SIGN SHARED TX', _sessionid, _inputs, _outputs)
     protocolId = '1337'
 
     /* Set protocol message. */
-    msg = 'testing...'
+    msg = 'finalization...'
 
     script = [
         utf8ToBin(protocolId),
@@ -97,7 +97,7 @@ addressIdx = 0
     ecPair = bchjs.ECPair.fromWIF(wif)
 
 // FIXME Identify our owned inputs.
-ownedInputs = [ 0, 1, 2 ]
+ownedInputs = [ 0, 1, 2, 3, 4, 5, 6 ]
 
     for (let i = 0; i < _inputs.length; i++) {
         /* Verify input ownership. */

+ 53 - 8
web/server/routes/v1.post.ts

@@ -8,11 +8,20 @@ import {
     binToHex,
     binToUtf8,
 } from '@nexajs/utils'
+import { v5 as uuidv5 } from 'uuid'
+
+/* Set Hush (UUIDv5) Namespace */
+// NOTE: Replace the 1st 4-bytes of the standard prefix with the
+//       Hush Protocol ID (0x48555348).
+// For reference:
+//   - uuidv5.DNS 6ba7b810-9dad-11d1-80b4-00c04fd430c8
+//   - uuidv5.URL 6ba7b811-9dad-11d1-80b4-00c04fd430c8
+const HUSH_NAMESPACE = '48555348-9dad-11d1-80b4-00c04fd430c8'
 
-// import { useProfileStore } from '@/stores/profile'
 
 export default defineEventHandler(async (event) => {
     /* Initialize locals. */
+    let componentid
     let components
     let params
     let profile
@@ -28,9 +37,11 @@ export default defineEventHandler(async (event) => {
 
     /* Set (request) body. */
     const body = await readBody(event)
-    console.log('BODY', body)
+    // console.log('BODY', body)
 
     if (!body) {
+        setResponseStatus(event, 401)
+
         return `Authorization FAILED!`
     }
 
@@ -60,9 +71,13 @@ export default defineEventHandler(async (event) => {
         return 'Authorization failed!'
     }
 
+console.log('DEBUG::INSERTING A NEW FUSION')
+const fusion = Db.fusions['4e9654f9-3de9-4f9a-8169-3834f40847f5']
+console.log('FUSION', fusion)
+
     /* Request session. */
     profile = Db.profiles[authid]
-    // console.log('PROFILE', profile)
+    console.log('PROFILE', profile)
 
     /* Validate profile id. */
     if (typeof profile === 'undefined' || profile === null) {
@@ -92,11 +107,22 @@ export default defineEventHandler(async (event) => {
         // console.log('UPDATE PROFILE', response)
     }
 
-console.log('DEBUG::INSERTING A NEW FUSION')
-const fusion = Db.fusions['4e9654f9-3de9-4f9a-8169-3834f40847f5']
-console.log('FUSION', fusion)
+    if (profile) {
+        fusion.guests[authid] = {
+            createdAt: profile.createdAt,
+            updatedAt: profile.updatedAt,
+        }
+    } else {
+        setResponseStatus(event, 401)
+
+        return 'Oops! Authorization failed!'
+    }
+
+    fusion.numGuests = Object.keys(fusion.guests).length
+
+    /* Update progress. */
+    fusion.progress = 12.5
 
-    let componentid
 
     components.forEach(_component => {
         /* Validate inputs. */
@@ -115,8 +141,27 @@ console.log('FUSION', fusion)
         }
     })
 
+    fusion.numInputs = Object.keys(fusion.inputs).length
+
+    fusion.numOutputs = Object.keys(fusion.outputs).length
+
+/* Calculate session contents. */
+const sessionContents = JSON.stringify({ ...fusion.inputs, ...fusion.outputs })
+// console.log('SESSION CONTENTS', sessionContents)
+
+/* Calculate (UUIDv5) session hash. */
+const sessionHash = sha256(sessionContents)
+// console.log('SESSION HASH', sessionHash)
+
+/* Calculate session ID. */
+const sessionid = uuidv5(sessionHash, HUSH_NAMESPACE)
+// console.log('SESSION (uuid v5)', sessionid)
+
+    /* Set session ID. */
+    fusion.sessionid = sessionid
+
     /* Update progress. */
-    fusion.progress = 12.5
+    fusion.progress = 75.5
 
     /* Set (new) updated at (timestamp). */
     fusion.updatedAt = moment().unix()

+ 9 - 4
web/stores/wallet/completeFusion.ts

@@ -1,3 +1,7 @@
+/* Import modules. */
+import signSharedTx from '../../handlers/signSharedTx.ts'
+
+const DUST_VAL = 546
 
 export default async function () {
     /* Initialize locals. */
@@ -7,9 +11,10 @@ export default async function () {
     let rawTx
     let response
     let session
+    let sessionid
 
-    // FIXME
-    const sessionid = '4e9654f9-3de9-4f9a-8169-3834f40847f5'
+    // FIXME Where do we get the session ID from??
+    sessionid = '4e9654f9-3de9-4f9a-8169-3834f40847f5'
 
     /* Request session details. */
     session = await $fetch(`http://localhost:39159/v1/fusion/${sessionid}`)
@@ -73,10 +78,10 @@ export default async function () {
 
     /* Sign shared transaction. */
     rawTx = signSharedTx(
-        sessionid, Wallet.mnemonic, sortedInputs, sortedOutputs)
+        sessionid, this.mnemonic, sortedInputs, sortedOutputs)
     console.log('RAW TX', rawTx)
 
-    response = await Wallet.broadcast('BCH', rawTx)
+    response = await this.broadcast('BCH', rawTx)
         .catch(err => console.error(err))
     console.log('BROADCAST (response)', response)
 }

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

@@ -73,15 +73,23 @@ export default async function () {
                     const numOutputs = response.length
                     console.log('NUM OUTPUTS', numOutputs)
 
+                    // FIXME THE RESULTING BYTE COUNTS ARE WRONG!!
+                    //       THIS FUNCTION MAY BE OUTDATED!!
                     const fee = bchjs.BitcoinCash
                         .getByteCount({ P2PKH: fusionInputs.length }, { P2PKH: numOutputs })
                     // const fee = bchjs.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 })
                     console.log('FEE', fee)
 
+                    const safeFee = (fee * 2) + (fee * Math.random())
+                    console.log('SAFE FEE', safeFee)
+
+                    const feeOffset = Math.ceil(safeFee / numOutputs)
+                    console.log('FEE OFFSET', feeOffset)
+
                     const outputs = response.map(_outputValue => {
                         return {
                             address: this.getFusionAddress(),
-                            value: (_outputValue - Math.ceil(fee / numOutputs)),
+                            value: (_outputValue - feeOffset),
                             // value: (_outputValue - fee),
                         }
                     })