Browse Source

Implement check for OP_RETURN

patch-2
Maran 11 years ago
committed by BtcDrak
parent
commit
5227d33d1d
  1. 20
      examples/Opreturn.js
  2. 13
      lib/Script.js

20
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();
}

13
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;
};

Loading…
Cancel
Save