From b3ad87af41b78aa025f9ad70a1fb537d4215e283 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 17 Feb 2014 16:39:13 -0300 Subject: [PATCH 1/4] add Readme.md compilation in grunt --- Gruntfile.js | 18 ++++++++++++++++++ README.md | 1 + package.json | 1 + 3 files changed, 20 insertions(+) diff --git a/Gruntfile.js b/Gruntfile.js index 32d0f9d..0c45029 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -6,6 +6,7 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-mocha-test'); + grunt.loadNpmTasks('grunt-markdown'); // Project Configuration grunt.initConfig({ @@ -28,6 +29,10 @@ module.exports = function(grunt) { } }, watch: { + readme: { + files: ['README.md'], + tasks: ['markdown'] + }, scripts: { files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!**/bundle.js', '!**/vendor.js'], tasks: ['browserify'/*, 'mochaTest'*/], @@ -39,6 +44,19 @@ module.exports = function(grunt) { }, src: ['test/*.js'], }, + markdown: { + all: { + files: [ + { + expand: true, + src: 'README.md', + dest: '.', + ext: '.html' + } + ] + } + } + }); diff --git a/README.md b/README.md index e7640cc..0397e9f 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ npm install -g grunt-cli grunt browserify ``` + #License **Code released under [the MIT license](https://github.com/bitpay/bitcore/blob/master/LICENSE).** diff --git a/package.json b/package.json index 1d1dfe4..6461a07 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "grunt-contrib-watch": "~0.5.3", "grunt-mocha-test": "~0.8.2", "grunt-browserify": "~1.3.0", + "grunt-markdown": "~0.5.0", "chai": "~1.9.0" }, "license": "MIT" From b308609ae6c57ab8a2c7116c80f8c755be14dcea Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 18 Feb 2014 04:48:48 -0300 Subject: [PATCH 2/4] add reject message handler --- Connection.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Connection.js b/Connection.js index 6e9e365..e04b039 100644 --- a/Connection.js +++ b/Connection.js @@ -526,6 +526,7 @@ function spec(b) { case 'getaddr': case 'verack': + case 'reject': // Empty message, nothing to parse break; From 398e39efb951bc3451dec2c7785e759d2a11a44e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 18 Feb 2014 04:49:35 -0300 Subject: [PATCH 3/4] add some examples to README and examples/ path --- README.md | 153 ++++++++++++++++++++++++++++++++++- examples/.Address.js.swo | Bin 0 -> 12288 bytes examples/.PeerManager.js.swo | Bin 0 -> 12288 bytes examples/Address.js | 23 ++++++ examples/PeerManager.js | 26 ++++++ examples/Rpc.js | 23 ++++++ examples/SendTx.js | 60 ++++++++++++++ 7 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 examples/.Address.js.swo create mode 100644 examples/.PeerManager.js.swo create mode 100644 examples/Address.js create mode 100644 examples/PeerManager.js create mode 100644 examples/Rpc.js create mode 100644 examples/SendTx.js diff --git a/README.md b/README.md index 0397e9f..6997676 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,12 @@ var Address = require('bitcore/Address').class(); #Examples +Some examples are provided at the [examples](/examples) path. Here are some snippets: + +## Address handling Validating a Bitcoin address: ``` var Address = require('bitcore/Address').class(); - var addr = new Address("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"); try { @@ -38,6 +40,155 @@ try { } ``` +## Monitoring Blocks and Transactions +``` +var networks = require('bitcore/networks'); +var Peer = require('bitcore/Peer').class(); +var PeerManager = require('bitcore/PeerManager').createClass({ + network: networks.livenet +}); + +var handleBlock = function(b) { + console.log('block received:', b); +}; +var handleTx = function(b) { + console.log('block tx:', b); +}; +var peerman = new PeerManager(); +peerman.addPeer( new Peer('62.75.216.13',8333) ); +peerman.addPeer( new Peer('62.75.253.91',8333) ); +peerman.on('connection', function(conn) { + conn.on('block', handleBlock); + 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) + + +## Create and send a Transaction thought P2P +``` +var networks = require('bitcore/networks'); +var Peer = require('bitcore/Peer').class(); +var Transaction = require('bitcore/Transaction').class(); +var Address = require('bitcore/Address').class(); +var Script = require('bitcore/Script').class(); +var coinUtil = require('bitcore/util/util'); +var PeerManager = require('bitcore/PeerManager').createClass({ + network: networks.testnet +}); + +var createTx = function() { + var TXIN='d05f35e0bbc495f6dcab03e599c8f5e32a07cdb4bc76964de201d06a2a7d8265'; + var TXIN_N=0; + var ADDR='muHct3YZ9Nd5Pq7uLYYhXRAxeW4EnpcaLz'; + var VAL='1.234'; + var txobj = {}; + txobj.version = 1; + txobj.lock_time = 0; + txobj.ins = []; + txobj.outs = []; + var txin = {}; + txin.s = coinUtil.EMPTY_BUFFER; //Add signature + txin.q = 0xffffffff; + + var hash = new Buffer(TXIN, 'hex'); + hash.reverse(); + 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'); } ); +}); +peerman.start(); +``` + +## RPC Client +``` +var RpcClient = require('../RpcClient').class(); +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) { + console.log(err); + console.log(util.inspect(ret, { depth: 10} )); +}); +``` +Check the list of all supported RPC call at [RpcClient.js](RpcClient.js) + +## Script Parsing + +Gets an address strings from a ScriptPubKey Buffer + +``` + var Address = require('bitcore/Address').class(); + var coinUtil= require('bitcore/util/util'); + + 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; + }; + + var s = new Script(scriptBuffer); + console.log(getAddrStr(s); + +``` + #Security Please use at your own risk. diff --git a/examples/.Address.js.swo b/examples/.Address.js.swo new file mode 100644 index 0000000000000000000000000000000000000000..928542addf8d928db5487b1ed542e143e808c89b GIT binary patch literal 12288 zcmeI2F>ljA6vwX}*b1l}SYOTHL?AmUw3S*hL@GjvwhE-cfEX_Jb8?k)Hg^}d0kz%v z1k8xf!H5JBp8*C2B)$Xi>^KZaMX2gh>7De~+4tW4eedTiR$jmLQQ^PRE0@F}uF@}BRqRe23hjNss zXv0n%lbH?n?I|X_%mkRg83`iT&*;P3m;e)C0!)AjFaajO1egF5 zxQGN?J||A-;Pdt2uhsM1UvquS6BA$pOn?b60Vco%m;e)C0!)AjFaaiT0SU-hh~a_| zUrFWv|FiG^Usr_qNqSFuM>-%Cq&{hbv`G4KS%}Z1Po$5ex1e7ro+oAByC+$tZh+6k3iP_0OZjcU=E3Hgvw zAx#yBJD~9Z>i+NQd_89%47DLt@3l9{`b4v~y?%bhM+XPn^+|Y8q^MQlkH>V%E%+k oQyC%T(hY$Od~g}~bzi*?DuGEh25At+mKcH?(z~wP?dg~H8*b~}l>h($ literal 0 HcmV?d00001 diff --git a/examples/.PeerManager.js.swo b/examples/.PeerManager.js.swo new file mode 100644 index 0000000000000000000000000000000000000000..8cd3240e1490bf957c8ac1718725282bcbfc8be1 GIT binary patch literal 12288 zcmeI2&1w@-6vuBA#6@ex?gZgfg-M{!G>zC&aiOjR3x2!4nK@~vozBENH)*7_?%n$c z1>GvRaG?*O8}SWX_yB&O{~K?jw5E`aP&gBQ%*>s0&z$qiD&#ch9^6=_h47-naZ;&E z$B*89|KaAbr_)ODhPG+PJvjJirG`tZjb#&NMXa+C;XA(*MhSO0i}E7jsF~WhFdT7D zZ?rQmqg%{oS?78Mxz@@NiDX#}h=K78bZqOw;-qTK*XP{Eb7yC0`qcgLa!9lo5CdXB z42S_SAO^&M7!U)8+JJ3W)Dv`Y#CLJppDTOL{#H(6Kn#chF(3xSfEW-1Vn7Ut0Wly3 z#K0jmpcAFePb&2sxc~nLzyE(uDfJHQf*tS>oB*mUNB3mI!`jb*K>RS(~j|96l-p&Mq9%$og{8HyGl84QvWKb&o6{?_|${h`eLKe zs1ee3wIQFHt_*|yNVhYtw!QJP$QbkDE51Ooy(JXT^f?BOkz`m?(-kywzu}nEE+_l5 z^jqGa$bb1c+s(qPFQM4}MHyO*v*V`Ui)+zOXg z>eg`QKeo8oew1%?@YX${gXI8WQABA@nv%5DV#B$mwkg)|g}EnpxSi>ksjclQp{{^z eL4hBv4=8QX Date: Wed, 19 Feb 2014 12:37:46 -0300 Subject: [PATCH 4/4] remove vim files --- .gitignore | 2 ++ examples/.Address.js.swo | Bin 12288 -> 0 bytes examples/.PeerManager.js.swo | Bin 12288 -> 0 bytes 3 files changed, 2 insertions(+) delete mode 100644 examples/.Address.js.swo delete mode 100644 examples/.PeerManager.js.swo diff --git a/.gitignore b/.gitignore index 5effac1..4aa6b97 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ browser/bundle.js browser/vendor.js node_modules/ *.swp +*.swo *~ .project +README.html diff --git a/examples/.Address.js.swo b/examples/.Address.js.swo deleted file mode 100644 index 928542addf8d928db5487b1ed542e143e808c89b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2F>ljA6vwX}*b1l}SYOTHL?AmUw3S*hL@GjvwhE-cfEX_Jb8?k)Hg^}d0kz%v z1k8xf!H5JBp8*C2B)$Xi>^KZaMX2gh>7De~+4tW4eedTiR$jmLQQ^PRE0@F}uF@}BRqRe23hjNss zXv0n%lbH?n?I|X_%mkRg83`iT&*;P3m;e)C0!)AjFaajO1egF5 zxQGN?J||A-;Pdt2uhsM1UvquS6BA$pOn?b60Vco%m;e)C0!)AjFaaiT0SU-hh~a_| zUrFWv|FiG^Usr_qNqSFuM>-%Cq&{hbv`G4KS%}Z1Po$5ex1e7ro+oAByC+$tZh+6k3iP_0OZjcU=E3Hgvw zAx#yBJD~9Z>i+NQd_89%47DLt@3l9{`b4v~y?%bhM+XPn^+|Y8q^MQlkH>V%E%+k oQyC%T(hY$Od~g}~bzi*?DuGEh25At+mKcH?(z~wP?dg~H8*b~}l>h($ diff --git a/examples/.PeerManager.js.swo b/examples/.PeerManager.js.swo deleted file mode 100644 index 8cd3240e1490bf957c8ac1718725282bcbfc8be1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&1w@-6vuBA#6@ex?gZgfg-M{!G>zC&aiOjR3x2!4nK@~vozBENH)*7_?%n$c z1>GvRaG?*O8}SWX_yB&O{~K?jw5E`aP&gBQ%*>s0&z$qiD&#ch9^6=_h47-naZ;&E z$B*89|KaAbr_)ODhPG+PJvjJirG`tZjb#&NMXa+C;XA(*MhSO0i}E7jsF~WhFdT7D zZ?rQmqg%{oS?78Mxz@@NiDX#}h=K78bZqOw;-qTK*XP{Eb7yC0`qcgLa!9lo5CdXB z42S_SAO^&M7!U)8+JJ3W)Dv`Y#CLJppDTOL{#H(6Kn#chF(3xSfEW-1Vn7Ut0Wly3 z#K0jmpcAFePb&2sxc~nLzyE(uDfJHQf*tS>oB*mUNB3mI!`jb*K>RS(~j|96l-p&Mq9%$og{8HyGl84QvWKb&o6{?_|${h`eLKe zs1ee3wIQFHt_*|yNVhYtw!QJP$QbkDE51Ooy(JXT^f?BOkz`m?(-kywzu}nEE+_l5 z^jqGa$bb1c+s(qPFQM4}MHyO*v*V`Ui)+zOXg z>eg`QKeo8oew1%?@YX${gXI8WQABA@nv%5DV#B$mwkg)|g}EnpxSi>ksjclQp{{^z eL4hBv4=8QX