Skip to content

Commit

Permalink
support soop with browser all test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Mar 8, 2014
1 parent c05181e commit 089fbab
Show file tree
Hide file tree
Showing 32 changed files with 228 additions and 81 deletions.
28 changes: 15 additions & 13 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
module.exports = function(grunt) {

//Load NPM tasks
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-markdown');
grunt.loadNpmTasks('grunt-shell');

// Project Configuration
grunt.initConfig({
browserify: {
client: {
src: ['bitcore.js'],
dest: 'browser/bundle.js',
shell: {
browserify: {
options: {
debug: true,
alias: [
'browserify-bignum/bignumber.js:bignum',
'browserify-buffertools/buffertools.js:buffertools'
],
standalone: 'bitcore',
}
stdout: true
},
command: 'node ./browserify.js > browser/bundle.js',
},
browserifyData: {
options: {
stdout: true
},
command: 'browserify -t brfs test/testdata.js > browser/testdata.js'
},
},
browserify: {
test_data: {
src: ['test/testdata.js'],
dest: 'browser/testdata.js',
Expand All @@ -40,7 +42,7 @@ module.exports = function(grunt) {
},
scripts: {
files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!browser/bundle.js', '!browser/testdata.js'],
tasks: ['browserify' /*, 'mochaTest'*/ ],
tasks: ['shell' /*, 'mochaTest'*/ ],
},
},
mochaTest: {
Expand Down
6 changes: 4 additions & 2 deletions PeerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ var imports = require('soop').imports();
var config = imports.config || require('./config');
var log = imports.log || require('./util/log');
var network = imports.network || require('./networks')[config.network];
var Connection = imports.Connection ||
require('soop').load('./Connection', {config: config, network: network});

var Connection = imports.Connection ||
require('soop').load('Connection', {config: config, network: network}) ||
require ('./Connection');

var Peer = imports.Peer || require('./Peer');

Expand Down
14 changes: 12 additions & 2 deletions bitcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
*/



module.exports.bignum = require('bignum');
module.exports.base58 = require('base58-native');
module.exports.buffertools = require('buffertools');

module.exports.config = require('./config');
module.exports.const = require('./const');
module.exports.Deserialize = require('./Deserialize');
module.exports.log = require('./util/log');
module.exports.networks = require('./networks');
module.exports.util = require('./util/util');

module.exports.EncodedData = require('./util/EncodedData');
module.exports.VersionedData = require('./util/VersionedData');
module.exports.Address = require('./Address');
module.exports.Opcode = require('./Opcode');
module.exports.Script = require('./Script');
module.exports.Transaction = require('./Transaction');
module.exports.Connection = require('./Connection');
module.exports.Peer = require('./Peer');
module.exports.PeerManager = require('./PeerManager');
module.exports.Block = require('./Block');
module.exports.Connection = require('./Connection');
module.exports.ScriptInterpreter = require('./ScriptInterpreter');
module.exports.Bloom = require('./Bloom');
module.exports.KeyModule = require('./Key');
Expand All @@ -34,6 +36,14 @@ module.exports.WalletKey = require('./WalletKey');
module.exports.Buffer = Buffer;

if (typeof process.versions === 'undefined') {
// Browser specific
module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1});
// module.exports.PeerManager = function () {
// throw new Error('PeerManager not availabe in browser Bitcore, under .bitcore. Use it with: require(\'PeerManager\');');
// };
}
else {
// Nodejs specific
module.exports.PeerManager = require('./PeerManager');
}

6 changes: 6 additions & 0 deletions browser/bignum_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('bignum').config({
EXPONENTIAL_AT: 9999999,
DECIMAL_PLACES: 0,
ROUNDING_MODE: 1,
});

88 changes: 88 additions & 0 deletions browserify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

/*
* Example for usage of browserify with soop
*
* The key parameter 'pack'
* The supplied 'custom_prelude.js' file is needed for
* .load function of soop.
*/

var fs = require('fs');
var browserify = require('browserify');
var browserPack = require('browser-pack');
var opts = {};


var preludePath = 'node_modules/soop/example/custom_prelude.js';

var pack = function (params) {
params.raw = true;
params.sourceMapPrefix = '//#';
params.prelude= fs.readFileSync(preludePath, 'utf8');
params.preludePath= preludePath;
return browserPack(params);
};

opts.pack = pack;
opts.debug = true;

var modules = [
'Address',
'Block',
'Bloom',
'Buffers.monkey',
'Connection',
'Deserialize',
'Gruntfile',
'Number.monkey',
'Opcode',
'Peer',
'PeerManager',
'PrivateKey',
'RpcClient',
'SIN',
'SINKey',
'Script',
'ScriptInterpreter',
'Sign',
'Transaction',
'Wallet',
'WalletKey',
'config',
'const',
'networks',
'bitcore',
];

var b = browserify(opts);
b.require('browserify-bignum/bignumber.js', {expose: 'bignum'} );
b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'});
b.require('buffer', {expose: 'buffer'});
b.require('base58-native');
b.require('./Key.js', {expose: 'KeyModule'});
b.require('./util/log');
b.require('./util/util');
b.require('./util/EncodedData');
b.require('./util/VersionedData');
b.add('./browser/bignum_config.js');

modules.forEach(function(m) {
b.require('./' + m + '.js' ,{expose:m} );
});

var bopts = {
// detectGlobals: true,
// insertGlobals: 'Buffer',
// insertGlobalVars: {
// Buffer: function () {
// return 'require("buffer").Buffer';
// },
// },
};

b.bundle(bopts).pipe(process.stdout);




37 changes: 28 additions & 9 deletions examples/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id='content'></div>
<pre>
<div id='content'></div>
</pre>
<script src="../browser/bundle.js"></script>

<script type="text/javascript">
var Address = require('Address');

print = function(s){
var div = document.getElementById('content');
div.innerHTML += s + '<br />';
};

var Address = bitcore.Address;

var addrStrings = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx",
Expand All @@ -37,15 +38,33 @@
}

});
print('<hr>');


var Key = bitcore.KeyModule.Key;
var Key = require('KeyModule').Key;
var buffertools = require('buffertools');
var k = Key.generateSync();

print ('Generated Key Pair:');
print ('Private:' + bitcore.buffertools.toHex(k.private));
print ('Public:' + bitcore.buffertools.toHex(k.public));

print ('Generate Key Pair:');
print ('Private:' + buffertools.toHex(k.private));
print ('Public:' + buffertools.toHex(k.public));

print('<hr>');
/*
Using bitcore root module
*/

var bitcore = require('bitcore');
var k = bitcore.KeyModule.Key.generateSync();

print ('Generate Key Pair:');
print ('Private:' + buffertools.toHex(k.private));
print ('Public:' + buffertools.toHex(k.public));

print('<hr>');

console.log('[example.html.65:PeerManager:]'); //TODO

var pm = require('PeerManager');

</script>
</body>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"devDependencies": {
"grunt-contrib-watch": "~0.5.3",
"grunt-mocha-test": "~0.8.2",
"grunt-browserify": "~1.3.0",
"grunt-shell": "~0.6.4",
"browser-pack": "*",
"grunt-markdown": "~0.5.0",
"mocha": ">=1.15.1",
"browserify-bignum": "git://github.com/maraoz/browserify-bignum.git",
Expand Down
9 changes: 8 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
<script>mocha.setup('bdd')</script>
<script src="../browser/bundle.js"></script>
<script src="../browser/testdata.js"></script>
<script src="adapter.js"></script>

<script>
var bitcore = require('bitcore');
this.Buffer = require('buffer').Buffer;
</script>


<!-- <script src="adapter.js"></script> -->

<script src="test.Address.js"></script>
<script src="test.basic.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions test/test.Address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
4 changes: 2 additions & 2 deletions test/test.Block.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');
var should = chai.should();

var BlockModule = bitcore.Block;
Expand Down
4 changes: 2 additions & 2 deletions test/test.Bloom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
4 changes: 2 additions & 2 deletions test/test.Connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
4 changes: 2 additions & 2 deletions test/test.EncodedData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
4 changes: 2 additions & 2 deletions test/test.Key.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var buffertools = require('buffertools');

Expand Down
4 changes: 2 additions & 2 deletions test/test.Opcode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
4 changes: 2 additions & 2 deletions test/test.Peer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down
7 changes: 4 additions & 3 deletions test/test.PeerManager.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

var PeerManagerModule = bitcore.PeerManager;
var PeerManagerModule = bitcore.PeerManager || require('PeerManager');

var PeerManager;

describe('PeerManager', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/test.PrivateKey.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var chai = require('chai');
var bitcore = require('../bitcore');
var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');

var should = chai.should();

Expand Down

0 comments on commit 089fbab

Please sign in to comment.