Merge pull request #891 from yemel/fix/hd-random-testnet
Allow creating a new random testnet HDPrivate key
This commit is contained in:
commit
dc7abe054b
|
@ -41,25 +41,25 @@ function HDPrivateKey(arg) {
|
||||||
if (!(this instanceof HDPrivateKey)) {
|
if (!(this instanceof HDPrivateKey)) {
|
||||||
return new HDPrivateKey(arg);
|
return new HDPrivateKey(arg);
|
||||||
}
|
}
|
||||||
if (arg) {
|
if (!arg) {
|
||||||
if (_.isString(arg) || BufferUtil.isBuffer(arg)) {
|
|
||||||
if (HDPrivateKey.isValidSerialized(arg)) {
|
|
||||||
this._buildFromSerialized(arg);
|
|
||||||
} else if (JSUtil.isValidJSON(arg)) {
|
|
||||||
this._buildFromJSON(arg);
|
|
||||||
} else {
|
|
||||||
throw HDPrivateKey.getSerializedError(arg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (_.isObject(arg)) {
|
|
||||||
this._buildFromObject(arg);
|
|
||||||
} else {
|
|
||||||
throw new hdErrors.UnrecognizedArgument(arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return this._generateRandomly();
|
return this._generateRandomly();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Network.get(arg)) {
|
||||||
|
return this._generateRandomly(arg);
|
||||||
|
} else if (_.isString(arg) || BufferUtil.isBuffer(arg)) {
|
||||||
|
if (HDPrivateKey.isValidSerialized(arg)) {
|
||||||
|
this._buildFromSerialized(arg);
|
||||||
|
} else if (JSUtil.isValidJSON(arg)) {
|
||||||
|
this._buildFromJSON(arg);
|
||||||
|
} else {
|
||||||
|
throw HDPrivateKey.getSerializedError(arg);
|
||||||
|
}
|
||||||
|
} else if (_.isObject(arg)) {
|
||||||
|
this._buildFromObject(arg);
|
||||||
|
} else {
|
||||||
|
throw new hdErrors.UnrecognizedArgument(arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -266,7 +266,6 @@ HDPrivateKey.prototype._generateRandomly = function(network) {
|
||||||
*/
|
*/
|
||||||
HDPrivateKey.fromSeed = function(hexa, network) {
|
HDPrivateKey.fromSeed = function(hexa, network) {
|
||||||
/* jshint maxcomplexity: 8 */
|
/* jshint maxcomplexity: 8 */
|
||||||
|
|
||||||
if (JSUtil.isHexaString(hexa)) {
|
if (JSUtil.isHexaString(hexa)) {
|
||||||
hexa = BufferUtil.hexToBuffer(hexa);
|
hexa = BufferUtil.hexToBuffer(hexa);
|
||||||
}
|
}
|
||||||
|
@ -282,7 +281,7 @@ HDPrivateKey.fromSeed = function(hexa, network) {
|
||||||
var hash = Hash.sha512hmac(hexa, new buffer.Buffer('Bitcoin seed'));
|
var hash = Hash.sha512hmac(hexa, new buffer.Buffer('Bitcoin seed'));
|
||||||
|
|
||||||
return new HDPrivateKey({
|
return new HDPrivateKey({
|
||||||
network: Network.get(network) || Network.livenet,
|
network: Network.get(network) || Network.defaultNetwork,
|
||||||
depth: 0,
|
depth: 0,
|
||||||
parentFingerPrint: 0,
|
parentFingerPrint: 0,
|
||||||
childIndex: 0,
|
childIndex: 0,
|
||||||
|
@ -441,7 +440,6 @@ HDPrivateKey.prototype.toJSON = function toJSON() {
|
||||||
HDPrivateKey.DefaultDepth = 0;
|
HDPrivateKey.DefaultDepth = 0;
|
||||||
HDPrivateKey.DefaultFingerprint = 0;
|
HDPrivateKey.DefaultFingerprint = 0;
|
||||||
HDPrivateKey.DefaultChildIndex = 0;
|
HDPrivateKey.DefaultChildIndex = 0;
|
||||||
HDPrivateKey.DefaultNetwork = Network.livenet;
|
|
||||||
HDPrivateKey.Hardened = 0x80000000;
|
HDPrivateKey.Hardened = 0x80000000;
|
||||||
HDPrivateKey.RootElementAlias = ['m', 'M', 'm\'', 'M\''];
|
HDPrivateKey.RootElementAlias = ['m', 'M', 'm\'', 'M\''];
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,12 @@ describe('HDPrivate key interface', function() {
|
||||||
should.exist(new HDPrivateKey().xprivkey);
|
should.exist(new HDPrivateKey().xprivkey);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should make a new private key from random for testnet', function() {
|
||||||
|
var key = new HDPrivateKey('testnet');
|
||||||
|
should.exist(key.xprivkey);
|
||||||
|
key.network.name.should.equal('testnet');
|
||||||
|
});
|
||||||
|
|
||||||
it('should not be able to change read-only properties', function() {
|
it('should not be able to change read-only properties', function() {
|
||||||
var hdkey = new HDPrivateKey();
|
var hdkey = new HDPrivateKey();
|
||||||
expect(function() {
|
expect(function() {
|
||||||
|
|
Loading…
Reference in New Issue