Merge branch 'v0.8' into feature/keys-interface-rename

This commit is contained in:
Braydon Fuller 2014-11-25 17:37:24 -05:00
commit 747b14dc2e
16 changed files with 98 additions and 88 deletions

View File

@ -1,25 +1,34 @@
{ {
"node": true, // Enable globals available when code is running inside of the NodeJS runtime environment.
"browser": true, // Standard browser globals e.g. `window`, `document`.
"esnext": true, // Allow ES.next specific features such as `const` and `let`.
"bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.). "bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.).
"browser": true, // Standard browser globals e.g. `window`, `document`.
"camelcase": false, // Permit only camelcase for `var` and `object indexes`. "camelcase": false, // Permit only camelcase for `var` and `object indexes`.
"curly": false, // Require {} for every new block or scope. "curly": true, // Require {} for every new block or scope.
"devel": false, // Allow development statements e.g. `console.log();`.
"eqeqeq": true, // Require triple equals i.e. `===`. "eqeqeq": true, // Require triple equals i.e. `===`.
"esnext": true, // Allow ES.next specific features such as `const` and `let`.
"freeze": true, // Forbid overwriting prototypes of native objects such as Array, Date and so on.
"immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` "immed": true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
"indent": 2, // Specify indentation spacing
"latedef": true, // Prohibit variable use before definition. "latedef": true, // Prohibit variable use before definition.
"newcap": false, // Require capitalization of all constructor functions e.g. `new F()`. "newcap": false, // Require capitalization of all constructor functions e.g. `new F()`.
"noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`. "noarg": true, // Prohibit use of `arguments.caller` and `arguments.callee`.
"node": true, // Enable globals available when code is running inside of the NodeJS runtime environment.
"noempty": true, // Prohibit use of empty blocks.
"nonew": true, // Prohibits the use of constructor functions for side-effects
"quotmark": "single", // Define quotes to string values. "quotmark": "single", // Define quotes to string values.
"regexp": true, // Prohibit `.` and `[^...]` in regular expressions. "regexp": true, // Prohibit `.` and `[^...]` in regular expressions.
"undef": true, // Require all non-global variables be declared before they are used. "smarttabs": false, // Supress warnings about mixed tabs and spaces
"unused": true, // Warn unused variables.
"strict": true, // Require `use strict` pragma in every file. "strict": true, // Require `use strict` pragma in every file.
"trailing": true, // Prohibit trailing whitespaces. "trailing": true, // Prohibit trailing whitespaces.
"smarttabs": false, // Suppresses warnings about mixed tabs and spaces "undef": true, // Require all non-global variables be declared before they are used.
"globals": { // Globals variables. "unused": true, // Warn unused variables.
"angular": true
}, "maxparams": 4, // Maximum number of parameters for a function
"maxstatements": 15, // Maximum number of statements in a function
"maxcomplexity": 4, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity)
"maxdepth": 4, // Maximum depth of nested control structures
"maxlen": 600 // Maximum number of lines of code in a file
"predef": [ // Extra globals. "predef": [ // Extra globals.
"define", "define",
"require", "require",
@ -30,10 +39,6 @@
"beforeEach", "beforeEach",
"after", "after",
"afterEach", "afterEach",
"requirejs",
"it" "it"
], ]
"indent": false, // Specify indentation spacing
"devel": true, // Allow development statements e.g. `console.log();`.
"noempty": true // Prohibit use of empty blocks.
} }

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
browserify index.js -o browser/bitcore.js browserify index.js -o browser/bitcore.js
ls test/*.js | xargs browserify -o browser/tests.js find test/ -type f -name "*.js" | xargs browserify -o browser/tests.js

View File

@ -49,7 +49,7 @@ You can take a look at the javadocs for the [Transaction class here](link
missing). This document will go over the expected high level use cases. missing). This document will go over the expected high level use cases.
* from(utxo) * from(utxo)
* fromMultisig(utxo, pubkeys, threshold) * from(utxo, pubkeys, threshold)
* change(address) * change(address)
* fee(amount) * fee(amount)
* usingStrategy(strategy) * usingStrategy(strategy)

View File

@ -4,6 +4,7 @@ var BN = require('./bn');
var Point = require('./point'); var Point = require('./point');
var Random = require('./random'); var Random = require('./random');
var PublicKey = require('../publickey'); var PublicKey = require('../publickey');
var PrivateKey = require('../privatekey');
var Signature = require('../signature'); var Signature = require('../signature');
var ECDSA = function ECDSA(obj) { var ECDSA = function ECDSA(obj) {
@ -15,24 +16,29 @@ var ECDSA = function ECDSA(obj) {
ECDSA.prototype.set = function(obj) { ECDSA.prototype.set = function(obj) {
this.hashbuf = obj.hashbuf || this.hashbuf; this.hashbuf = obj.hashbuf || this.hashbuf;
this.keypair = obj.keypair || this.keypair; this.privkey = obj.privkey || this.privkey;
this.pubkey = obj.pubkey || this.pubkey;
this.sig = obj.sig || this.sig; this.sig = obj.sig || this.sig;
this.k = obj.k || this.k; this.k = obj.k || this.k;
this.verified = obj.verified || this.verified; this.verified = obj.verified || this.verified;
return this; return this;
}; };
ECDSA.prototype.privkey2pubkey = function(){
this.pubkey = PublicKey.fromPrivateKey(this.privkey);
};
ECDSA.prototype.calci = function() { ECDSA.prototype.calci = function() {
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
this.sig.i = i; this.sig.i = i;
var Qprime;
try { try {
var Qprime = this.sig2pubkey(); Qprime = this.sig2pubkey();
} catch (e) { } catch (e) {
continue; continue;
} }
if (Qprime.point.eq(this.pubkey.point)) {
if (Qprime.point.eq(this.keypair.pubkey.point)) { this.sig.compressed = this.pubkey.compressed;
this.sig.compressed = this.keypair.pubkey.compressed;
return this; return this;
} }
} }
@ -45,8 +51,10 @@ ECDSA.prototype.fromString = function(str) {
var obj = JSON.parse(str); var obj = JSON.parse(str);
if (obj.hashbuf) if (obj.hashbuf)
this.hashbuf = new Buffer(obj.hashbuf, 'hex'); this.hashbuf = new Buffer(obj.hashbuf, 'hex');
if (obj.keypair) if (obj.pubkey)
this.keypair = Keypair().fromString(obj.keypair); this.pubkey = PublicKey.fromString(obj.pubkey);
if (obj.privkey)
this.privkey = PrivateKey.fromString(obj.privkey);
if (obj.sig) if (obj.sig)
this.sig = Signature().fromString(obj.sig); this.sig = Signature().fromString(obj.sig);
if (obj.k) if (obj.k)
@ -106,7 +114,7 @@ ECDSA.prototype.sig2pubkey = function() {
//var Q = R.multiplyTwo(s, G, eNeg).mul(rInv); //var Q = R.multiplyTwo(s, G, eNeg).mul(rInv);
var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv); var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv);
var pubkey = new PublicKey({point: Q}); var pubkey = PublicKey.fromPoint(Q);
pubkey.compressed = this.sig.compressed; pubkey.compressed = this.sig.compressed;
pubkey.validate(); pubkey.validate();
@ -118,7 +126,7 @@ ECDSA.prototype.sigError = function() {
return 'hashbuf must be a 32 byte buffer'; return 'hashbuf must be a 32 byte buffer';
try { try {
this.keypair.pubkey.validate(); this.pubkey.validate();
} catch (e) { } catch (e) {
return 'Invalid pubkey: ' + e; return 'Invalid pubkey: ' + e;
} }
@ -135,7 +143,7 @@ ECDSA.prototype.sigError = function() {
var u1 = sinv.mul(e).mod(n); var u1 = sinv.mul(e).mod(n);
var u2 = sinv.mul(r).mod(n); var u2 = sinv.mul(r).mod(n);
var p = Point.getG().mulAdd(u1, this.keypair.pubkey.point, u2); var p = Point.getG().mulAdd(u1, this.pubkey.point, u2);
if (p.isInfinity()) if (p.isInfinity())
return 'p is infinity'; return 'p is infinity';
@ -147,7 +155,7 @@ ECDSA.prototype.sigError = function() {
ECDSA.prototype.sign = function() { ECDSA.prototype.sign = function() {
var hashbuf = this.hashbuf; var hashbuf = this.hashbuf;
var privkey = this.keypair.privkey; var privkey = this.privkey;
var k = this.k; var k = this.k;
var d = privkey.bn; var d = privkey.bn;
@ -170,7 +178,7 @@ ECDSA.prototype.sign = function() {
var s = k.invm(N).mul(e.add(d.mul(r))).mod(N); var s = k.invm(N).mul(e.add(d.mul(r))).mod(N);
} while (r.cmp(0) <= 0 || s.cmp(0) <= 0); } while (r.cmp(0) <= 0 || s.cmp(0) <= 0);
this.sig = new Signature({r: r, s: s, compressed: this.keypair.pubkey.compressed}); this.sig = new Signature({r: r, s: s, compressed: this.privkey.compressed});
return this.sig; return this.sig;
}; };
@ -183,6 +191,10 @@ ECDSA.prototype.toString = function() {
var obj = {}; var obj = {};
if (this.hashbuf) if (this.hashbuf)
obj.hashbuf = this.hashbuf.toString('hex'); obj.hashbuf = this.hashbuf.toString('hex');
if (this.pubkey)
obj.pubkey = this.pubkey.toString();
if (this.privkey)
obj.privkey = this.privkey.toString();
if (this.keypair) if (this.keypair)
obj.keypair = this.keypair.toString(); obj.keypair = this.keypair.toString();
if (this.sig) if (this.sig)
@ -199,10 +211,10 @@ ECDSA.prototype.verify = function() {
return false; return false;
}; };
ECDSA.sign = function(hashbuf, keypair) { ECDSA.sign = function(hashbuf, privkey) {
return ECDSA().set({ return ECDSA().set({
hashbuf: hashbuf, hashbuf: hashbuf,
keypair: keypair privkey: privkey
}).signRandomK(); }).signRandomK();
}; };
@ -210,7 +222,7 @@ ECDSA.verify = function(hashbuf, sig, pubkey) {
return ECDSA().set({ return ECDSA().set({
hashbuf: hashbuf, hashbuf: hashbuf,
sig: sig, sig: sig,
keypair: Keypair().set({pubkey: pubkey}) pubkey: pubkey
}).verify(); }).verify();
}; };

View File

@ -30,7 +30,7 @@ Base58.encode = function(buf) {
Base58.decode = function(str) { Base58.decode = function(str) {
if (typeof str !== 'string') if (typeof str !== 'string')
throw new Error('Input should be a string'); throw new Error('Input should be a string');
return bs58.decode(str); return new Buffer(bs58.decode(str));
}; };
Base58.prototype.fromBuffer = function(buf) { Base58.prototype.fromBuffer = function(buf) {

View File

@ -5,8 +5,8 @@
"author": "BitPay <dev@bitpay.com>", "author": "BitPay <dev@bitpay.com>",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "mocha --reporter spec", "test": "mocha --recursive --reporter spec",
"coverage": "istanbul cover _mocha" "coverage": "istanbul cover _mocha -- --recursive"
}, },
"contributors": [ "contributors": [
{ {

View File

@ -2,7 +2,7 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
describe('BN', function() { describe('BN', function() {
it('should create a bn', function() { it('should create a bn', function() {

View File

@ -2,14 +2,13 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var ECDSA = bitcore.ECDSA; var ECDSA = bitcore.crypto.ECDSA;
var Hash = bitcore.Hash; var Hash = bitcore.crypto.Hash;
var Keypair = bitcore.Keypair;
var PrivateKey = bitcore.PrivateKey; var PrivateKey = bitcore.PrivateKey;
var PublicKey = bitcore.PublicKey; var PublicKey = bitcore.PublicKey;
var Signature = bitcore.Signature; var Signature = bitcore.Signature;
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
var point = bitcore.Point; var point = bitcore.crypto.Point;
describe('ECDSA', function() { describe('ECDSA', function() {
@ -20,12 +19,8 @@ describe('ECDSA', function() {
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test data')); ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
ecdsa.keypair = new Keypair(); ecdsa.privkey = new PrivateKey(BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')));
ecdsa.keypair.privkey = new PrivateKey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))}); ecdsa.privkey2pubkey();
ecdsa.keypair.pubkey = new PublicKey({
point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')),
BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex')))
});
describe('#set', function() { describe('#set', function() {
@ -49,13 +44,10 @@ describe('ECDSA', function() {
var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10); var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10); var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.keypair = new Keypair(); ecdsa.privkey = PrivateKey(BN().fromBuffer(Hash.sha256(new Buffer('test'))));
ecdsa.keypair.privkey = PrivateKey(); ecdsa.privkey2pubkey();
ecdsa.keypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test')));
ecdsa.keypair.privkey2pubkey();
ecdsa.hashbuf = hashbuf; ecdsa.hashbuf = hashbuf;
ecdsa.sig = new Signature({r: r, s: s}); ecdsa.sig = new Signature({r: r, s: s});
ecdsa.calci(); ecdsa.calci();
ecdsa.sig.i.should.equal(1); ecdsa.sig.i.should.equal(1);
}); });
@ -69,7 +61,8 @@ describe('ECDSA', function() {
var ecdsa2 = new ECDSA(); var ecdsa2 = new ECDSA();
ecdsa2.fromString(str); ecdsa2.fromString(str);
should.exist(ecdsa.hashbuf); should.exist(ecdsa.hashbuf);
should.exist(ecdsa.keypair); should.exist(ecdsa.pubkey);
should.exist(ecdsa.privkey);
}); });
}); });
@ -100,7 +93,7 @@ describe('ECDSA', function() {
ecdsa.sign(); ecdsa.sign();
ecdsa.sig.i = 1; ecdsa.sig.i = 1;
var pubkey = ecdsa.sig2pubkey(); var pubkey = ecdsa.sig2pubkey();
pubkey.point.eq(ecdsa.keypair.pubkey.point).should.equal(true); pubkey.point.eq(ecdsa.pubkey.point).should.equal(true);
}); });
}); });
@ -123,8 +116,7 @@ describe('ECDSA', function() {
ecdsa.hashbuf = Hash.sha256(new Buffer('test')); ecdsa.hashbuf = Hash.sha256(new Buffer('test'));
var pk = new PublicKey(); var pk = new PublicKey();
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.keypair = new Keypair(); ecdsa.pubkey = pk;
ecdsa.keypair.pubkey = pk;
ecdsa.sig = new Signature(); ecdsa.sig = new Signature();
ecdsa.sig.r = BN(0); ecdsa.sig.r = BN(0);
ecdsa.sig.s = BN(0); ecdsa.sig.s = BN(0);
@ -151,7 +143,8 @@ describe('ECDSA', function() {
it('should should throw an error if hashbuf is not 32 bytes', function() { it('should should throw an error if hashbuf is not 32 bytes', function() {
var ecdsa2 = ECDSA().set({ var ecdsa2 = ECDSA().set({
hashbuf: ecdsa.hashbuf.slice(0, 31), hashbuf: ecdsa.hashbuf.slice(0, 31),
keypair: ecdsa.keypair pubkey: ecdsa.pubkey,
privkey: ecdsa.privkey
}); });
ecdsa2.randomK(); ecdsa2.randomK();
(function() { (function() {
@ -197,7 +190,7 @@ describe('ECDSA', function() {
describe('@sign', function() { describe('@sign', function() {
it('should produce a signature', function() { it('should produce a signature', function() {
var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.keypair); var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey);
(sig instanceof Signature).should.equal(true); (sig instanceof Signature).should.equal(true);
}); });
@ -206,10 +199,10 @@ describe('ECDSA', function() {
describe('@verify', function() { describe('@verify', function() {
it('should verify a valid signature, and unverify an invalid signature', function() { it('should verify a valid signature, and unverify an invalid signature', function() {
var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.keypair); var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey);
ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.keypair.pubkey).should.equal(true); ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey).should.equal(true);
var fakesig = Signature(sig.r.add(1), sig.s); var fakesig = Signature(sig.r.add(1), sig.s);
ECDSA.verify(ecdsa.hashbuf, fakesig, ecdsa.keypair.pubkey).should.equal(false); ECDSA.verify(ecdsa.hashbuf, fakesig, ecdsa.pubkey).should.equal(false);
}); });
}); });

View File

@ -2,7 +2,7 @@
require('chai').should(); require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var Hash = bitcore.Hash; var Hash = bitcore.crypto.Hash;
describe('Hash', function() { describe('Hash', function() {
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);

View File

@ -2,8 +2,8 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var point = bitcore.Point; var point = bitcore.crypto.Point;
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
describe('Point', function() { describe('Point', function() {

View File

@ -2,7 +2,7 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var Random = bitcore.Random; var Random = bitcore.crypto.Random;
describe('Random', function() { describe('Random', function() {

View File

@ -2,7 +2,7 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var Base58 = bitcore.Base58; var Base58 = bitcore.encoding.Base58;
describe('Base58', function() { describe('Base58', function() {
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);

View File

@ -2,8 +2,8 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var Base58Check = bitcore.Base58Check; var Base58Check = bitcore.encoding.Base58Check;
var base58 = bitcore.Base58; var base58 = bitcore.encoding.Base58;
describe('Base58Check', function() { describe('Base58Check', function() {
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);

View File

@ -2,9 +2,9 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var BufferWriter = bitcore.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
var BufferReader = bitcore.BufferReader; var BufferReader = bitcore.encoding.BufferReader;
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
describe('BufferReader', function() { describe('BufferReader', function() {

View File

@ -2,9 +2,9 @@
var bitcore = require('../..'); var bitcore = require('../..');
var should = require('chai').should(); var should = require('chai').should();
var BufferWriter = bitcore.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
var BufferReader = bitcore.BufferReader; var BufferReader = bitcore.encoding.BufferReader;
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
describe('BufferWriter', function() { describe('BufferWriter', function() {

View File

@ -2,10 +2,10 @@
var should = require('chai').should(); var should = require('chai').should();
var bitcore = require('../..'); var bitcore = require('../..');
var BN = bitcore.BN; var BN = bitcore.crypto.BN;
var BufferReader = bitcore.BufferReader; var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.BufferWriter; var BufferWriter = bitcore.encoding.BufferWriter;
var Varint = bitcore.Varint; var Varint = bitcore.encoding.Varint;
describe('Varint', function() { describe('Varint', function() {