Add autogenerated errors

This commit is contained in:
Esteban Ordano 2014-11-29 18:23:38 -03:00
parent 803027353a
commit 13fd2592fc
10 changed files with 222 additions and 214 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ node_modules
browser/bitcore.js
browser/tests.js
lib/errors/index.js
CONTRIBUTING.html
LICENSE.html

View File

@ -44,13 +44,13 @@ var testKarma = shell.task([
]);
gulp.task('test', testMocha);
gulp.task('test', ['errors'], testMocha);
gulp.task('test-all', function(callback) {
gulp.task('test-all', ['errors'], function(callback) {
runSequence(['test'], ['karma'], callback);
});
gulp.task('test-nofail', function() {
gulp.task('test-nofail', ['errors'], function() {
return testMocha().on('error', ignoreError);
});
@ -93,7 +93,7 @@ gulp.task('lint', function() {
.pipe(jshint.reporter('default'));
});
gulp.task('browser', function() {
gulp.task('browser', ['errors'], function() {
return gulp.src('index.js')
.pipe(browserify({
insertGlobals: true
@ -106,13 +106,17 @@ gulp.task('browser-test', shell.task([
'find test/ -type f -name "*.js" | xargs browserify -o ./browser/tests.js'
]));
gulp.task('browser-all', function(callback) {
gulp.task('browser-all', ['errors'], function(callback) {
runSequence(['browser'], ['browser-test'], callback);
});
gulp.task('karma', testKarma);
gulp.task('minify', function() {
gulp.task('errors', shell.task([
'node ./lib/errors/build.js'
]));
gulp.task('minify', ['errors'], function() {
return gulp.src('dist/bitcore.js')
.pipe(closureCompiler({
fileName: 'bitcore.min.js',
@ -126,5 +130,8 @@ gulp.task('minify', function() {
});
gulp.task('default', function(callback) {
return runSequence(['lint', 'jsdoc', 'browser', 'test'], ['coverage', 'minify'], callback);
return runSequence(['lint', 'jsdoc'],
['browser', 'test'],
['coverage', 'minify'],
callback);
});

View File

@ -23,6 +23,8 @@ bitcore.util.bitcoin = require('./lib/util/bitcoin');
bitcore.util.buffer = require('./lib/util/buffer');
bitcore.util.js = require('./lib/util/js');
bitcore.errors = require('./lib/errors');
// main bitcoin library
bitcore.Address = require('./lib/address');
bitcore.Block = require('./lib/block');

55
lib/errors/build.js Normal file
View File

@ -0,0 +1,55 @@
'use strict';
var _ = require('lodash');
var fs = require('fs');
var formatMessage = function(message) {
message = '\'' + message + '\'';
for (var i = 0; i < 3; i++) {
message += '.replace(\'{' + i + '}\', arguments[' + i + '])';
}
return message;
};
var defineElement = function(fullName, baseClass, message) {
return fullName + ' = function() {\n' +
' this.message = ' + formatMessage(message) + ';\n' +
' ' + baseClass + '.call(this, this.message);\n' +
' this.name = "' + fullName + '";\n' +
'};\n' +
'inherits(' + fullName + ', ' + baseClass + ');\n\n';
};
var traverseNode = function(baseClass, errorDefinition) {
var className = baseClass + '.' + errorDefinition.name;
var generated = defineElement(className, baseClass, errorDefinition.message);
if (errorDefinition.errors) {
generated += childDefinitions(className, errorDefinition.errors);
}
return generated;
};
/* jshint latedef: false */
var childDefinitions = function(parent, childDefinitions) {
var generated = '';
_.each(childDefinitions, function(childDefinition) {
generated += traverseNode(parent, childDefinition);
});
return generated;
};
/* jshint latedef: true */
var traverseRoot = function(errorsDefinition) {
var fullName = 'bitcore.Error';
var path = 'Error';
var generated = '\'use strict\';\n\nvar inherits = require(\'inherits\');\n\n';
generated += '/** AUTOGENERATED FILE. DON\'T EDIT, MODIFY "lib/errors/spec.js" INSTEAD */\n\n';
generated += 'var bitcore = {};\n\n';
generated += defineElement(fullName, path, 'Internal error');
generated += childDefinitions(fullName, errorsDefinition);
generated += 'module.exports = bitcore.Error;\n';
return generated;
};
var data = require('./spec');
fs.writeFileSync(__dirname + '/index.js', traverseRoot(data));

85
lib/errors/spec.js Normal file
View File

@ -0,0 +1,85 @@
module.exports = [{
name: 'HDPrivateKey',
message: 'Internal Error on HDPrivateKey {0}',
errors: [
{
name: 'InvalidArgument',
message: 'HDPrivateKey: Invalid Argument {0}, expected {1} but got {2}',
errors: [{
name: 'InvalidB58Char',
message: 'Invalid Base58 character: {0} in {1}'
}, {
name: 'InvalidB58Checksum',
message: 'Invalid Base58 checksum for {0}'
}, {
name: 'InvalidDerivationArgument',
message: 'Invalid derivation argument {0}, expected string, or number and boolean'
}, {
name: 'InvalidEntropyArgument',
message: 'Invalid entropy: must be an hexa string or binary buffer, got {0}',
errors: [{
name: 'TooMuchEntropy',
message: 'Invalid entropy: more than 512 bits is non standard, got "{0}"'
}, {
name: 'NotEnoughEntropy',
message: 'Invalid entropy: at least 128 bits needed, got "{0}"'
}]
}, {
name: 'InvalidLength',
message: 'Invalid length for xprivkey string in {0}'
}, {
name: 'InvalidNetwork',
message: 'Invalid version for network: got {0}'
}, {
name: 'InvalidNetworkArgument',
message: 'Invalid network: must be "livenet" or "testnet", got {0}'
}, {
name: 'InvalidPath',
message: 'Invalid derivation path: {0}'
}, {
name: 'UnrecognizedArgument',
message: 'Invalid argument: creating a HDPrivateKey requires a string, buffer, json or object, got "{0}"'
}]
}
]
}, {
name: 'HDPublicKey',
message: 'Internal Error on HDPublicKey {0}',
errors: [
{
name: 'InvalidArgument',
message: 'HDPublicKey: Invalid Argument {0}, expected {1} but got {2}',
errors: [{
name: 'ArgumentIsPrivateExtended',
message: 'Argument is an extended private key: {0}'
}, {
name: 'InvalidB58Char',
message: 'Invalid Base58 character: {0} in {1}'
}, {
name: 'InvalidB58Checksum',
message: 'Invalid Base58 checksum for {0}'
}, {
name: 'InvalidDerivationArgument',
message: 'Invalid derivation argument: got {0}'
}, {
name: 'InvalidLength',
message: 'Invalid length for xpubkey: got "{0}"'
}, {
name: 'InvalidNetwork',
message: 'Invalid network, expected a different version: got "{0}"'
}, {
name: 'InvalidNetworkArgument',
message: 'Expected network to be "livenet" or "testnet", got "{0}"'
}, {
name: 'InvalidPath',
message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"'
}, {
name: 'MustSupplyArgument',
message: 'Must supply an argument to create a HDPublicKey'
}, {
name: 'UnrecognizedArgument',
message: 'Invalid argument for creation, must be string, json, buffer, or object'
}]
}
]
}];

View File

@ -15,7 +15,8 @@ var Point = require('./crypto/point');
var PrivateKey = require('./privatekey');
var Random = require('./crypto/random');
var inherits = require('inherits');
var bitcoreErrors = require('./errors');
var errors = bitcoreErrors.HDPrivateKey.InvalidArgument;
var bufferUtil = require('./util/buffer');
var jsUtil = require('./util/js');
@ -53,7 +54,7 @@ function HDPrivateKey(arg) {
if (_.isObject(arg)) {
this._buildFromObject(arg);
} else {
throw new HDPrivateKey.Error.UnrecognizedArgument(arg);
throw new errors.UnrecognizedArgument(arg);
}
}
} else {
@ -88,7 +89,7 @@ HDPrivateKey.prototype.derive = function(arg, hardened) {
} else if (_.isString(arg)) {
return this._deriveFromString(arg);
} else {
throw new HDPrivateKey.Error.InvalidDerivationArgument(arg);
throw new errors.InvalidDerivationArgument(arg);
}
};
@ -139,7 +140,7 @@ HDPrivateKey.prototype._deriveFromString = function(path) {
return this;
}
if (!_.contains(HDPrivateKey.RootElementAlias, steps[0])) {
throw new HDPrivateKey.Error.InvalidPath(path);
throw new errors.InvalidPath(path);
}
steps = steps.slice(1);
@ -172,23 +173,23 @@ HDPrivateKey.isValidSerialized = function(data, network) {
* @param {string|Buffer} data - the serialized private key
* @param {string|Network=} network - optional, if present, checks that the
* network provided matches the network serialized.
* @return {HDPrivateKey.Error.InvalidArgument|null}
* @return {errors.InvalidArgument|null}
*/
HDPrivateKey.getSerializedError = function(data, network) {
/* jshint maxcomplexity: 10 */
if (!(_.isString(data) || bufferUtil.isBuffer(data))) {
return new HDPrivateKey.Error.InvalidArgument('Expected string or buffer');
return new errors.UnrecognizedArgument('Expected string or buffer');
}
if (!Base58.validCharacters(data)) {
return new HDPrivateKey.Error.InvalidB58Char('(unknown)', data);
return new errors.InvalidB58Char('(unknown)', data);
}
try {
data = Base58Check.decode(data);
} catch (e) {
return new HDPrivateKey.Error.InvalidB58Checksum(data);
return new errors.InvalidB58Checksum(data);
}
if (data.length !== HDPrivateKey.DataLength) {
return new HDPrivateKey.Error.InvalidLength(data);
return new errors.InvalidLength(data);
}
if (!_.isUndefined(network)) {
var error = HDPrivateKey._validateNetwork(data, network);
@ -202,11 +203,11 @@ HDPrivateKey.getSerializedError = function(data, network) {
HDPrivateKey._validateNetwork = function(data, networkArg) {
var network = Network.get(networkArg);
if (!network) {
return new HDPrivateKey.Error.InvalidNetworkArgument(networkArg);
return new errors.InvalidNetworkArgument(networkArg);
}
var version = data.slice(0, 4);
if (bufferUtil.integerFromBuffer(version) !== network.xprivkey) {
return new HDPrivateKey.Error.InvalidNetwork(version);
return new errors.InvalidNetwork(version);
}
return null;
};
@ -264,13 +265,13 @@ HDPrivateKey.fromSeed = function(hexa, network) {
hexa = bufferUtil.hexToBuffer(hexa);
}
if (!Buffer.isBuffer(hexa)) {
throw new HDPrivateKey.Error.InvalidEntropyArgument(hexa);
throw new errors.InvalidEntropyArgument(hexa);
}
if (hexa.length < MINIMUM_ENTROPY_BITS * BITS_TO_BYTES) {
throw new HDPrivateKey.Error.NotEnoughEntropy(hexa);
throw new errors.InvalidEntropyArgument.NotEnoughEntropy(hexa);
}
if (hexa.length > MAXIMUM_ENTROPY_BITS * BITS_TO_BYTES) {
throw new HDPrivateKey.Error.TooMuchEntropy(hexa);
throw new errors.InvalidEntropyArgument.TooMuchEntropy(hexa);
}
var hash = Hash.sha512hmac(hexa, new buffer.Buffer('Bitcoin seed'));
@ -316,7 +317,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
arg.checksum = Base58Check.checksum(concat);
} else {
if (arg.checksum.toString() !== Base58Check.checksum(concat).toString()) {
throw new HDPrivateKey.Error.InvalidB58Checksum(concat);
throw new errors.InvalidB58Checksum(concat);
}
}
@ -447,80 +448,4 @@ HDPrivateKey.ChecksumEnd = HDPrivateKey.ChecksumStart + HDPrivateKey.
assert(HDPrivateKey.ChecksumEnd === HDPrivateKey.SerializedByteSize);
HDPrivateKey.Error = function() {
Error.apply(this, arguments);
};
inherits(HDPrivateKey.Error, Error);
HDPrivateKey.Error.InvalidArgument = function(message) {
HDPrivateKey.Error.apply(this, arguments);
this.message = 'Invalid argument: ' + message;
};
inherits(HDPrivateKey.Error.InvalidArgument, TypeError);
HDPrivateKey.Error.InvalidB58Char = function(character, string) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid Base 58 character: ' + character + ' in "' + string + '"';
};
inherits(HDPrivateKey.Error.InvalidB58Char, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidB58Checksum = function(message) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid Base 58 checksum in "' + message + '"';
};
inherits(HDPrivateKey.Error.InvalidB58Checksum, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidDerivationArgument = function(args) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid derivation argument "' + args + '", expected number and boolean or string';
};
inherits(HDPrivateKey.Error.InvalidDerivationArgument, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidEntropyArgument = function(message) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid argument: entropy must be an hexa string or binary buffer, got ' + typeof message;
};
inherits(HDPrivateKey.Error.InvalidEntropyArgument, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidLength = function(message) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid length for xprivkey format in "' + message + '"';
};
inherits(HDPrivateKey.Error.InvalidLength, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidNetwork = function(network) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Unexpected version for network: got ' + network;
};
inherits(HDPrivateKey.Error.InvalidNetwork, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidNetworkArgument = function(message) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Network argument must be \'livenet\' or \'testnet\', got "' + message + '"';
};
inherits(HDPrivateKey.Error.InvalidNetworkArgument, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.InvalidPath = function(message) {
HDPrivateKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid path for derivation "' + message + '", must start with "m"';
};
inherits(HDPrivateKey.Error.InvalidPath, HDPrivateKey.Error.InvalidArgument);
HDPrivateKey.Error.NotEnoughEntropy = function(message) {
HDPrivateKey.Error.InvalidEntropyArgument.apply(this, arguments);
this.message = 'Need more than 128 bytes of entropy, got ' + message.length + ' in "' + message + '"';
};
inherits(HDPrivateKey.Error.NotEnoughEntropy, HDPrivateKey.Error.InvalidEntropyArgument);
HDPrivateKey.Error.TooMuchEntropy = function(message) {
HDPrivateKey.Error.InvalidEntropyArgument.apply(this, arguments);
this.message = 'More than 512 bytes of entropy is non standard, got ' + message.length + ' in "' + message + '"';
};
inherits(HDPrivateKey.Error.TooMuchEntropy, HDPrivateKey.Error.InvalidEntropyArgument);
HDPrivateKey.Error.UnrecognizedArgument = function(message) {
this.message = 'Creating a HDPrivateKey requires a string, a buffer, a json, or an object, got "' + message + '" of type "' + typeof message + '"';
};
inherits(HDPrivateKey.Error.UnrecognizedArgument, HDPrivateKey.Error.InvalidArgument);
module.exports = HDPrivateKey;

View File

@ -11,8 +11,9 @@ var Network = require('./networks');
var Point = require('./crypto/point');
var PublicKey = require('./publickey');
var bitcoreErrors = require('./errors');
var errors = bitcoreErrors.HDPublicKey.InvalidArgument;
var assert = require('assert');
var inherits = require('inherits');
var jsUtil = require('./util/js');
var bufferUtil = require('./util/buffer');
@ -42,7 +43,7 @@ function HDPublicKey(arg) {
} else if (jsUtil.isValidJson(arg)) {
return this._buildFromJson(arg);
} else {
if (error instanceof HDPublicKey.Error.ArgumentIsPrivateExtended) {
if (error instanceof errors.ArgumentIsPrivateExtended) {
return new HDPrivateKey(arg).hdPublicKey;
}
throw error;
@ -55,11 +56,11 @@ function HDPublicKey(arg) {
return this._buildFromObject(arg);
}
} else {
throw new HDPublicKey.Error.UnrecognizedArgument(arg);
throw new errors.UnrecognizedArgument(arg);
}
}
} else {
throw new HDPublicKey.Error.MustSupplyArgument();
throw new errors.MustSupplyArgument();
}
}
@ -90,13 +91,13 @@ HDPublicKey.prototype.derive = function (arg, hardened) {
} else if (_.isString(arg)) {
return this._deriveFromString(arg);
} else {
throw new HDPublicKey.Error.InvalidDerivationArgument(arg);
throw new errors.InvalidDerivationArgument(arg);
}
};
HDPublicKey.prototype._deriveWithNumber = function (index, hardened) {
if (hardened || index >= HDPublicKey.Hardened) {
throw new HDPublicKey.Error.InvalidIndexCantDeriveHardened();
throw new errors.InvalidIndexCantDeriveHardened();
}
var cached = HDKeyCache.get(this.xpubkey, index, hardened);
if (cached) {
@ -132,7 +133,7 @@ HDPublicKey.prototype._deriveFromString = function (path) {
return this;
}
if (!_.contains(HDPublicKey.RootElementAlias, steps[0])) {
throw new HDPublicKey.Error.InvalidPath(path);
throw new errors.InvalidPath(path);
}
steps = steps.slice(1);
@ -165,24 +166,24 @@ HDPublicKey.isValidSerialized = function (data, network) {
* @param {string|Buffer} data - the serialized private key
* @param {string|Network=} network - optional, if present, checks that the
* network provided matches the network serialized.
* @return {HDPublicKey.Error|null}
* @return {errors|null}
*/
HDPublicKey.getSerializedError = function (data, network) {
/* jshint maxcomplexity: 10 */
/* jshint maxstatements: 20 */
if (!(_.isString(data) || bufferUtil.isBuffer(data))) {
return new HDPublicKey.Error.InvalidArgument('expected buffer or string');
return new errors.UnrecognizedArgument('expected buffer or string');
}
if (!Base58.validCharacters(data)) {
return new HDPublicKey.Error.InvalidB58Char('(unknown)', data);
return new errors.InvalidB58Char('(unknown)', data);
}
try {
data = Base58Check.decode(data);
} catch (e) {
return new HDPublicKey.Error.InvalidB58Checksum(data);
return new errors.InvalidB58Checksum(data);
}
if (data.length !== HDPublicKey.DataSize) {
return new HDPublicKey.Error.InvalidLength(data);
return new errors.InvalidLength(data);
}
if (!_.isUndefined(network)) {
var error = HDPublicKey._validateNetwork(data, network);
@ -192,7 +193,7 @@ HDPublicKey.getSerializedError = function (data, network) {
}
network = Network.get(network) || Network.defaultNetwork;
if (bufferUtil.integerFromBuffer(data.slice(0, 4)) === network.xprivkey) {
return new HDPublicKey.Error.ArgumentIsPrivateExtended();
return new errors.ArgumentIsPrivateExtended();
}
return null;
};
@ -200,11 +201,11 @@ HDPublicKey.getSerializedError = function (data, network) {
HDPublicKey._validateNetwork = function (data, networkArg) {
var network = Network.get(networkArg);
if (!network) {
return new HDPublicKey.Error.InvalidNetworkArgument(networkArg);
return new errors.InvalidNetworkArgument(networkArg);
}
var version = data.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd);
if (bufferUtil.integerFromBuffer(version) !== network.xpubkey) {
return new HDPublicKey.Error.InvalidNetwork(version);
return new errors.InvalidNetwork(version);
}
return null;
};
@ -289,7 +290,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
arg.checksum = checksum;
} else {
if (arg.checksum.toString('hex') !== checksum.toString('hex')) {
throw new HDPublicKey.Error.InvalidB58Checksum(concat, checksum);
throw new errors.InvalidB58Checksum(concat, checksum);
}
}
@ -407,75 +408,4 @@ HDPublicKey.ChecksumEnd = HDPublicKey.ChecksumStart + HDPublicKey.Che
assert(HDPublicKey.PublicKeyEnd === HDPublicKey.DataSize);
assert(HDPublicKey.ChecksumEnd === HDPublicKey.SerializedByteSize);
HDPublicKey.Error = function() {
Error.apply(this, arguments);
};
inherits(HDPublicKey.Error, Error);
HDPublicKey.Error.InvalidArgument = function() {
HDPublicKey.Error.apply(this, arguments);
this.message = 'Invalid argument';
};
inherits(HDPublicKey.Error.InvalidArgument, HDPublicKey.Error);
HDPublicKey.Error.ArgumentIsPrivateExtended = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Argument starts with xpriv..., it\'s a private key';
};
inherits(HDPublicKey.Error.ArgumentIsPrivateExtended, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidB58Char = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid Base 58 character';
};
inherits(HDPublicKey.Error.InvalidB58Char, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidB58Checksum = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid Base 58 checksum';
};
inherits(HDPublicKey.Error.InvalidB58Checksum, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidDerivationArgument = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid argument, expected number and boolean or string';
};
inherits(HDPublicKey.Error.InvalidDerivationArgument, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidLength = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid length for xpubkey format';
};
inherits(HDPublicKey.Error.InvalidLength, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidNetwork = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Unexpected version for network';
};
inherits(HDPublicKey.Error.InvalidNetwork, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidNetworkArgument = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Network argument must be \'livenet\' or \'testnet\'';
};
inherits(HDPublicKey.Error.InvalidNetworkArgument, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.InvalidPath = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Invalid path for derivation: must start with "m"';
};
inherits(HDPublicKey.Error.InvalidPath, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.MustSupplyArgument = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Must supply an argument for the constructor';
};
inherits(HDPublicKey.Error.MustSupplyArgument, HDPublicKey.Error.InvalidArgument);
HDPublicKey.Error.UnrecognizedArgument = function() {
HDPublicKey.Error.InvalidArgument.apply(this, arguments);
this.message = 'Creating a HDPublicKey requires a string, a buffer, a json, or an object';
};
inherits(HDPublicKey.Error.UnrecognizedArgument, HDPublicKey.Error.InvalidArgument);
module.exports = HDPublicKey;

View File

@ -75,7 +75,7 @@
"bs58": "=2.0.0",
"elliptic": "=0.15.14",
"hash.js": "=0.3.2",
"inherits": "^2.0.1",
"inherits": "=2.0.1",
"lodash": "=2.4.1",
"sha512": "=0.0.1"
},
@ -91,6 +91,7 @@
"gulp-mocha": "^2.0.0",
"gulp-rename": "^1.2.0",
"gulp-shell": "^0.2.10",
"inherits": "^2.0.1",
"lodash": "^2.4.1",
"mocha": "~2.0.1",
"run-sequence": "^1.0.2",

View File

@ -5,6 +5,7 @@ var assert = require('assert');
var should = require('chai').should();
var expect = require('chai').expect;
var bitcore = require('..');
var errors = bitcore.errors.HDPrivateKey.InvalidArgument;
var buffer = require('buffer');
var bufferUtil = bitcore.util.buffer;
var HDPrivateKey = bitcore.HDPrivateKey;
@ -49,7 +50,7 @@ describe('HDPrivate key interface', function() {
});
it('should error with an invalid checksum', function() {
expectFailBuilding(xprivkey + '1', HDPrivateKey.Error.InvalidB58Checksum);
expectFailBuilding(xprivkey + '1', errors.InvalidB58Checksum);
});
it('can be rebuilt from a json generated by itself', function() {
@ -66,7 +67,7 @@ describe('HDPrivate key interface', function() {
describe('should error with a nonsensical argument', function() {
it('like a number', function() {
expectFailBuilding(1, HDPrivateKey.Error.UnrecognizedArgument);
expectFailBuilding(1, errors.UnrecognizedArgument);
});
});
@ -80,11 +81,11 @@ describe('HDPrivate key interface', function() {
});
it('fails when trying to derive with an invalid argument', function() {
expectDerivationFail([], HDPrivateKey.Error.InvalidDerivationArgument);
expectDerivationFail([], errors.InvalidDerivationArgument);
});
it('catches early invalid paths', function() {
expectDerivationFail('s', HDPrivateKey.Error.InvalidPath);
expectDerivationFail('s', errors.InvalidPath);
});
it('allows derivation of hardened keys by passing a very big number', function() {
@ -102,28 +103,28 @@ describe('HDPrivate key interface', function() {
it('returns InvalidArgument if invalid data is given to getSerializedError', function() {
expect(
HDPrivateKey.getSerializedError(1) instanceof
HDPrivateKey.Error.InvalidArgument
errors.UnrecognizedArgument
).to.equal(true);
});
it('returns InvalidLength if data of invalid length is given to getSerializedError', function() {
expect(
HDPrivateKey.getSerializedError(Base58Check.encode(new buffer.Buffer('onestring'))) instanceof
HDPrivateKey.Error.InvalidLength
errors.InvalidLength
).to.equal(true);
});
it('returns InvalidNetworkArgument if an invalid network is provided', function() {
expect(
HDPrivateKey.getSerializedError(xprivkey, 'invalidNetwork') instanceof
HDPrivateKey.Error.InvalidNetworkArgument
errors.InvalidNetworkArgument
).to.equal(true);
});
it('recognizes that the wrong network was asked for', function() {
expect(
HDPrivateKey.getSerializedError(xprivkey, 'testnet') instanceof
HDPrivateKey.Error.InvalidNetwork
errors.InvalidNetwork
).to.equal(true);
});
@ -136,17 +137,17 @@ describe('HDPrivate key interface', function() {
HDPrivateKey.fromSeed('01234567890abcdef01234567890abcdef').xprivkey.should.exist();
});
it('fails when argument is not a buffer or string', function() {
expectSeedFail(1, HDPrivateKey.Error.InvalidEntropyArgument);
expectSeedFail(1, errors.InvalidEntropyArgument);
});
it('fails when argument doesn\'t provide enough entropy', function() {
expectSeedFail('01', HDPrivateKey.Error.NotEnoughEntropy);
expectSeedFail('01', errors.InvalidEntropyArgument.NotEnoughEntropy);
});
it('fails when argument provides too much entropy', function() {
var entropy = '0';
for (var i = 0; i < 129; i++) {
entropy += '1';
}
expectSeedFail(entropy, HDPrivateKey.Error.TooMuchEntropy);
expectSeedFail(entropy, errors.InvalidEntropyArgument.TooMuchEntropy);
});
});
@ -160,7 +161,7 @@ describe('HDPrivate key interface', function() {
} catch (e) {
error = e;
}
expect(error instanceof HDPrivateKey.Error.InvalidB58Checksum).to.equal(true);
expect(error instanceof errors.InvalidB58Checksum).to.equal(true);
});
it('correctly validates the checksum', function() {
var privKey = new HDPrivateKey(xprivkey);

View File

@ -7,6 +7,7 @@ var should = require('chai').should();
var expect = require('chai').expect;
var bitcore = require('..');
var buffer = require('buffer');
var errors = bitcore.errors.HDPublicKey.InvalidArgument;
var bufferUtil = bitcore.util.buffer;
var HDPrivateKey = bitcore.HDPrivateKey;
var HDPublicKey = bitcore.HDPublicKey;
@ -64,12 +65,12 @@ describe('HDPublicKey interface', function() {
});
it('fails when user doesn\'t supply an argument', function() {
expectFailBuilding(null, HDPublicKey.Error.MustSupplyArgument);
expectFailBuilding(null, errors.MustSupplyArgument);
});
it('doesn\'t recognize an invalid argument', function() {
expectFailBuilding(1, HDPublicKey.Error.UnrecognizedArgument);
expectFailBuilding(true, HDPublicKey.Error.UnrecognizedArgument);
expectFailBuilding(1, errors.UnrecognizedArgument);
expectFailBuilding(true, errors.UnrecognizedArgument);
});
@ -77,13 +78,13 @@ describe('HDPublicKey interface', function() {
it('fails on invalid length', function() {
expectFailBuilding(
Base58Check.encode(new buffer.Buffer([1, 2, 3])),
HDPublicKey.Error.InvalidLength
errors.InvalidLength
);
});
it('fails on invalid base58 encoding', function() {
expectFailBuilding(
xpubkey + '1',
HDPublicKey.Error.InvalidB58Checksum
errors.InvalidB58Checksum
);
});
it('user can ask if a string is valid', function() {
@ -111,7 +112,7 @@ describe('HDPublicKey interface', function() {
buffers.checksum = bufferUtil.integerAsBuffer(1);
expectFail(function() {
return new HDPublicKey(buffers);
}, HDPublicKey.Error.InvalidB58Checksum);
}, errors.InvalidB58Checksum);
});
});
@ -121,13 +122,13 @@ describe('HDPublicKey interface', function() {
expect(a instanceof b).to.equal(true);
};
it('throws invalid argument when argument is not a string or buffer', function() {
compareType(HDPublicKey.getSerializedError(1), HDPublicKey.Error.InvalidArgument);
compareType(HDPublicKey.getSerializedError(1), errors.UnrecognizedArgument);
});
it('if a network is provided, validates that data corresponds to it', function() {
compareType(HDPublicKey.getSerializedError(xpubkey, 'testnet'), HDPublicKey.Error.InvalidNetwork);
compareType(HDPublicKey.getSerializedError(xpubkey, 'testnet'), errors.InvalidNetwork);
});
it('recognizes invalid network arguments', function() {
compareType(HDPublicKey.getSerializedError(xpubkey, 'invalid'), HDPublicKey.Error.InvalidNetworkArgument);
compareType(HDPublicKey.getSerializedError(xpubkey, 'invalid'), errors.InvalidNetworkArgument);
});
it('recognizes a valid network', function() {
expect(HDPublicKey.getSerializedError(xpubkey, 'livenet')).to.equal(null);
@ -159,27 +160,27 @@ describe('HDPublicKey interface', function() {
it('doesn\'t allow object arguments for derivation', function() {
expectFail(function() {
return new HDPublicKey(xpubkey).derive({});
}, HDPublicKey.Error.InvalidDerivationArgument);
}, errors.InvalidDerivationArgument);
});
it('needs first argument for derivation', function() {
expectFail(function() {
return new HDPublicKey(xpubkey).derive('s');
}, HDPublicKey.Error.InvalidPath);
}, errors.InvalidPath);
});
it('doesn\'t allow other parameters like m\' or M\' or "s"', function() {
/* jshint quotmark: double */
expectDerivationFail("m'", HDPublicKey.Error.InvalidDerivationArgument);
expectDerivationFail("M'", HDPublicKey.Error.InvalidDerivationArgument);
expectDerivationFail("1", HDPublicKey.Error.InvalidDerivationArgument);
expectDerivationFail("S", HDPublicKey.Error.InvalidDerivationArgument);
expectDerivationFail("m'", errors.InvalidDerivationArgument);
expectDerivationFail("M'", errors.InvalidDerivationArgument);
expectDerivationFail("1", errors.InvalidDerivationArgument);
expectDerivationFail("S", errors.InvalidDerivationArgument);
});
it('can\'t derive hardened keys', function() {
expectFail(function() {
return new HDPublicKey(xpubkey).derive(HDPublicKey.Hardened + 1);
}, HDPublicKey.Error.InvalidDerivationArgument);
}, errors.InvalidDerivationArgument);
});
it('should use the cache', function() {