From 6057b875ec12bd76d0092f3b089551f3f6dc96fd Mon Sep 17 00:00:00 2001 From: joeuhren Date: Fri, 9 Jul 2021 20:39:02 -0600 Subject: [PATCH] Add new setting to decode tx OP_RETURN values --- README.md | 2 +- lib/database.js | 28 ++++++++++++++++++++++++++-- lib/settings.js | 5 ++++- models/tx.js | 3 ++- settings.json.template | 5 ++++- views/tx.pug | 8 ++++++++ 6 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1614c54..603341e 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ Table of Contents - **getmasternoderewardstotal:** Returns the total number of coins earned in masternode rewards for a specific address that arrived after a specific block height *\*only applicable to masternode coins* - **Claim Address:** Allows anyone to set custom display names for wallet addresses that they own using the **Sign Message** feature from their local wallet. Includes *bad word* filter support. - **Block Info:** Displays block summary and list of transactions for a specific block height - - **Transaction Info:** Displays transaction summary, list of input addresses and output addresses for a specific transaction + - **Transaction Info:** Displays transaction summary, optional OP_RETURN value, list of input addresses and output addresses for a specific transaction - **Address Info:** Displays wallet address summary (balance, total sent, total received, QR code) and a list of latest transactions for a specific wallet address - Choose from 22 built-in themes with tweakable settings such as light and dark options to customize the look and feel of the explorer: - **Exor** *\*default theme made especially for eIquidus* diff --git a/lib/database.js b/lib/database.js index c88b6fb..6ea273f 100644 --- a/lib/database.js +++ b/lib/database.js @@ -136,6 +136,19 @@ function save_tx(txid, blockheight, cb) { subloop.next(); }, function() { lib.calculate_total(vout, function(total) { + var op_return = null; + // check if the op_return value should be decoded and saved + if (settings.transaction_page.show_op_return) { + // loop through vout to find the op_return value + tx.vout.forEach(function (vout_data) { + // check if the op_return value exists + if (vout_data.scriptPubKey != null && vout_data.scriptPubKey.asm != null && vout_data.scriptPubKey.asm.indexOf('OP_RETURN') > -1) { + // decode the op_return value + op_return = hex_to_ascii(vout_data.scriptPubKey.asm.replace('OP_RETURN', '').trim()); + } + }); + } + var newTx = new Tx({ txid: tx.txid, vin: nvin, @@ -144,7 +157,8 @@ function save_tx(txid, blockheight, cb) { timestamp: tx.time, blockhash: tx.blockhash, blockindex: blockheight, - tx_type: (tx_type_vout == null ? tx_type_vin : tx_type_vout) + tx_type: (tx_type_vout == null ? tx_type_vin : tx_type_vout), + op_return: op_return }); newTx.save(function(err) { @@ -190,6 +204,13 @@ function check_add_db_field(model_obj, field_name, default_value, cb) { }); } +function hex_to_ascii(hex) { + var str = ''; + for (var i = 0; i < hex.length; i += 2) + str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); + return str; +} + module.exports = { // initialize DB connect: function(database, cb) { @@ -323,7 +344,10 @@ module.exports = { // collection has data // determine if tx_type field exists check_add_db_field(Tx, 'tx_type', null, function(exists) { - return cb(true); + // determine if op_return field exists + check_add_db_field(Tx, 'op_return', null, function(exists) { + return cb(true); + }); }); } else return cb(false); diff --git a/lib/settings.js b/lib/settings.js index 5fd8b3e..e1c82d2 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -354,7 +354,10 @@ exports.transaction_page = { // genesis_tx: Your coins genesis transaction hash is used to determine the beginning of the blockchain // For many bitcoin clones you can use the following cmd to find the list of transaction hashes from the genesis block: coin-cli getblock 00014f36c648cdbc750f7dd28487a23cd9e0b0f95f5fccc5b5d01367e3f57469 // NOTE: If this value is not entered correctly it will not be possible for the explorer to find or navigate to the genesis block by searching for the genesis transaction hash - "genesis_tx": "dd1d332ad2d8d8f49195056d482ae3c96fd2d16e9d166413b27ca7f19775644c" + "genesis_tx": "dd1d332ad2d8d8f49195056d482ae3c96fd2d16e9d166413b27ca7f19775644c", + // show_op_return: Determine whether to decode and show OP_RETURN values that may have been embeddeded in a transaction + // NOTE: Enabling this option will require a full reindex of the blockchain data before previously synced transactions can display the OP_RETURN value + "show_op_return": false }; // address_page: a collection of settings that pertain to the address page diff --git a/models/tx.js b/models/tx.js index a6d6783..6a14a60 100644 --- a/models/tx.js +++ b/models/tx.js @@ -9,7 +9,8 @@ var TxSchema = new Schema({ timestamp: { type: Number, default: 0, index: true }, blockhash: { type: String, index: true }, blockindex: {type: Number, default: 0, index: true}, - tx_type: { type: String, default: null } + tx_type: { type: String, default: null }, + op_return: { type: String, default: null } }, {id: false}); TxSchema.index({total: 1, total: -1, blockindex: 1, blockindex: -1}); diff --git a/settings.json.template b/settings.json.template index b5d1f5b..7f9bd0b 100644 --- a/settings.json.template +++ b/settings.json.template @@ -438,7 +438,10 @@ // genesis_tx: Your coins genesis transaction hash is used to determine the beginning of the blockchain // For many bitcoin clones you can use the following cmd to find the list of transaction hashes from the genesis block: coin-cli getblock 00014f36c648cdbc750f7dd28487a23cd9e0b0f95f5fccc5b5d01367e3f57469 // NOTE: If this value is not entered correctly it will not be possible for the explorer to find or navigate to the genesis block by searching for the genesis transaction hash - "genesis_tx": "dd1d332ad2d8d8f49195056d482ae3c96fd2d16e9d166413b27ca7f19775644c" + "genesis_tx": "dd1d332ad2d8d8f49195056d482ae3c96fd2d16e9d166413b27ca7f19775644c", + // show_op_return: Determine whether to decode and show OP_RETURN values that may have been embeddeded in a transaction + // NOTE: Enabling this option will require a full reindex of the blockchain data before previously synced transactions can display the OP_RETURN value + "show_op_return": false }, // address_page: a collection of settings that pertain to the address page diff --git a/views/tx.pug b/views/tx.pug index 46ff00b..d1c0bbd 100644 --- a/views/tx.pug +++ b/views/tx.pug @@ -43,6 +43,8 @@ block content tr th.text-center.d-table-cell.d-md-none th.d-none.d-md-table-cell #{settings.locale.tx_block_hash} + if settings.transaction_page.show_op_return == true + th='OP_RETURN' th.text-center #{settings.locale.confirmations} th.text-center #{settings.locale.timestamp} tbody @@ -54,6 +56,8 @@ block content span.fa.fa-eye(data-toggle='tooltip', data-placement='top', title=settings.locale.view_block) td.d-none.d-md-table-cell a.breakWord(href='/block/' + tx.blockhash) #{tx.blockhash} + if settings.transaction_page.show_op_return == true + td.breakWord #{tx.op_return} td.text-center #{confirms} td.text-center span#timestampCol @@ -65,6 +69,8 @@ block content span.fa.fa-eye(data-toggle='tooltip', data-placement='top', title=settings.locale.view_block) td.d-none.d-md-table-cell a.breakWord(href='/block/' + tx.blockhash) #{tx.blockhash} + if settings.transaction_page.show_op_return == true + td.breakWord #{tx.op_return} td.text-center #{confirms} td.text-center span#timestampCol @@ -75,6 +81,8 @@ block content span.fa.fa-eye(data-toggle='tooltip', data-placement='top', title=settings.locale.view_block) td.d-none.d-md-table-cell a.breakWord(href='/block/' + tx.blockhash) #{tx.blockhash} + if settings.transaction_page.show_op_return == true + td.breakWord #{tx.op_return} td.text-center #{confirms} td.text-center span#timestampCol