fix conflicts

This commit is contained in:
Matias Alejo Garcia 2014-03-12 11:56:10 -03:00
commit dc8d23dba5
29 changed files with 190 additions and 2957 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
build/ build/
browser/bundle.js browser/bundle.js
browser/vendor-bundle.js
browser/testdata.js browser/testdata.js
node_modules/ node_modules/
*.swp *.swp

View File

@ -9,6 +9,7 @@ function Address() {
Address.parent = parent; Address.parent = parent;
parent.applyEncodingsTo(Address); parent.applyEncodingsTo(Address);
Address.prototype.validate = function() { Address.prototype.validate = function() {
this.doAsBinary(function() { this.doAsBinary(function() {
Address.super(this, 'validate', arguments); Address.super(this, 'validate', arguments);
@ -16,4 +17,9 @@ Address.prototype.validate = function() {
}); });
}; };
Address.prototype.isValid = function() {
var answer = Address.super(this, 'isValid', arguments);
return answer;
};
module.exports = require('soop')(Address); module.exports = require('soop')(Address);

View File

@ -16,7 +16,7 @@ module.exports = function(grunt) {
stdout: true, stdout: true,
stderr: true stderr: true
}, },
command: 'node ./browser/browserify.js', command: 'node ./browser/browserify.js -a',
} }
}, },
watch: { watch: {
@ -25,7 +25,7 @@ module.exports = function(grunt) {
tasks: ['markdown'] tasks: ['markdown']
}, },
scripts: { scripts: {
files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!browser/bundle.js', '!browser/testdata.js'], files: ['**/*.js', '**/*.html', '!**/node_modules/**', '!browser/bundle.js', '!browser/testdata.js', '!browser/vendor-bundle.js'],
tasks: ['shell'], tasks: ['shell'],
}, },
}, },

7
Key.js
View File

@ -5,7 +5,7 @@ if (process.versions) {
module.exports = require('bindings')('KeyModule'); module.exports = require('bindings')('KeyModule');
} else { } else {
// pure js version // pure js version
var ECKey = require('./browser/bitcoinjs-lib.js').ECKey; var ECKey = require('./browser/vendor-bundle.js').ECKey;
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bufferToArray = function(buffer) { var bufferToArray = function(buffer) {
@ -89,10 +89,11 @@ if (process.versions) {
} }
var eck = new ECKey(); var eck = new ECKey();
eck.setPub( bufferToArray(self.public)); eck.setPub(bufferToArray(self.public));
eck.setCompressed(self.compressed); eck.setCompressed(self.compressed);
var sigA = bufferToArray(sig); var sigA = bufferToArray(sig);
return eck.verify(hash,sigA); var ret = eck.verify(hash,sigA);
return ret;
}; };

View File

@ -31,26 +31,19 @@ Validating a Bitcoin address:
```js ```js
var Address = require('bitcore/Address'); var Address = require('bitcore/Address');
var addrStrings = [ var addrs = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx',
"A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1600 Pennsylvania Ave NW", '1600 Pennsylvania Ave NW',
].map(function(addr) { ].map(function(addr) {
return new Address(addr); return new Address(addr);
}); });
addrStrings.forEach(function(addr) { addrs.forEach(function(addr) {
var valid = addr.isValid();
try { console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
addr.validate();
console.log(addr.data + ": is valid");
} catch(e) {
console.log(addr.data + ": is not a valid address. " + e);
}
}); });
``` ```
## Monitoring Blocks and Transactions ## Monitoring Blocks and Transactions
For this example you need a running bitcoind instance with RPC enabled. For this example you need a running bitcoind instance with RPC enabled.
@ -264,13 +257,49 @@ Bitcore is still under heavy development and not quite ready for "drop-in" produ
Bitcore needs some developer love. Please send pull requests for bug fixes, code optimization, and ideas for improvement. Bitcore needs some developer love. Please send pull requests for bug fixes, code optimization, and ideas for improvement.
#Browser support #Browser support
Work to enable Bitcore for use in the browser is ongoing. To build bitcore for the browser: ## Building the browser bundle
To build bitcore full bundle for the browser:
(this is automatically executed after you run `npm install`)
``` ```
npm install -g grunt-cli node browser/browserify.js -a
grunt shell ```
This will generate a `browser/bundle.js` file which you can include
in your HTML to use bitcore in the browser.
##
##Example browser usage
From example/simple.html
```
<!DOCTYPE html>
<html>
<body>
<script src="../browser/bundle.js"></script>
<script>
var bitcore = require('bitcore');
var Address = bitcore.Address;
var a = new Address('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d');
console.log('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d is valid? '+a.isValid());
</script>
</body>
</html>
``` ```
You can check a usage example at examples/example.html You can check a more complex usage example at examples/example.html
## Generating a customized browser bundle
To generate a customized bitcore bundle, you can specify
which submodules you want to include in it with the -s option:
```
node browser/browserify.js -s Transaction,Address
```
This will generate a `browser/bundle.js` containing only the Transaction
and Address class, with all their dependencies.
Use this option if you are not using the whole bitcore library, to optimize
the bundle size, script loading time, and general resource usage.
#License #License

View File

@ -1,2 +0,0 @@
Bitcoin = {};

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
if ('undefined' === typeof window) window = this;

View File

@ -10,7 +10,33 @@
var fs = require('fs'); var fs = require('fs');
var browserify = require('browserify'); var browserify = require('browserify');
var browserPack = require('browser-pack'); var browserPack = require('browser-pack');
var program = require('commander');
// concat browser vendor files
var exec = require('child_process').exec;
var sys = require('sys');
var puts = function(error, stdout, stderr) {
if (error) console.log(error);
sys.puts(stdout);
sys.puts(stderr);
};
exec('cd browser; sh concat.sh', puts);
var list = function(val) {
return val.split(',');
};
program
.version('0.0.1')
.option('-a, --includeall', 'Include all submodules.')
.option('-s, --submodules <items>', 'Include the listed comma-separated submodules.', list)
.parse(process.argv);
if (!program.includeall && (!program.submodules || program.submodules.length === 0)) {
console.log('Must use either -s or -a option. For more info use the --help option');
process.exit(1);
}
var pack = function (params) { var pack = function (params) {
var preludePath = 'node_modules/soop/example/custom_prelude.js'; var preludePath = 'node_modules/soop/example/custom_prelude.js';
@ -64,9 +90,11 @@ b.require('./util/log');
b.require('./util/util'); b.require('./util/util');
b.require('./util/EncodedData'); b.require('./util/EncodedData');
b.require('./util/VersionedData'); b.require('./util/VersionedData');
b.add('./browser/bignum_config.js');
modules.forEach(function(m) { modules.forEach(function(m) {
b.require('./' + m + '.js' ,{expose: './'+m} ); if (program.includeall || program.submodules.indexOf(m) > -1) {
console.log('Including '+m+' in the browser bundle');
b.require('./' + m + '.js' , {expose: './'+m} );
}
}); });
b.require('soop'); b.require('soop');

View File

@ -1,3 +1,7 @@
#! /bin/bash #! /bin/bash
cat browser.js crypto.js ripemd160.js bitcoin.js navigator-adapter.js jsbn.js jsbn2.js prng4.js util.js rng.js ec.js sec.js ecdsa.js eckey.js > bitcoinjs-lib.js cd vendor/
cat browser-adapter.js crypto.js ripemd160.js jsbn.js jsbn2.js prng4.js util.js rng.js ec.js sec.js ecdsa.js eckey.js > vendor-bundle.js
mv vendor-bundle.js ../
cd ../

View File

@ -1,7 +1,6 @@
if ('undefined' === typeof window) window = this;
Bitcoin = {};
if (typeof navigator === 'undefined') { if (typeof navigator === 'undefined') {
var navigator = {}; var navigator = {};
navigator.appName = 'NodeJS'; navigator.appName = 'NodeJS';
} }

View File

@ -41,24 +41,34 @@ ECPointFp.prototype.getEncoded = function (compressed) {
return enc; return enc;
}; };
ECPointFp.decodeFrom = function (curve, enc) { ECPointFp.decodeFrom = function (ecparams, enc) {
var type = enc[0]; var type = enc[0];
var dataLen = enc.length-1; var dataLen = enc.length-1;
// Extract x and y as byte arrays // Extract x and y as byte arrays
var xBa = enc.slice(1, 1 + dataLen/2); if (type === 4) {
var yBa = enc.slice(1 + dataLen/2, 1 + dataLen); var xBa = enc.slice(1, 1 + dataLen/2),
yBa = enc.slice(1 + dataLen/2, 1 + dataLen),
// Prepend zero byte to prevent interpretation as negative integer x = BigInteger.fromByteArrayUnsigned(xBa),
xBa.unshift(0); y = BigInteger.fromByteArrayUnsigned(yBa);
yBa.unshift(0); }
else {
// Convert to BigIntegers var xBa = enc.slice(1),
var x = new BigInteger(xBa); x = BigInteger.fromByteArrayUnsigned(xBa),
var y = new BigInteger(yBa); p = ecparams.getQ(),
xCubedPlus7 = x.multiply(x).multiply(x).add(new BigInteger('7')).mod(p),
pPlus1Over4 = p.add(new BigInteger('1'))
.divide(new BigInteger('4')),
y = xCubedPlus7.modPow(pPlus1Over4,p);
if (y.mod(new BigInteger('2')).toString() != ''+(type % 2)) {
y = p.subtract(y)
}
}
// Return point // Return point
return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y)); return new ECPointFp(ecparams,
ecparams.fromBigInteger(x),
ecparams.fromBigInteger(y));
}; };
ECPointFp.prototype.add2D = function (b) { ECPointFp.prototype.add2D = function (b) {

View File

@ -29,5 +29,17 @@ f+(((m|~n)^p)+c[2]):64>b?f+((m&p|n&~p)+c[3]):f+((m^(n|~p))+c[4]),f|=0,f=f<<k[b]|
e[a>>>5]|=128<<24-a%32;e[(a+64>>>9<<4)+14]=(b<<8|b>>>24)&16711935|(b<<24|b>>>8)&4278255360;g.sigBytes=4*(e.length+1);this._process();g=this._hash;e=g.words;for(b=0;5>b;b++)a=e[b],e[b]=(a<<8|a>>>24)&16711935|(a<<24|a>>>8)&4278255360;return g},clone:function(){var e=l.clone.call(this);e._hash=this._hash.clone();return e}});j.RIPEMD160=l._createHelper(k);j.HmacRIPEMD160=l._createHmacHelper(k)})(Math); e[a>>>5]|=128<<24-a%32;e[(a+64>>>9<<4)+14]=(b<<8|b>>>24)&16711935|(b<<24|b>>>8)&4278255360;g.sigBytes=4*(e.length+1);this._process();g=this._hash;e=g.words;for(b=0;5>b;b++)a=e[b],e[b]=(a<<8|a>>>24)&16711935|(a<<24|a>>>8)&4278255360;return g},clone:function(){var e=l.clone.call(this);e._hash=this._hash.clone();return e}});j.RIPEMD160=l._createHelper(k);j.HmacRIPEMD160=l._createHmacHelper(k)})(Math);
module.exports.RIPEMD160 = CryptoJS.RIPEMD160; module.exports.ripemd160 = function(bytes) {
module.exports.WordArray = CryptoJS.lib.WordArray; if (!Buffer.isBuffer(bytes)) {
throw new Error('arg should be a buffer');
}
var w = new CryptoJS.lib.WordArray.init(Crypto.util.bytesToWords(bytes), bytes.length);
var wordArray = CryptoJS.RIPEMD160(w);
var words = wordArray.words;
var answer = [];
for (var b = 0; b < words.length * 32; b += 8) {
answer.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
}
return answer;
};

View File

@ -4,22 +4,16 @@
var Address = require('../Address'); var Address = require('../Address');
var addrStrings = [ var addrs = [
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", '1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx',
"A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", 'A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
"1600 Pennsylvania Ave NW", '1600 Pennsylvania Ave NW',
].map(function(addr) { ].map(function(addr) {
return new Address(addr); return new Address(addr);
}); });
addrStrings.forEach(function(addr) { addrs.forEach(function(addr) {
var valid = addr.isValid();
try { console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
addr.validate();
console.log(addr.data + ": is valid");
} catch(e) {
console.log(addr.data + ": is not a valid address. " + e);
}
}); });

12
examples/simple.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<body>
<script src="../browser/bundle.js"></script>
<script>
var bitcore = require('bitcore');
var Address = bitcore.Address;
var a = new Address('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d');
console.log('1KerhGhLn3SYBEQwby7VyVMWf16fXQUj5d is valid? '+a.isValid());
</script>
</body>
</html>

View File

@ -1 +0,0 @@
bitcore.js

View File

@ -36,12 +36,14 @@
"currency", "currency",
"virtual" "virtual"
], ],
"main": "bitcore.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/bitpay/bitcore.git" "url": "https://github.com/bitpay/bitcore.git"
}, },
"scripts": { "scripts": {
"test": "mocha test -R spec" "test": "mocha test -R spec",
"postinstall": "node ./browser/browserify.js -a"
}, },
"dependencies": { "dependencies": {
"soop": "git://github.com/bitpay/soop.git", "soop": "git://github.com/bitpay/soop.git",
@ -66,7 +68,8 @@
"browserify-buffertools": "~1.0.2", "browserify-buffertools": "~1.0.2",
"chai": "~1.9.0", "chai": "~1.9.0",
"brfs": "~1.0.0", "brfs": "~1.0.0",
"async": "~0.2.10" "async": "~0.2.10",
"commander": "~2.1.0"
}, },
"license": "MIT" "license": "MIT"
} }

View File

@ -38,6 +38,8 @@
<script src="test.VersionedData.js"></script> <script src="test.VersionedData.js"></script>
<script src="test.Wallet.js"></script> <script src="test.Wallet.js"></script>
<script src="test.WalletKey.js"></script> <script src="test.WalletKey.js"></script>
<!--
-->
<script> <script>
mocha.run(); mocha.run();
</script> </script>

View File

@ -20,17 +20,31 @@ describe('Address', function() {
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T'); var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
should.exist(a); should.exist(a);
}); });
it('should validate correctly', function() { var data = [
var a = new Address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
var m = new Address('32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6'); ['11111111111111111111111111122222234', false], // totally invalid
var b = new Address('11111111111111111111111111122222234'); ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true],
a.validate.bind(a).should.not.throw(Error); ['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true],
m.validate.bind(m).should.not.throw(Error); ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true],
b.validate.bind(b).should.throw(Error); ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum
['1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // data changed, original checksum.
['1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // invalid chars
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j', false], // checksums don't match.
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!', false], // bad char (!)
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz', false], // too long Bitcoin address
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false],// too long Bitcoin address
['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data
];
data.forEach(function(datum) {
var address = datum[0];
var result = datum[1];
it('should validate correctly ' + address, function() {
var a = new Address(address);
var s = a.toString();
a.isValid().should.equal(result);
s.should.equal(a.toString()); // check that validation doesn't change data
});
}); });
}); });

View File

@ -4,10 +4,10 @@ var bignum = require('bignum');
var Binary = require('binary'); var Binary = require('binary');
var Put = require('bufferput'); var Put = require('bufferput');
var buffertools = require('buffertools'); var buffertools = require('buffertools');
var bjs; var browser;
if (!process.versions) { if (!process.versions) {
// browser version // browser version
bjs = require('../browser/bitcoinjs-lib.js'); browser = require('../browser/vendor-bundle.js');
} }
@ -21,7 +21,7 @@ var ripe160 = exports.ripe160 = function (data) {
} }
if (!process.versions) { if (!process.versions) {
var result = bjs.ripemd160(data); var result = browser.ripemd160(data);
return new Buffer(result, 'hex'); return new Buffer(result, 'hex');
} }
return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary'); return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');