From 0d543b902a4186ea44c717aeff058755c6a18224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Sat, 18 Nov 2023 11:36:34 -0600 Subject: [PATCH] Update transaction format in blocks to be same as by hash --- clients/nodejs/modules/JsonRpcServer.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/clients/nodejs/modules/JsonRpcServer.js b/clients/nodejs/modules/JsonRpcServer.js index 175e54a15..cd51d7fb6 100644 --- a/clients/nodejs/modules/JsonRpcServer.js +++ b/clients/nodejs/modules/JsonRpcServer.js @@ -324,7 +324,8 @@ class JsonRpcServer { async getTransactionByBlockHashAndIndex(blockHash, txIndex) { const block = await this._client.getBlock(Nimiq.Hash.fromString(blockHash), true); if (block && block.transactions.length > txIndex) { - return this._transactionToObj(block.transactions[txIndex], block, txIndex); + const head = await this._client.getHeadHeight() + return this._transactionToObj(block.transactions[txIndex], block, head); } return null; } @@ -332,7 +333,8 @@ class JsonRpcServer { async getTransactionByBlockNumberAndIndex(number, txIndex) { const block = await this._getBlockByNumber(number); if (block && block.transactions.length > txIndex) { - return this._transactionToObj(block.transactions[txIndex], block, txIndex); + const head = await this._client.getHeadHeight() + return this._transactionToObj(block.transactions[txIndex], block, head); } return null; } @@ -790,12 +792,14 @@ class JsonRpcServer { confirmations: (await this._client.getHeadHeight()) - block.height + 1 }; if (block.isFull()) { + const head = await this._client.getHeadHeight() + obj.miner = block.minerAddr.toHex(); obj.minerAddress = block.minerAddr.toUserFriendlyAddress(); obj.extraData = Nimiq.BufferUtils.toHex(block.body.extraData); obj.size = block.serializedSize; obj.transactions = includeTransactions - ? await Promise.all(block.transactions.map((tx, i) => this._transactionToObj(tx, block, i))) + ? await Promise.all(block.transactions.map((tx) => this._transactionToObj(tx, block, head))) : block.transactions.map((tx) => tx.hash().toHex()); } return obj; @@ -804,25 +808,29 @@ class JsonRpcServer { /** * @param {Transaction} tx * @param {Block} [block] - * @param {number} [i] + * @param {number} [head] * @private */ - async _transactionToObj(tx, block, i) { + async _transactionToObj(tx, block, head) { return { hash: tx.hash().toHex(), blockHash: block ? block.hash().toHex() : undefined, blockNumber: block ? block.height : undefined, timestamp: block ? block.timestamp : undefined, - confirmations: block ? (await this._client.getHeadHeight()) - block.height + 1 : 0, - transactionIndex: i, + confirmations: block && head ? head - block.height + 1 : 0, from: tx.sender.toHex(), fromAddress: tx.sender.toUserFriendlyAddress(), + fromType: tx.senderType, to: tx.recipient.toHex(), toAddress: tx.recipient.toUserFriendlyAddress(), + toType: tx.recipientType, value: tx.value, fee: tx.fee, data: Nimiq.BufferUtils.toHex(tx.data) || null, + proof: Nimiq.BufferUtils.toHex(tx.proof) || null, flags: tx.flags, + validityStartHeight: tx.validityStartHeight, + networkId: tx.networkId, }; }