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.).
"browser": true, // Standard browser globals e.g. `window`, `document`.
"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. `===`.
"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(){}() );`
"indent": 2, // Specify indentation spacing
"latedef": true, // Prohibit variable use before definition.
"newcap": false, // Require capitalization of all constructor functions e.g. `new F()`.
"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.
"regexp": true, // Prohibit `.` and `[^...]` in regular expressions.
"undef": true, // Require all non-global variables be declared before they are used.
"unused": true, // Warn unused variables.
"smarttabs": false, // Supress warnings about mixed tabs and spaces
"strict": true, // Require `use strict` pragma in every file.
"trailing": true, // Prohibit trailing whitespaces.
"smarttabs": false, // Suppresses warnings about mixed tabs and spaces
"globals": { // Globals variables.
"angular": true
},
"undef": true, // Require all non-global variables be declared before they are used.
"unused": true, // Warn unused variables.
"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.
"define",
"require",
@ -30,10 +39,6 @@
"beforeEach",
"after",
"afterEach",
"requirejs",
"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
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.
* from(utxo)
* fromMultisig(utxo, pubkeys, threshold)
* from(utxo, pubkeys, threshold)
* change(address)
* fee(amount)
* usingStrategy(strategy)

View File

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

View File

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

View File

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

View File

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

View File

@ -2,14 +2,13 @@
var should = require('chai').should();
var bitcore = require('../..');
var ECDSA = bitcore.ECDSA;
var Hash = bitcore.Hash;
var Keypair = bitcore.Keypair;
var ECDSA = bitcore.crypto.ECDSA;
var Hash = bitcore.crypto.Hash;
var PrivateKey = bitcore.PrivateKey;
var PublicKey = bitcore.PublicKey;
var Signature = bitcore.Signature;
var BN = bitcore.BN;
var point = bitcore.Point;
var BN = bitcore.crypto.BN;
var point = bitcore.crypto.Point;
describe('ECDSA', function() {
@ -20,12 +19,8 @@ describe('ECDSA', function() {
var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('test data'));
ecdsa.keypair = new Keypair();
ecdsa.keypair.privkey = new PrivateKey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))});
ecdsa.keypair.pubkey = new PublicKey({
point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')),
BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex')))
});
ecdsa.privkey = new PrivateKey(BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex')));
ecdsa.privkey2pubkey();
describe('#set', function() {
@ -49,13 +44,10 @@ describe('ECDSA', function() {
var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA();
ecdsa.keypair = new Keypair();
ecdsa.keypair.privkey = PrivateKey();
ecdsa.keypair.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test')));
ecdsa.keypair.privkey2pubkey();
ecdsa.privkey = PrivateKey(BN().fromBuffer(Hash.sha256(new Buffer('test'))));
ecdsa.privkey2pubkey();
ecdsa.hashbuf = hashbuf;
ecdsa.sig = new Signature({r: r, s: s});
ecdsa.calci();
ecdsa.sig.i.should.equal(1);
});
@ -69,7 +61,8 @@ describe('ECDSA', function() {
var ecdsa2 = new ECDSA();
ecdsa2.fromString(str);
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.sig.i = 1;
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'));
var pk = new PublicKey();
pk.fromDER(new Buffer('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.keypair = new Keypair();
ecdsa.keypair.pubkey = pk;
ecdsa.pubkey = pk;
ecdsa.sig = new Signature();
ecdsa.sig.r = 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() {
var ecdsa2 = ECDSA().set({
hashbuf: ecdsa.hashbuf.slice(0, 31),
keypair: ecdsa.keypair
pubkey: ecdsa.pubkey,
privkey: ecdsa.privkey
});
ecdsa2.randomK();
(function() {
@ -197,7 +190,7 @@ describe('ECDSA', function() {
describe('@sign', 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);
});
@ -206,10 +199,10 @@ describe('ECDSA', function() {
describe('@verify', function() {
it('should verify a valid signature, and unverify an invalid signature', function() {
var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.keypair);
ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.keypair.pubkey).should.equal(true);
var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey);
ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey).should.equal(true);
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();
var bitcore = require('../..');
var Hash = bitcore.Hash;
var Hash = bitcore.crypto.Hash;
describe('Hash', function() {
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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