From 0eff7d44168a5e2186aa25e19e46320d5c751608 Mon Sep 17 00:00:00 2001 From: Paul Kernfeld Date: Fri, 5 Feb 2016 18:51:58 +0100 Subject: [PATCH] Make the user pass in a WebRTC implementation (for #7) --- bin/bridge.js | 1 + bin/wallet.js | 1 + lib/bridge.js | 2 +- lib/node.js | 1 + lib/peerGroup.js | 18 ++++-------------- package.json | 3 --- test/blockchain.js | 7 ++++--- test/common.js | 2 ++ test/node.js | 2 ++ test/wallet.js | 3 ++- 10 files changed, 18 insertions(+), 22 deletions(-) create mode 100644 test/common.js diff --git a/bin/bridge.js b/bin/bridge.js index ec8bb9f..a041158 100755 --- a/bin/bridge.js +++ b/bin/bridge.js @@ -14,6 +14,7 @@ var argv = require('minimist')(process.argv.slice(2)) var bridge = new Bridge({ network: argv.testnet ? Networks.testnet : Networks.livenet, + wrtc: require('wrtc'), localPeer: argv.local }) diff --git a/bin/wallet.js b/bin/wallet.js index 30cb91e..4ac8aa7 100755 --- a/bin/wallet.js +++ b/bin/wallet.js @@ -5,6 +5,7 @@ var Node = require('../lib/node.js') var node = new Node({ network: Networks.livenet, + wrtc: require('wrtc'), path: 'data', acceptWeb: true }) diff --git a/lib/bridge.js b/lib/bridge.js index fc9d00c..dcbfe03 100644 --- a/lib/bridge.js +++ b/lib/bridge.js @@ -17,7 +17,7 @@ var Bridge = module.exports = function (opts) { webSeeds.forEach(function (uri) { debug('bridge connecting to web seed', uri) - var client = new PeerhubClient(uri, function () { + var client = new PeerhubClient(uri, { wrtc: opts.wrtc }, function () { debug('bridge connected to web seed', uri) self.webSeeds.push(client) self.emit('seedconnect', client) diff --git a/lib/node.js b/lib/node.js index e50e0ad..128318e 100644 --- a/lib/node.js +++ b/lib/node.js @@ -24,6 +24,7 @@ var Node = module.exports = function (opts, cb) { this.peers = opts.peers || new PeerGroup({ network: this.network, + wrtc: opts.wrtc, acceptWeb: opts.accept || opts.acceptWeb, acceptTcp: opts.accept || opts.acceptTcp, getTip: function () { return self.chain.getTip() }, diff --git a/lib/peerGroup.js b/lib/peerGroup.js index 73d4cb7..271b530 100644 --- a/lib/peerGroup.js +++ b/lib/peerGroup.js @@ -14,17 +14,6 @@ var u = require('./utils.js') var webSeeds = require('./constants.js').webSeeds -var supportsWebRTC = false -if (process.browser) { - var RTCPC = window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.RTCPeerConnection - supportsWebRTC = !!RTCPC -} else { - try { - require('wrtc') - supportsWebRTC = true - } catch (e) {} -} - // HACK: suppress warnings from Buffer#get() Buffer.prototype.get = function get (offset) { return this.readUInt8(offset) @@ -35,6 +24,7 @@ var commands = Object.keys((new Builder()).commandsMap) var PeerGroup = module.exports = function (opts) { opts = opts || {} this.network = Networks.get(opts.network) || Networks.defaultNetwork + this.wrtc = opts.wrtc this.acceptTcp = opts.acceptTcp || false this.acceptWeb = opts.acceptWeb || false this.tcpCount = opts.tcpCount != null ? opts.tcpCount : 5 @@ -173,10 +163,10 @@ PeerGroup.prototype.connect = function (opts, cb) { }) } - if (supportsWebRTC) { + if (self.wrtc) { tasks.push(function (cb) { async.each(webSeeds, function (uri, cb) { - var client = new PeerhubClient(uri, function () { + var client = new PeerhubClient(uri, { wrtc: self.wrtc }, function () { self.webSeeds.push(client) self.emit('seedconnect', client) self._connectToWebPeers() @@ -271,7 +261,7 @@ PeerGroup.prototype._onWebPeerConnect = function (conn, incoming) { } PeerGroup.prototype.acceptWebPeers = function () { - if (!supportsWebRTC) throw new Error('WebRTC is not supported') + if (!this.wrtc) throw new Error('WebRTC is not supported') this.webSeeds.forEach(this._acceptFromPeerhub.bind(this)) this.acceptWeb = true } diff --git a/package.json b/package.json index c826c0b..2301fb0 100644 --- a/package.json +++ b/package.json @@ -47,9 +47,6 @@ "buffertools": "browserify-buffertools", "bitcore-p2p": "bitcore-p2p-browserify" }, - "optionalDependencies": { - "wrtc": "0.0.58" - }, "repository": { "type": "git", "url": "https://github.com/mappum/webcoin" diff --git a/test/blockchain.js b/test/blockchain.js index 53d3c1a..6992870 100644 --- a/test/blockchain.js +++ b/test/blockchain.js @@ -6,6 +6,7 @@ var BlockStore = require('../lib/blockStore.js') var Blockchain = require('../lib/blockchain.js') var u = require('../lib/utils.js') var constants = require('../lib/constants.js') +var common = require('./common.js') try { var leveldown = require('leveldown') @@ -49,7 +50,7 @@ test('creating blockchain instances', function (t) { t.test('create blockchain with instantiated BlockStore', function (t) { t.doesNotThrow(function () { - var peers = new PeerGroup() + var peers = new PeerGroup({wrtc: common.wrtc}) var store = new BlockStore({ path: storePath }) var chain = new Blockchain({ peerGroup: peers, store: store }) endStore(chain.store, t) @@ -58,7 +59,7 @@ test('creating blockchain instances', function (t) { t.test('create blockchain with path instead of BlockStore', function (t) { t.doesNotThrow(function () { - var peers = new PeerGroup() + var peers = new PeerGroup({wrtc: common.wrtc}) var chain = new Blockchain({ peerGroup: peers, path: storePath }) endStore(chain.store, t) }) @@ -397,7 +398,7 @@ test('blockchain queries', function (t) { }) test('blockchain sync', function (t) { - var peers = new PeerGroup() + var peers = new PeerGroup({wrtc: common.wrtc}) peers.on('error', function (err) { console.error(err) }) peers.connect() diff --git a/test/common.js b/test/common.js new file mode 100644 index 0000000..f947832 --- /dev/null +++ b/test/common.js @@ -0,0 +1,2 @@ +// Feel free to substitute in an implementation of your preference. +module.exports.wrtc = require('wrtc') diff --git a/test/node.js b/test/node.js index d90fe1d..eb54ae9 100644 --- a/test/node.js +++ b/test/node.js @@ -1,6 +1,7 @@ var test = require('tape') var Networks = require('bitcore-lib').Networks var Node = require('../lib/node.js') +var common = require('./common.js') test('Node constructor', function (t) { var HEIGHT = 1000 @@ -10,6 +11,7 @@ test('Node constructor', function (t) { t.plan(1) node = new Node({ network: Networks.testnet, + wrtc: common.wrtc, path: 'data/' + process.pid, to: HEIGHT }) diff --git a/test/wallet.js b/test/wallet.js index d2cabf3..8a474c5 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -1,7 +1,7 @@ var test = require('tape') var Networks = require('bitcore-lib').Networks var Node = require('../lib/node.js') -// var Wallet = require('../lib/wallet.js') +var common = require('./common.js') test('Node wallet creation', function (t) { var node @@ -9,6 +9,7 @@ test('Node wallet creation', function (t) { t.test('setup', function (t) { node = new Node({ network: Networks.testnet, + wrtc: common.wrtc, path: 'data/' + process.pid, to: 1000 })