From 56c12a03b9e463334e96cbbd02f7a7bb1702e8d3 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 12 Mar 2014 13:08:52 -0300 Subject: [PATCH 1/3] add example tests --- examples/Address.js | 36 +++++++---- examples/PeerManager.js | 78 +++++++++++----------- examples/Rpc.js | 48 +++++++------- examples/SendTx.js | 140 ++++++++++++++++++++-------------------- test/mute.js | 14 ++++ test/test.examples.js | 24 +++++++ 6 files changed, 195 insertions(+), 145 deletions(-) create mode 100644 test/mute.js create mode 100644 test/test.examples.js diff --git a/examples/Address.js b/examples/Address.js index af4131161..923d436b6 100644 --- a/examples/Address.js +++ b/examples/Address.js @@ -1,19 +1,27 @@ 'use strict'; -// Replace '..' with 'bitcore' if you plan on using this code elsewhere. -var Address = require('../Address'); +var run = function() { + // Replace '..' with 'bitcore' if you plan on using this code elsewhere. + var Address = require('../Address'); -var addrs = [ - '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', - '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', - 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', - '1600 Pennsylvania Ave NW', -].map(function(addr) { - return new Address(addr); -}); + var addrs = [ + '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx', + 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', + '1600 Pennsylvania Ave NW', + ].map(function(addr) { + return new Address(addr); + }); -addrs.forEach(function(addr) { - var valid = addr.isValid(); - console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); -}); + addrs.forEach(function(addr) { + var valid = addr.isValid(); + console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); + }); + +}; + +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/examples/PeerManager.js b/examples/PeerManager.js index f3fc20c29..3cb04018a 100644 --- a/examples/PeerManager.js +++ b/examples/PeerManager.js @@ -1,47 +1,47 @@ 'use strict'; -// Replace '..' with 'bitcore' if you plan on using this code elsewhere. +var run = function() { + // Replace '..' with 'bitcore' if you plan on using this code elsewhere. + var networks = require('../networks'); + var Peer = require('../Peer'); + var PeerManager = require('soop').load('../PeerManager', { + network: networks.testnet + }); -var util = require('util'); -var networks = require('../networks'); -var Peer = require('../Peer'); -var PeerManager = require('soop').load('../PeerManager', - {network: networks.testnet}); + var handleBlock = function(info) { + console.log('** Block Received **'); + console.log(info.message); + }; -var handleBlock = function(info) { + var handleTx = function(info) { + var tx = info.message.tx.getStandardizedObject(); - console.log('** Block Received **'); - console.log(info.message); + console.log('** TX Received **'); + console.log(tx); + }; + var handleInv = function(info) { + console.log('** Inv **'); + console.log(info.message); + + var invs = info.message.invs; + info.conn.sendGetData(invs); + }; + + var peerman = new PeerManager(); + + peerman.addPeer(new Peer('127.0.0.1', 18333)); + + peerman.on('connection', function(conn) { + conn.on('inv', handleInv); + conn.on('block', handleBlock); + conn.on('tx', handleTx); + }); + + peerman.start(); }; -var handleTx = function(info) { - - var tx = info.message.tx.getStandardizedObject(); - - console.log('** Block TX **'); - console.log(tx); - -}; - -var handleInv = function(info) { - - console.log('** Block Inv **'); - console.log(info.message); - - var invs = info.message.invs; - info.conn.sendGetData(invs); - -}; - -var peerman = new PeerManager(); - -peerman.addPeer( new Peer('127.0.0.1', 18333) ); - -peerman.on('connection', function(conn) { - conn.on('inv', handleInv); - conn.on('block', handleBlock); - conn.on('tx', handleTx); -}); - -peerman.start(); +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/examples/Rpc.js b/examples/Rpc.js index ca4e12e46..ea180bfd8 100644 --- a/examples/Rpc.js +++ b/examples/Rpc.js @@ -1,29 +1,31 @@ 'use strict'; -// Replace '..' with 'bitcore' if you plan on using this code elsewhere. +var run = function() { + // Replace '..' with 'bitcore' if you plan on using this code elsewhere. + var RpcClient = require('../RpcClient'); + var hash = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; -var util = require('util'); -var RpcClient = require('../RpcClient'); -var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; + var config = { + protocol: 'http', + user: 'user', + pass: 'pass', + host: '127.0.0.1', + port: '18332', + }; - var config = { - protocol: 'http', - user: 'user', - pass: 'pass', - host: '127.0.0.1', - port: '18332', + var rpc = new RpcClient(config); + + rpc.getBlock(hash, function(err, ret) { + if (err) { + console.error('An error occured fetching block', hash); + console.error(err); + return; + } + console.log(ret); + }); }; - -var rpc = new RpcClient(config); -rpc.getBlock(hash, function(err, ret) { - - if(err) { - console.error("An error occured fetching block", hash); - console.error(err); - return; - } - - console.log(ret); - -}); +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/examples/SendTx.js b/examples/SendTx.js index da7e7bd97..90b70ef79 100644 --- a/examples/SendTx.js +++ b/examples/SendTx.js @@ -1,73 +1,75 @@ +'use strict'; -// Replace '..' with 'bitcore' if you plan on using this code elsewhere. - -var networks = require('../networks'); -var Peer = require('../Peer'); -var Transaction = require('../Transaction'); -var Address = require('../Address'); -var Script = require('../Script'); -var coinUtil = require('../util/util'); -var PeerManager = require('soop').load('../PeerManager', - {network: networks.testnet}); - -var createTx = function() { - - var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; - var TXIN_N = 0; - var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; - var VAL = '1.234'; - - var txobj = { - version: 1, - lock_time: 0, - ins: [], - outs: [] - } - - var txin = { - s: coinUtil.EMPTY_BUFFER, // Add signature - q: 0xffffffff - }; - - var hash = new Buffer(TXIN.split('').reverse(), 'hex'); - - var vout = parseInt(TXIN_N); - var voutBuf = new Buffer(4); - - voutBuf.writeUInt32LE(vout, 0); - txin.o = Buffer.concat([hash, voutBuf]); - txobj.ins.push(txin); - - var addr = new Address(ADDR); - var script = Script.createPubKeyHashOut(addr.payload()); - var valueNum = coinUtil.parseValue(VAL); - var value = coinUtil.bigIntToValue(valueNum); - - var txout = { - v: value, - s: script.getBuffer(), - }; - txobj.outs.push(txout); - - return new Transaction(txobj); - -}; - -var peerman = new PeerManager(); -peerman.addPeer(new Peer('127.0.0.1', 18333)); - -peerman.on('connect', function(conn) { - - var conn = peerman.getActiveConnection(); - - if (conn) { - conn.sendTx(createTx()); - } - - conn.on('reject', function () { - console.log('Transaction Rejected'); +var run = function() { + // Replace '..' with 'bitcore' if you plan on using this code elsewhere. + var networks = require('../networks'); + var Peer = require('../Peer'); + var Transaction = require('../Transaction'); + var Address = require('../Address'); + var Script = require('../Script'); + var coinUtil = require('../util/util'); + var PeerManager = require('soop').load('../PeerManager', { + network: networks.testnet }); -}); + var createTx = function() { + var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; + var TXIN_N = 0; + var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; + var VAL = '0.001'; -peerman.start(); + var txobj = { + version: 1, + lock_time: 0, + ins: [], + outs: [] + }; + + var txin = { + s: coinUtil.EMPTY_BUFFER, // Add signature + q: 0xffffffff + }; + + var hash = new Buffer(TXIN.split('').reverse(), 'hex'); + var vout = parseInt(TXIN_N); + var voutBuf = new Buffer(4); + + voutBuf.writeUInt32LE(vout, 0); + txin.o = Buffer.concat([hash, voutBuf]); + txobj.ins.push(txin); + + var addr = new Address(ADDR); + var script = Script.createPubKeyHashOut(addr.payload()); + var valueNum = coinUtil.parseValue(VAL); + var value = coinUtil.bigIntToValue(valueNum); + + var txout = { + v: value, + s: script.getBuffer(), + }; + txobj.outs.push(txout); + + return new Transaction(txobj); + + }; + + var peerman = new PeerManager(); + peerman.addPeer(new Peer('127.0.0.1', 18333)); + + peerman.on('connect', function() { + var conn = peerman.getActiveConnection(); + if (conn) { + conn.sendTx(createTx()); + } + conn.on('reject', function() { + console.log('Transaction Rejected'); + }); + }); + + peerman.start(); +}; + +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/test/mute.js b/test/mute.js new file mode 100644 index 000000000..4dc60504a --- /dev/null +++ b/test/mute.js @@ -0,0 +1,14 @@ +'use strict'; + +var backup = console.log; +var nop = function() {}; +var mute = function() { + console.log = nop; +}; + +var unmute = function() { + console.log = backup; +}; + +module.exports.mute = mute; +module.exports.unmute = unmute; diff --git a/test/test.examples.js b/test/test.examples.js new file mode 100644 index 000000000..63cb4ab37 --- /dev/null +++ b/test/test.examples.js @@ -0,0 +1,24 @@ +'use strict'; + +var chai = chai || require('chai'); +var should = chai.should(); +var mute = require('./mute').mute; +var unmute = require('./mute').unmute; + +var examples = [ + 'Address', + 'PeerManager', + 'Rpc', + 'SendTx', +]; + +describe('Examples run', function() { + before(mute); + after(unmute); + examples.forEach(function(example) { + it('valid '+example, function() { + var ex = require('../examples/'+example); + ex.run.should.not.throw(); + }); + }); +}); From d35609ff9bf3ec4f507dc9d3bb8ff59c843eccbf Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 12 Mar 2014 14:21:23 -0300 Subject: [PATCH 2/3] change example to new style --- examples/Address.js | 5 +++-- examples/PeerManager.js | 7 ++++--- examples/Rpc.js | 5 +++-- examples/SendTx.js | 15 ++++++++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/examples/Address.js b/examples/Address.js index 923d436b6..7aa9778cf 100644 --- a/examples/Address.js +++ b/examples/Address.js @@ -2,8 +2,9 @@ var run = function() { - // Replace '..' with 'bitcore' if you plan on using this code elsewhere. - var Address = require('../Address'); + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var Address = bitcore.Address; var addrs = [ '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', diff --git a/examples/PeerManager.js b/examples/PeerManager.js index 3cb04018a..0d0bdbae5 100644 --- a/examples/PeerManager.js +++ b/examples/PeerManager.js @@ -1,9 +1,10 @@ 'use strict'; var run = function() { - // Replace '..' with 'bitcore' if you plan on using this code elsewhere. - var networks = require('../networks'); - var Peer = require('../Peer'); + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var networks = bitcore.networks; + var Peer = bitcore.Peer; var PeerManager = require('soop').load('../PeerManager', { network: networks.testnet }); diff --git a/examples/Rpc.js b/examples/Rpc.js index ea180bfd8..e4bf5226b 100644 --- a/examples/Rpc.js +++ b/examples/Rpc.js @@ -1,8 +1,9 @@ 'use strict'; var run = function() { - // Replace '..' with 'bitcore' if you plan on using this code elsewhere. - var RpcClient = require('../RpcClient'); + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var RpcClient = bitcore.RpcClient; var hash = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; var config = { diff --git a/examples/SendTx.js b/examples/SendTx.js index 90b70ef79..36646606a 100644 --- a/examples/SendTx.js +++ b/examples/SendTx.js @@ -1,13 +1,14 @@ 'use strict'; var run = function() { - // Replace '..' with 'bitcore' if you plan on using this code elsewhere. - var networks = require('../networks'); - var Peer = require('../Peer'); - var Transaction = require('../Transaction'); - var Address = require('../Address'); - var Script = require('../Script'); - var coinUtil = require('../util/util'); + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var networks = bitcore.networks; + var Peer = bitcore.Peer; + var Transaction = bitcore.Transaction; + var Address = bitcore.Address; + var Script = bitcore.Script; + var coinUtil = bitcore.util; var PeerManager = require('soop').load('../PeerManager', { network: networks.testnet }); From 1732ec2ced7a7e9916e5055d0d12320969756779 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 12 Mar 2014 15:21:46 -0300 Subject: [PATCH 3/3] add new example --- README.md | 221 ++++++++++++++++++++---------------------- examples/Script.js | 52 ++++++++++ test/mute.js | 3 + test/test.examples.js | 3 +- util/log.js | 9 +- 5 files changed, 171 insertions(+), 117 deletions(-) create mode 100644 examples/Script.js diff --git a/README.md b/README.md index 33f9750af..42b96f315 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,8 @@ Some examples are provided at the [examples](/examples) path. Here are some snip ## Validating an address Validating a Bitcoin address: ```js -var Address = require('bitcore/Address'); +var bitcore = require('bitcore'); +var Address = bitcore.Address; var addrs = [ '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', @@ -48,80 +49,103 @@ addrs.forEach(function(addr) { ## Monitoring Blocks and Transactions For this example you need a running bitcoind instance with RPC enabled. ```js -var util = require('util'); -var networks = require('bitcore/networks'); -var Peer = require('bitcore/Peer'); -var PeerManager = require('soop').load('bitcore/PeerManager', - {network: networks.testnet}); +var bitcore = require('bitcore'); +var networks = bitcore.networks; +var Peer = bitcore.Peer; +var PeerManager = require('soop').load('../PeerManager', { + network: networks.testnet +}); var handleBlock = function(info) { - console.log('** Block Received **'); console.log(info.message); - }; var handleTx = function(info) { - var tx = info.message.tx.getStandardizedObject(); - console.log('** Block TX **'); + console.log('** TX Received **'); console.log(tx); - }; var handleInv = function(info) { - - console.log('** Block Inv **'); + console.log('** Inv **'); console.log(info.message); var invs = info.message.invs; info.conn.sendGetData(invs); - }; var peerman = new PeerManager(); -peerman.addPeer( new Peer('127.0.0.1', 18333) ); - +peerman.addPeer(new Peer('127.0.0.1', 18333)); + peerman.on('connection', function(conn) { - conn.on('inv', handleInv); + conn.on('inv', handleInv); conn.on('block', handleBlock); - conn.on('tx', handleTx); + conn.on('tx', handleTx); }); peerman.start(); - ``` PeerManager will emit the following events: 'version', 'verack', 'addr', 'getaddr', 'error' 'disconnect'; and will relay events like: 'tx', 'block', 'inv'. Please see [PeerManager.js](PeerManager.js), [Peer.js](Peer.js) and [Connection.js](Connection.js) +## Consuming bitcoind RPC +For this example you need a running bitcoind instance with RPC enabled. +```js +var bitcore = require('bitcore'); +var RpcClient = bitcore.RpcClient; +var hash = '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; + +var config = { + protocol: 'http', + user: 'user', + pass: 'pass', + host: '127.0.0.1', + port: '18332', +}; + +var rpc = new RpcClient(config); + +rpc.getBlock(hash, function(err, ret) { + if (err) { + console.error('An error occured fetching block', hash); + console.error(err); + return; + } + console.log(ret); +}); +``` +Check the list of all supported RPC call at [RpcClient.js](RpcClient.js) + ## Creating and sending a Transaction through P2P For this example you need a running bitcoind instance with RPC enabled. ```js -var networks = require('bitcore/networks'); -var Peer = require('bitcore/Peer'); -var Transaction = require('bitcore/Transaction'); -var Address = require('bitcore/Address'); -var Script = require('bitcore/Script'); -var coinUtil = require('bitcore/util/util'); -var PeerManager = require('soop').load('bitcore/PeerManager', - {network: networks.testnet}); +var bitcore = require('bitcore'); +var networks = bitcore.networks; +var Peer = bitcore.Peer; +var Transaction = bitcore.Transaction; +var Address = bitcore.Address; +var Script = bitcore.Script; +var coinUtil = bitcore.util; +var PeerManager = require('soop').load('../PeerManager', { + network: networks.testnet +}); var createTx = function() { - - var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; + var TXIN = 'd05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; var TXIN_N = 0; - var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; - var VAL = '1.234'; + var ADDR = 'muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; + var VAL = '0.001'; var txobj = { - version: 1, + version: 1, lock_time: 0, - ins: [], - outs: [] - } + ins: [], + outs: [] + }; var txin = { s: coinUtil.EMPTY_BUFFER, // Add signature @@ -129,18 +153,17 @@ var createTx = function() { }; var hash = new Buffer(TXIN.split('').reverse(), 'hex'); - - var vout = parseInt(TXIN_N); + var vout = parseInt(TXIN_N); var voutBuf = new Buffer(4); voutBuf.writeUInt32LE(vout, 0); txin.o = Buffer.concat([hash, voutBuf]); txobj.ins.push(txin); - var addr = new Address(ADDR); - var script = Script.createPubKeyHashOut(addr.payload()); + var addr = new Address(ADDR); + var script = Script.createPubKeyHashOut(addr.payload()); var valueNum = coinUtil.parseValue(VAL); - var value = coinUtil.bigIntToValue(valueNum); + var value = coinUtil.bigIntToValue(valueNum); var txout = { v: value, @@ -155,97 +178,67 @@ var createTx = function() { var peerman = new PeerManager(); peerman.addPeer(new Peer('127.0.0.1', 18333)); -peerman.on('connect', function(conn) { - +peerman.on('connect', function() { var conn = peerman.getActiveConnection(); - if (conn) { conn.sendTx(createTx()); } - - conn.on('reject', function () { - console.log('Transaction Rejected'); + conn.on('reject', function() { + console.log('Transaction Rejected'); }); - }); peerman.start(); ``` -## Consuming bitcoind RPC -For this example you need a running bitcoind instance with RPC enabled. -```js -var util = require('util'); -var RpcClient = require('bitcore/RpcClient'); -var hash = process.argv[2] || '0000000000b6288775bbd326bedf324ca8717a15191da58391535408205aada4'; - - var config = { - protocol: 'http', - user: 'user', - pass: 'pass', - host: '127.0.0.1', - port: '18332', -}; - -var rpc = new RpcClient(config); - -rpc.getBlock(hash, function(err, ret) { - - if(err) { - console.error("An error occured fetching block", hash); - console.error(err); - return; - } - - console.log(ret); - -}); -``` -Check the list of all supported RPC call at [RpcClient.js](RpcClient.js) ## Parsing a Script -Gets an address strings from a ScriptPubKey Buffer +Gets an address strings from a ScriptPubKey Buffer -``` - var Address = require('bitcore/Address'); - var coinUtil= require('bitcore/util/util'); +```js +var bitcore = require('bitcore'); +var Address = bitcore.Address; +var coinUtil = bitcore.util; +var Script = bitcore.Script; +var network = bitcore.networks.testnet; - var getAddrStr = function(s) { - var addrStrs = []; - var type = s.classify(); - var addr; +var getAddrStr = function(s) { + var addrStrs = []; + var type = s.classify(); + var addr; - switch (type) { - case Script.TX_PUBKEY: - var chunk = s.captureOne(); - addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); - addrStrs.push(addr.toString()); - break; - case Script.TX_PUBKEYHASH: - addr = new Address(network.addressPubkey, s.captureOne()); - addrStrs.push(addr.toString()); - break; - case Script.TX_SCRIPTHASH: - addr = new Address(network.addressScript, s.captureOne()); - addrStrs.push(addr.toString()); - break; - case Script.TX_MULTISIG: - var chunks = s.capture(); - chunks.forEach(function(chunk) { - var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); - addrStrs.push(a.toString()); - }); - break; - case Script.TX_UNKNOWN: - break; - } - return addrStrs; - }; + switch (type) { + case Script.TX_PUBKEY: + var chunk = s.captureOne(); + addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); + addrStrs.push(addr.toString()); + break; + case Script.TX_PUBKEYHASH: + addr = new Address(network.addressPubkey, s.captureOne()); + addrStrs.push(addr.toString()); + break; + case Script.TX_SCRIPTHASH: + addr = new Address(network.addressScript, s.captureOne()); + addrStrs.push(addr.toString()); + break; + case Script.TX_MULTISIG: + var chunks = s.capture(); + chunks.forEach(function(chunk) { + var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); + addrStrs.push(a.toString()); + }); + break; + case Script.TX_UNKNOWN: + console.log('tx type unkown'); + break; + } + return addrStrs; +}; - var s = new Script(scriptBuffer); - console.log(getAddrStr(s); - +var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG'; +var s = Script.fromHumanReadable(script); +console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT ``` #Security diff --git a/examples/Script.js b/examples/Script.js new file mode 100644 index 000000000..81811d853 --- /dev/null +++ b/examples/Script.js @@ -0,0 +1,52 @@ +'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 getAddrStr = function(s) { + var addrStrs = []; + var type = s.classify(); + var addr; + + switch (type) { + case Script.TX_PUBKEY: + var chunk = s.captureOne(); + addr = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); + addrStrs.push(addr.toString()); + break; + case Script.TX_PUBKEYHASH: + addr = new Address(network.addressPubkey, s.captureOne()); + addrStrs.push(addr.toString()); + break; + case Script.TX_SCRIPTHASH: + addr = new Address(network.addressScript, s.captureOne()); + addrStrs.push(addr.toString()); + break; + case Script.TX_MULTISIG: + var chunks = s.capture(); + chunks.forEach(function(chunk) { + var a = new Address(network.addressPubkey, coinUtil.sha256ripe160(chunk)); + addrStrs.push(a.toString()); + }); + break; + case Script.TX_UNKNOWN: + console.log('tx type unkown'); + break; + } + return addrStrs; + }; + + var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG'; + var s = Script.fromHumanReadable(script); + console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT +}; + +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/test/mute.js b/test/mute.js index 4dc60504a..1e1f18218 100644 --- a/test/mute.js +++ b/test/mute.js @@ -1,13 +1,16 @@ 'use strict'; var backup = console.log; +var ebackup = console.error; var nop = function() {}; var mute = function() { console.log = nop; + console.error = nop; }; var unmute = function() { console.log = backup; + console.error = ebackup; }; module.exports.mute = mute; diff --git a/test/test.examples.js b/test/test.examples.js index 63cb4ab37..34fbcd0c5 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -10,9 +10,10 @@ var examples = [ 'PeerManager', 'Rpc', 'SendTx', + 'Script', ]; -describe('Examples run', function() { +describe('Examples', function() { before(mute); after(unmute); examples.forEach(function(example) { diff --git a/util/log.js b/util/log.js index f2dbce42a..435007f04 100644 --- a/util/log.js +++ b/util/log.js @@ -1,9 +1,14 @@ +'use strict'; + var noop = function() {}; +var cl = function() { + console.log(arguments); +}; var loggers = { none: {info: noop, warn: noop, err: noop, debug: noop}, - normal: {info: console.log, warn: console.log, err: console.log, debug: noop}, - debug: {info: console.log, warn: console.log, err: console.log, debug: console.log}, + normal: {info: cl, warn: cl, err: cl, debug: noop}, + debug: {info: cl, warn: cl, err: cl, debug: cl}, }; var config = require('../config');