diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fce8114 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - "0.10" + +script: "mocha" diff --git a/Address.js b/Address.js index b9a4ed1..c3bf0ee 100644 --- a/Address.js +++ b/Address.js @@ -1,7 +1,9 @@ 'use strict'; var imports = require('soop').imports(); -var parent = imports.parent || require('./util/VersionedData'); -var networks= imports.networks || require('./networks'); +var coinUtil = imports.coinUtil || require('./util/util'); +var parent = imports.parent || require('./util/VersionedData'); +var networks = imports.networks || require('./networks'); +var Script = imports.Script || require('./Script'); function Address() { Address.super(this, arguments); @@ -10,6 +12,47 @@ function Address() { Address.parent = parent; parent.applyEncodingsTo(Address); +//create a pubKeyHash address +Address.fromPubKey = function(pubKey, network) { + if (!network) + network = 'livenet'; + + if (pubKey.length != 33 && pubKey.length != 65) + throw new Error('Invalid public key'); + + var version = networks[network].addressVersion; + var hash = coinUtil.sha256ripe160(pubKey); + + return new Address(version, hash); +}; + +//create a p2sh m-of-n multisig address +Address.fromPubKeys = function(mReq, pubKeys, network, opts) { + if (!network) + network = 'livenet'; + + for (var i in pubKeys) { + var pubKey = pubKeys[i]; + if (pubKey.length != 33 && pubKey.length != 65) + throw new Error('Invalid public key'); + } + + var script = Script.createMultisig(mReq, pubKeys, opts); + + return Address.fromScript(script, network); +}; + +//create a p2sh address from redeemScript +Address.fromScript = function(script, network) { + if (!network) + network = 'livenet'; + + var version = networks[network].P2SHVersion; + var buf = script.getBuffer(); + var hash = coinUtil.sha256ripe160(buf); + + return new Address(version, hash); +}; Address.prototype.validate = function() { this.doAsBinary(function() { diff --git a/Connection.js b/Connection.js index f273ec3..319a723 100644 --- a/Connection.js +++ b/Connection.js @@ -186,10 +186,15 @@ Connection.prototype.sendVersion = function () { }; Connection.prototype.sendGetBlocks = function (starts, stop, wantHeaders) { + // Default value for stop is 0 to get as many blocks as possible (500) + stop = stop || util.NULL_HASH; + var put = new Put(); - put.word32le(this.sendVer); + // https://en.bitcoin.it/wiki/Protocol_specification#getblocks + put.word32le(this.sendVer); put.varint(starts.length); + for (var i = 0; i < starts.length; i++) { if (starts[i].length != 32) { throw new Error('Invalid hash length'); @@ -443,8 +448,8 @@ Connection.prototype.parseMessage = function (command, payload) { data.headers = []; for (i = 0; i < data.count; i++) { var header = new Block(); -header.parse(parser); -data.headers.push(header); + header.parse(parser); + data.headers.push(header); } break; diff --git a/PeerManager.js b/PeerManager.js index c6e2029..d822986 100644 --- a/PeerManager.js +++ b/PeerManager.js @@ -111,6 +111,9 @@ PeerManager.prototype.addConnection = function(socketConn, peer) { }; PeerManager.prototype.handleVersion = function(e) { + e.peer.version = e.message.version; + e.peer.start_height = e.message.start_height; + if (!e.conn.inbound) { // TODO: Advertise our address (if listening) } @@ -132,7 +135,7 @@ PeerManager.prototype.handleReady = function (e) { }); if(this.isConnected == false) { - this.emit('netConnected'); + this.emit('netConnected', e); this.isConnected = true; } }; diff --git a/README.md b/README.md index b2a9396..1cd568f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ Bitcore ======= +[](https://travis-ci.org/bitpay/bitcore) + A pure, powerful core for your bitcoin project. Bitcore is a complete, native interface to the Bitcoin network, and provides the core functionality needed to develop apps for bitcoin. diff --git a/browser/build.js b/browser/build.js index 43ae350..941d520 100644 --- a/browser/build.js +++ b/browser/build.js @@ -64,7 +64,10 @@ var createBitcore = function(opts) { opts.dir = opts.dir || ''; // concat browser vendor files - exec('cd ' + opts.dir + 'browser; sh concat.sh', puts); + var cwd = process.cwd(); + process.chdir(opts.dir + 'browser'); + exec('sh concat.sh', puts); + process.chdir(cwd); if (!opts.includeall && (!opts.submodules || opts.submodules.length === 0)) { if (!opts.stdout) console.log('Must use either -s or -a option. For more info use the --help option'); diff --git a/examples/CreateAndSignTx-PayToScriptHash.js b/examples/CreateAndSignTx-PayToScriptHash.js index 8b035ee..f80593c 100644 --- a/examples/CreateAndSignTx-PayToScriptHash.js +++ b/examples/CreateAndSignTx-PayToScriptHash.js @@ -41,10 +41,10 @@ var run = function() { }); // multisig p2sh - var opts = {nreq:3, pubkeys:pubkeys, amount:0.05}; + var opts = {nreq:3, pubkeys:pubkeys}; // p2scriphash p2sh - //var opts = [{address: an_address, amount:0.05}]; + //var opts = [{address: an_address}]; var info = Builder.infoForP2sh(opts, 'testnet'); var p2shScript = info.scriptBufHex; diff --git a/examples/PayToScriptHashAddress.js b/examples/PayToScriptHashAddress.js new file mode 100644 index 0000000..846d6d2 --- /dev/null +++ b/examples/PayToScriptHashAddress.js @@ -0,0 +1,17 @@ +var bitcore = require('../bitcore'); +var Address = bitcore.Address; +var bitcoreUtil = bitcore.util; +var Script = bitcore.Script; +var network = bitcore.networks.livenet; + + +var script = ''; // write down your script here +var s = Script.fromHumanReadable(script); +var hash = bitcoreUtil.sha256ripe160(s.getBuffer()); +var version = network.addressScript; + +var addr = new Address(version, hash); +var addrStr = addr.as('base58'); + +// This outputs the "address" of thescript +console.log(addrStr); diff --git a/examples/SimpleP2Pmonitor.js b/examples/SimpleP2Pmonitor.js new file mode 100644 index 0000000..1791b9b --- /dev/null +++ b/examples/SimpleP2Pmonitor.js @@ -0,0 +1,72 @@ +/** + * This is a simple script that will display network messages. + * It users the Peer / Connection classes * directly instead of + * relying on PeerManager. + */ + +// replace by require('bitcore') if you use somewhere else +var bitcore = require('../'); + +//bitcore.config.logger = 'debug'; + +var Peer = bitcore.Peer, + Connection = bitcore.Connection; + +var peer = new Peer('127.0.0.1', 8333); + +var socket = peer.createConnection(); + +var con = new Connection(socket, peer); + +con.on('error', function (msg) { + var peer = msg.peer, err = msg.err; + console.error('Error connecting to peer', peer.host + ':' + peer.port, '(' + err.message + ')'); +}); + +con.on('disconnect', function (msg) { + console.log('disconnect: ', msg); +}); + +con.on('connect', function (msg) { + console.log('Connected to %s', msg.peer.host + ':' + msg.peer.port); +}); + +/* Listen P2P messages */ + +// Make a log function available to all listeners +// The log function is just like console.log except it prefixes +// messages with [host:port] +function listen (event_name, fn) { + con.on(event_name, function (event) { + fn(event, function () { + var args = Array.prototype.slice.call(arguments); + var str = args.shift(); + str = '[%s:%s] ' + str; + args = [ str, event.peer.host, event.peer.port ].concat(args); + console.log.apply(console, args); + }); + }); +} + +listen('getaddr', function (event, log) { + log('Received message getaddr'); + log(event); +}); + +listen('verack', function (event, log) { + log('Received message verack'); +}); + +listen('version', function (event, log) { + log('Received message version (%s)', event.message.version); +}); + +listen('addr', function (event, log) { + log('Received message addr (%s addresses)', event.message.addrs.length); +}); + +listen('inv', function (event, log) { + log('Received message inv (%s invs)', event.message.count); + console.log(event.message.invs); +}); + diff --git a/examples/browser/README.md b/examples/browser/README.md new file mode 100644 index 0000000..6dcc8b6 --- /dev/null +++ b/examples/browser/README.md @@ -0,0 +1 @@ +Run `node browser/build.js -a` in the repository's root directory before using those examples. diff --git a/examples/example.html b/examples/browser/example.html similarity index 94% rename from examples/example.html rename to examples/browser/example.html index 74a1f64..b0914b3 100644 --- a/examples/example.html +++ b/examples/browser/example.html @@ -9,12 +9,12 @@
- + - +