소스 검색

Improve unlocking script handlers.

Shomari 1 개월 전
부모
커밋
0cb6892732
3개의 변경된 파일59개의 추가작업 그리고 52개의 파일을 삭제
  1. 16 12
      web/server/routes/v1.post.ts
  2. 32 36
      web/stores/wallet/completeFusion.ts
  3. 11 4
      web/stores/wallet/signFusion.ts

+ 16 - 12
web/server/routes/v1.post.ts

@@ -149,18 +149,22 @@ console.log('FUSION', fusion)
         /* Handle inputs. */
         Object.keys(inputs).forEach(_outpoint => {
 console.log('OUTPOINT', _outpoint)
-            /* Handle unlocked (scripts). */
-            Object.keys(unlocked).forEach(_lockpoint => {
-console.log('LOCKPOINT', _lockpoint)
-                const unlock = unlocked[_lockpoint]
-console.log('UNLOCK', unlock)
-                /* Validate transaction hash. */
-                if (inputs[_outpoint].tx_hash === unlock.tx_hash) {
-console.log('FOUND A MATCH', unlock.tx_hash)
-                    /* Set unlocking script. */
-                    inputs[_outpoint].unlocking = unlock.unlocking
-                }
-            })
+
+            /* Add unlocking script to input. */
+            inputs[_outpoint].unlocking = unlocked[_outpoint].unlocking
+
+//             /* Handle unlocked (scripts). */
+//             Object.keys(unlocked).forEach(_lockpoint => {
+// console.log('LOCKPOINT', _lockpoint)
+//                 const unlock = unlocked[_lockpoint]
+// console.log('UNLOCK', unlock)
+//                 /* Validate transaction hash. */
+//                 if (inputs[_outpoint].tx_hash === unlock.tx_hash) {
+// console.log('FOUND A MATCH', unlock.tx_hash)
+//                     /* Set unlocking script. */
+//                     inputs[_outpoint].unlocking = unlock.unlocking
+//                 }
+//             })
         })
 console.log('UPDATED FUSION', fusion)
         return 'almost there...'

+ 32 - 36
web/stores/wallet/completeFusion.ts

@@ -8,13 +8,19 @@ const DUST_VAL = 546
 
 export default async function () {
     /* Initialize locals. */
+    let input
     let inputs
     let keys
     let outputs
     let rawTx
     let response
+    let script
     let session
     let sessionid
+    let sortedInputs
+    let sortedOutputs
+    let transaction
+    let transactionBuilder
 
     // FIXME Where do we get the session ID from??
     sessionid = '4e9654f9-3de9-4f9a-8169-3834f40847f5'
@@ -42,7 +48,7 @@ export default async function () {
     // console.log('KEYS (sorted)', keys)
 
     /* Initialize sorted inputs. */
-    const sortedInputs = []
+    sortedInputs = []
 
     /* Handle (input) keys. */
     keys.forEach(_keyid => {
@@ -81,7 +87,7 @@ export default async function () {
     // console.log('KEYS (sorted)', keys)
 
     /* Initialize sorted outputs. */
-    const sortedOutputs = []
+    sortedOutputs = []
 
     /* Handle (output) keys. */
     keys.forEach(_keyid => {
@@ -93,7 +99,7 @@ export default async function () {
     // console.log('OUTPUTS (sorted)', sortedOutputs)
 
     /* Sign shared transaction. */
-    const transactionBuilder = buildSharedTx.bind(this)(
+    transactionBuilder = buildSharedTx.bind(this)(
         sessionid, sortedInputs, sortedOutputs)
 // transactionBuilder.transaction.tx.ins[0].script = script
     console.log('FINALIZED TRANSACTION', transactionBuilder)
@@ -101,26 +107,35 @@ export default async function () {
 // console.log('UNLOCKING', inputs['185ad6a10ea70d977d943a910f54dc446163a16771017c6df35c7893c1db0c35'])
 
 
-    const transaction = transactionBuilder.transaction.buildIncomplete()
+    transaction = transactionBuilder.transaction.buildIncomplete()
     // const transaction = transactionBuilder.transaction
     console.log('TRANSACTION', transaction)
 
-const scripts = []
-Object.keys(inputs).forEach(_outpoint => {
-    const input = inputs[_outpoint]
-    console.log('SCRIPT INPUT', input)
+// const scripts = []
+    /* Handle (sorted) inputs. */
+    for (let i = 0; i < sortedInputs.length; i++) {
+        /* Set input. */
+        input = sortedInputs[i]
+        console.log('UNLOCKING SCRIPT INPUT', input)
 
-    scripts.push({
-        id: _outpoint,
-        script: Buffer.from(input.unlocking, 'hex')
-    })
-})
-console.log('SCRIPTS', scripts)
+        /* Set script. */
+        script = Buffer.from(input.unlocking, 'hex')
+
+        /* Add unlocking script to input. */
+        transaction.ins[i].script = script
+    }
+    console.log('TRANSACTION (hex)', transaction.toHex())
+// Object.keys(inputs).forEach(_outpoint => {
+//     // 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))
 
@@ -132,7 +147,7 @@ console.log('SCRIPTS', scripts)
     //   )
     // console.log('TX BUILDER-2', transactionBuilder2)
 
-      // build tx
+    // build tx
     //   const tx = transactionBuilder2.build()
     //   const tx = transactionBuilder2.transaction.buildIncomplete()
     //   console.log('TX BUILDER 2', tx)
@@ -147,22 +162,3 @@ console.log('SCRIPTS', scripts)
         .catch(err => console.error(err))
     console.log('BROADCAST (response)', response)
 }
-
-
-    // // Overwrite the tx inputs of the first partially-signed TX with the signed
-    // // inputs from the other two transactions.
-    // txObj.ins[1].script = txObj2.ins[1].script
-    // txObj.ins[2].script = txObj3.ins[2].script
-
-    // // console.log(`Fully-signed txObj.ins: ${JSON.stringify(txObj.ins, null, 2)}`)
-
-    // // Port the transaction object into the TransactionBuilder.
-    // const transactionBuilder = Bitcoin.TransactionBuilder.fromTransaction(
-    //   txObj,
-    //   'mainnet'
-    // )
-
-    // // build tx
-    // const tx = transactionBuilder.build()
-    // // output rawhex
-    // const txHex = tx.toHex()

+ 11 - 4
web/stores/wallet/signFusion.ts

@@ -1,5 +1,8 @@
 /* Import modules. */
-import { encryptForPubkey } from '@nexajs/crypto'
+import {
+    encryptForPubkey,
+    sha256,
+} from '@nexajs/crypto'
 import { binToHex } from '@nexajs/utils'
 
 import signSharedTx from '../../handlers/signSharedTx.ts'
@@ -12,6 +15,7 @@ export default async function () {
     let clubWallet
     let inputs
     let keys
+    let outpoint
     let outputs
     let publicKey
     // let rawTx
@@ -114,10 +118,14 @@ export default async function () {
     let unlocked = {}
 
     transaction.ins.forEach(_input => {
+console.log('INS (input)', _input)
         /* Validate (unlocked) script. */
         if (_input.script) {
+            outpoint = sha256(binToHex(_input.hash.reverse()) + ':' + _input.index)
+            console.log('INS (outpoint)', outpoint)
+
             /* Add input. */
-            unlocked[binToHex(_input.hash)] = {
+            unlocked[outpoint] = {
                 tx_hash: binToHex(_input.hash.reverse()),
                 unlocking: binToHex(_input.script),
             }
@@ -146,10 +154,9 @@ export default async function () {
     const body = {
         authid: binToHex(this.wallet.publicKey),
         actionid: 'unlock-components',
-        // tierid,
         unlocked: blindComponents,
     }
-    console.log('BODY', body)
+    // console.log('BODY', body)
 
     response = await $fetch('/v1', {
         method: 'POST',