From 2cfc78d78fda7bbe990fd9cb44060d3e7570a5b2 Mon Sep 17 00:00:00 2001 From: Nesopie Date: Mon, 25 Mar 2024 12:51:14 +0530 Subject: [PATCH] fix: coinbase serialization --- .gitignore | 3 ++- output.txt | 4 ++-- src/features/block/coinbaseTransaction.ts | 2 +- src/features/encoding/serializer.ts | 6 +----- src/index.ts | 5 +++++ 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 40b878d..8dd884b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules/ \ No newline at end of file +node_modules/ +output.txt \ No newline at end of file diff --git a/output.txt b/output.txt index 72b9472..a18ad39 100644 --- a/output.txt +++ b/output.txt @@ -1,5 +1,5 @@ -040000000000ffff00000000000000000000000000000000000000000000000000000000644fbf96c3029092bc313552afed1d20bf61e3e874ff09ec696496a1dd2d0e2854220166ffff001f68450000 -00000000010000000000000000000000000000000000000000000000000000000000000000ffffffff040323370800000000028651da4d000000001976a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac0000000000000000266a24aa21a9edf600a0667d8eef335e2f4cd9eaf746ce287bb6f62e9b0ef94869c65420a959a100000000 +040000000000ffff00000000000000000000000000000000000000000000000000000000644fbf96c3029092bc313552afed1d20bf61e3e874ff09ec696496a1dd2d0e28c5250166ffff001f8e190000 +000000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff040323370800000000028651da4d000000001976a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac0000000000000000266a24aa21a9edf600a0667d8eef335e2f4cd9eaf746ce287bb6f62e9b0ef94869c65420a959a10120000000000000000000000000000000000000000000000000000000000000000000000000 66de1e6c0437305764ab5ba0104bb5bdb6dc4a48b83323aeff155d2684cabccc a9f1f2279711f476c30e2e1382a0e99358c662a48fbd1774d7e5e7f8ea27d98d b9ea7324bf46de83ff81f0876dd8e6e8485318165cf9c5073549dd8824c7c2d3 diff --git a/src/features/block/coinbaseTransaction.ts b/src/features/block/coinbaseTransaction.ts index e9c5310..d163c4a 100644 --- a/src/features/block/coinbaseTransaction.ts +++ b/src/features/block/coinbaseTransaction.ts @@ -17,7 +17,7 @@ const coinbaseTemplate = { prevout: null, scriptsig: "03233708", scriptsig_asm: "OP_PUSHBYTES_3 233708", - witness: [Buffer.from(ZEROS, "hex")], + witness: [ZEROS], }, ], vout: [ diff --git a/src/features/encoding/serializer.ts b/src/features/encoding/serializer.ts index 0fa573b..700993b 100644 --- a/src/features/encoding/serializer.ts +++ b/src/features/encoding/serializer.ts @@ -93,11 +93,7 @@ export const txSerializer = (tx: Transaction) => { return { serializedTx, - serializedWTx: isWitness - ? tx.vin[0].txid === ZEROS - ? ZEROS - : serializedWTx - : serializedTx, + serializedWTx: isWitness ? serializedWTx : serializedTx, }; }; diff --git a/src/index.ts b/src/index.ts index c8fa77f..0c4b22e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { txSerializer, txWeight } from "./features/encoding/serializer"; import { feePerByte } from "./features/block/fee"; import { mine } from "./features/block/mine"; import * as path from "path"; +import * as bitcoin from "bitcoinjs-lib"; (async () => { const files = fs.readdirSync("./mempool"); @@ -38,6 +39,10 @@ import * as path from "path"; const { serializedBlock, blockHash, coinbaseTransaction } = mine(txs); + const ct = bitcoin.Transaction.fromHex( + txSerializer(coinbaseTransaction).serializedWTx + ); + fs.writeFileSync(outputFile, serializedBlock); fs.appendFileSync(outputFile, "\n"); fs.appendFileSync(