diff --git a/Opcode.js b/Opcode.js index 5ae4bee82..44f693ff0 100644 --- a/Opcode.js +++ b/Opcode.js @@ -155,4 +155,14 @@ for (var k in Opcode.map) { } } +Opcode.asList = function() { + var keys = []; + for (var prop in Opcode.map) { + if (Opcode.map.hasOwnProperty(prop)) { + keys.push(prop); + } + } + return keys; +}; + module.exports = require('soop')(Opcode); diff --git a/Script.js b/Script.js index ada391de6..95f522c2c 100644 --- a/Script.js +++ b/Script.js @@ -265,6 +265,8 @@ Script.prototype.getBuffer = function() { return this.buffer; }; +Script.prototype.serialize = Script.prototype.getBuffer; + Script.prototype.getStringContent = function(truncate, maxEl) { if (truncate === null) { truncate = true; diff --git a/test/test.Opcode.js b/test/test.Opcode.js index 235f26cb9..9a0ea663e 100644 --- a/test/test.Opcode.js +++ b/test/test.Opcode.js @@ -17,13 +17,13 @@ describe('Opcode', function() { should.exist(Opcode); }); it('should be able to create instance', function() { - var oc = new Opcode(); + var oc = new Opcode(81); should.exist(oc); }); it('should be able to create some constants', function() { // TODO: test works in node but not in browser for (var i in Opcode.map) { - eval('var '+i + ' = ' + Opcode.map[i] + ';'); + eval('var ' + i + ' = ' + Opcode.map[i] + ';'); } should.exist(OP_VER); should.exist(OP_HASH160); @@ -31,11 +31,10 @@ describe('Opcode', function() { should.exist(OP_EQUALVERIFY); should.exist(OP_CHECKSIG); should.exist(OP_CHECKMULTISIG); - + }); + it('#asList should work', function() { + var list = Opcode.asList(); + (typeof(list[0])).should.equal('string'); + list.length.should.equal(116); }); }); - - - - - diff --git a/test/test.sighash.js b/test/test.sighash.js new file mode 100644 index 000000000..8ef6d1fc7 --- /dev/null +++ b/test/test.sighash.js @@ -0,0 +1,91 @@ +'use strict'; + +// inspired in bitcoin core test: +// https://github.com/bitcoin/bitcoin/blob/7d49a9173ab636d118c2a81fc3c3562192e7813a/src/test/sighash_tests.cpp + +var chai = chai || require('chai'); +var should = chai.should(); +var bitcore = bitcore || require('../bitcore'); +var Transaction = bitcore.Transaction; +var Script = bitcore.Script; +var Opcode = bitcore.Opcode; + +var randInt = function(low, high) { + return Math.floor(Math.random() * (high - low + 1) + low); +}; +var randUIntN = function(nBits) { + return randInt(0, Math.pow(2, nBits)); +}; +var randUInt32 = function() { + return randUIntN(32); +}; +var randBool = function() { + return Math.random() < 0.5; +}; +var hexAlphabet = '0123456789abcdef'; +var randHex = function() { + return hexAlphabet[randInt(0, 15)]; +}; +var randHexN = function(n) { + var s = ''; + while (n--) { + s += randHex(); + } + return s; +}; +var randTxHash = function() { + return randHexN(64); +}; +var randPick = function(list) { + return list[randInt(0, list.length-1)]; +}; + + +var opList = Opcode.asList(); + +var randomScript = function() { + var s = new Script(); + var ops = randInt(0,10); + for (var i=0; i