From d05aa27bed35c0dc68805001be7262899a0ed2f5 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 3 Dec 2014 20:40:06 -0300 Subject: [PATCH] finish all tests --- lib/script.js | 55 +++++++++++++++++++++++++++++++ test/script.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/lib/script.js b/lib/script.js index 2474a5b..7b78e94 100644 --- a/lib/script.js +++ b/lib/script.js @@ -421,4 +421,59 @@ Script.prototype._addBuffer = function(buf, prepend) { return this; }; + +// high level script builder methods + +/** + * @returns a new Multisig output script for given public keys, + * requiring m of those public keys to spend + */ +Script.buildMultisigOut = function(pubkeys, m) { + console.log(pubkeys); + console.log(m); + return new Script(); +}; + +/** + * @returns a new pay to public key hash output for the given + * address or public key + */ +Script.buildPublicKeyHashOut = function(to) { + console.log(to); + return new Script(); +}; + +/** + * @returns a new pay to public key output for the given + * public key + */ +Script.buildPublicKeyOut = function(pubkey) { + console.log(pubkey); + return new Script(); +}; + +/** + * @returns a new OP_RETURN script with data + */ +Script.buildDataOut = function(data) { + console.log(data); + return new Script(); +}; + +/** + * @returns a new pay to script hash script for given script + */ +Script.buildScriptHashOut = function(script) { + console.log(script); + return new Script(); +}; + + +/** + * @returns a new pay to script hash script that pays to this script + */ +Script.prototype.toScriptHashOut = function() { + return Script.buildScriptHashOut(this); +}; + module.exports = Script; diff --git a/test/script.js b/test/script.js index ce0d896..7c27ba5 100644 --- a/test/script.js +++ b/test/script.js @@ -4,6 +4,8 @@ var should = require('chai').should(); var bitcore = require('..'); var Script = bitcore.Script; var Opcode = bitcore.Opcode; +var PublicKey = bitcore.PublicKey; +var Address = bitcore.Address; describe('Script', function() { @@ -374,5 +376,91 @@ describe('Script', function() { Script('OP_TRUE OP_FALSE').isStandard().should.equal(false); }); }); + describe('new methods', function() { + describe('#buildMultisigOut', function() { + var pubkey_hexs = [ + '022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da', + '03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9', + '021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18', + '02bf97f572a02a8900246d72c2e8fa3d3798a6e59c4e17de2d131d9c60d0d9b574', + '036a98a36aa7665874b1ba9130bc6d318e52fd3bdb5969532d7fc09bf2476ff842', + '033aafcbead78c08b0e0aacc1b0cdb40702a7c709b660bebd286e973242127e15b', + ]; + var test_mn = function(m, n) { + var pubkeys = pubkey_hexs.slice(0, n).map(PublicKey); + var s = Script.buildMultisigOut(pubkeys, m); + should.exist(s); + s.isMultisigOut().should.equal(true); + }; + for (var n = 1; n < 6; n++) { + for (var m = 1; m <= n; m++) { + it('should create ' + m + '-of-' + n, test_mn.bind(null, m, n)); + } + } + }); + describe('#buildPublicKeyHashOut', function() { + it('should create script from livenet address', function() { + var address = Address.fromString('1NaTVwXDDUJaXDQajoa9MqHhz4uTxtgK14'); + var s = Script.buildPublicKeyHashOut(address); + should.exist(s); + s.toString().should.equal('OP_DUP OP_HASH160 ecae7d092947b7ee4998e254aa48900d26d2ce1d OP_EQUALVERIFY OP_CHECKSIG'); + }); + it('should create script from testnet address', function() { + var address = Address.fromString('mxRN6AQJaDi5R6KmvMaEmZGe3n5ScV9u33'); + var s = Script.buildPublicKeyHashOut(address); + should.exist(s); + s.toString().should.equal('OP_DUP OP_HASH160 b96b816f378babb1fe585b7be7a2cd16eb99b3e4 OP_EQUALVERIFY OP_CHECKSIG'); + }); + it('should create script from public key', function() { + var pubkey = new PublicKey('022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da'); + var s = Script.buildPublicKeyHashOut(pubkey); + should.exist(s); + s.toString().should.equal('OP_DUP OP_HASH160 9674af7395592ec5d91573aa8d6557de55f60147 OP_EQUALVERIFY OP_CHECKSIG'); + }); + }); + describe('#buildPublicKeyOut', function() { + it('should create script from public key', function() { + var pubkey = new PublicKey('022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da'); + var s = Script.buildPublicKeyOut(pubkey); + should.exist(s); + s.toString().should.equal('9674af7395592ec5d91573aa8d6557de55f60147 OP_CHECKSIG'); + }); + }); + describe('#buildDataOut', function() { + it('should create script from empty data', function() { + var data = new Buffer(); + var s = Script.buildDataOut(data); + should.exist(s); + s.toString().should.equal('OP_RETURN'); + }); + it('should create script from some data', function() { + var data = new Buffer('bacacafe0102030405', 'hex'); + var s = Script.buildDataOut(data); + should.exist(s); + s.toString().should.equal('OP_RETURN bacacafe0102030405'); + }); + it('should create script from string', function() { + var data = 'hello world'; + var s = Script.buildDataOut(data); + should.exist(s); + s.toString().should.equal('OP_RETURN 68656c6c6f20776f726c64212121'); + }); + }); + describe('#buildScriptHashOut', function() { + it('should create script from another script', function() { + var inner = new Script('OP_DUP OP_HASH160 06c06f6d931d7bfba2b5bd5ad0d19a8f257af3e3 OP_EQUALVERIFY OP_CHECKSIG'); + var s = Script.buildScriptHashOut(inner); + should.exist(s); + s.toString().should.equal('OP_HASH160 b96d20131e15d948aa9196e477e9611d8e43c8f0 OP_EQUAL'); + }); + }); + describe('#toScriptHashOut', function() { + it('should create script from another script', function() { + var s = new Script('OP_DUP OP_HASH160 06c06f6d931d7bfba2b5bd5ad0d19a8f257af3e3 OP_EQUALVERIFY OP_CHECKSIG'); + s.toScriptHashOut().toString().should.equal('OP_HASH160 b96d20131e15d948aa9196e477e9611d8e43c8f0 OP_EQUAL'); + }); + }); + + }); });