bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.86k stars 2.09k forks source link

require('soop') fails in the browser #197

Closed brianosaurus closed 10 years ago

brianosaurus commented 10 years ago

When I try to require soop in the browser (chrome) it fails (from the JS console):

require('bitcore') Object {Buffer: function, bignum: (...), base58: (...), bufferput: (...), buffertools: (...)…}

require('soop') TypeError: boolean is not a function

ryanxcharles commented 10 years ago

Soop isn't really designed for the browser. Bitcore has its own way of compiling soop for the browser. Are you writing a project that uses both soop and bitcore? Have you compiled them both separately for the browser?

brianosaurus commented 10 years ago

I was just going through the example on bitcore.io to listen for block, tx, and inv messages. It's the example that begins with:

var bitcore = require('bitcore');
var networks = bitcore.networks;
var Peer = bitcore.Peer;
var PeerManager = require('soop').load('bitcore/PeerManager', {
  network: networks.testnet
});
matiu commented 10 years ago

Please check:

https://github.com/bitpay/bitcore/blob/master/examples/example.html

ryanxcharles commented 10 years ago

You can only use PeerManager from node, not the browser.

maraoz commented 10 years ago

@brianosaurus You might need to create a little node service that connects to a bitcoind instance and relays messages via websockets to the browser. Check https://github.com/bitpay/insight-api/blob/master/lib/PeerSync.js for the p2p connection to bitcoind stuff, and https://github.com/bitpay/insight-api/blob/master/app/controllers/socket.js for a simple example of using socket.io to broadcast the messages to the browser

maraoz commented 10 years ago

I'll make some tests regarding using only the browser to connect to bitcoind, but I'm not sure it's possible :)

maraoz commented 10 years ago

OK, I created a html example using PeerManager and couldn't make it work. It runs, but the Peer never connects. https://github.com/maraoz/bitcore/blob/sample/PeerManager-browser/examples/PeerManager.html

After digging a little, I realized it seems that browserify cannot automatically convert the 'net' module. So for now, PeerManager is not usable in the browser. The only solution I see is trying to implement the 'net' module for the browser, but I don't know if that's even possible. There's seems to be someone attempting that already, but the repo is empty: https://github.com/jryans/net-browserify