Add new setting to decode tx OP_RETURN values
This commit is contained in:
+26
-2
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user