From 5227d33d1d389e078601587386f4243f5a917fea Mon Sep 17 00:00:00 2001 From: Maran Date: Thu, 14 Aug 2014 19:04:52 +0200 Subject: [PATCH] Implement check for OP_RETURN --- examples/Opreturn.js | 20 ++++++++++++++++++++ lib/Script.js | 13 ++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 examples/Opreturn.js diff --git a/examples/Opreturn.js b/examples/Opreturn.js new file mode 100644 index 0000000..66ce779 --- /dev/null +++ b/examples/Opreturn.js @@ -0,0 +1,20 @@ +'use strict'; + +var run = function() { + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var Address = bitcore.Address; + var coinUtil = bitcore.util; + var Script = bitcore.Script; + var network = bitcore.networks.testnet; + + var script = 'OP_RETURN 58434c524e4748530000000000000000000000010000000005f5e100'; + var s = Script.fromHumanReadable(script); + var result = (s.classify() == Script.TX_RETURN) + console.log("Is op_return:", result); +}; + +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/lib/Script.js b/lib/Script.js index ea2217f..86e9a02 100644 --- a/lib/Script.js +++ b/lib/Script.js @@ -11,13 +11,15 @@ var TX_PUBKEY = 1; var TX_PUBKEYHASH = 2; var TX_MULTISIG = 3; var TX_SCRIPTHASH = 4; +var TX_RETURN = 5; var TX_TYPES = [ 'unknown', 'pubkey', 'pubkeyhash', 'multisig', - 'scripthash' + 'scripthash', + 'return' ]; function Script(buffer) { @@ -35,6 +37,7 @@ Script.TX_PUBKEY = TX_PUBKEY; Script.TX_PUBKEYHASH = TX_PUBKEYHASH; Script.TX_MULTISIG = TX_MULTISIG; Script.TX_SCRIPTHASH = TX_SCRIPTHASH; +Script.TX_RETURN = TX_RETURN; Script.prototype.parse = function() { this.chunks = []; @@ -90,6 +93,12 @@ Script.prototype.isPubkey = function() { this.chunks[1] == Opcode.map.OP_CHECKSIG); }; +Script.prototype.isReturn = function() { + return (this.chunks.length == 2 && + Buffer.isBuffer(this.chunks[1]) && + this.chunks[0] == Opcode.map.OP_RETURN); +}; + Script.prototype.isPubkeyHash = function() { return (this.chunks.length == 5 && this.chunks[0] == Opcode.map.OP_DUP && @@ -282,6 +291,8 @@ Script.prototype.classify = function() { return TX_MULTISIG; if (this.isPubkey()) return TX_PUBKEY; + if (this.isReturn()) + return TX_RETURN; return TX_UNKNOWN; };