Add tests
This commit is contained in:
parent
b89bdd19f8
commit
5728c30371
235
lib/hdprivkey.js
235
lib/hdprivkey.js
|
@ -8,14 +8,27 @@ var Hash = require('./crypto/hash');
|
|||
var Network = require('./network');
|
||||
var Point = require('./crypto/point');
|
||||
var PrivateKey = require('./privkey');
|
||||
var Random = require('./crypto/random');
|
||||
|
||||
var assert = require('assert');
|
||||
var buffer = require('buffer');
|
||||
var util = require('./util');
|
||||
|
||||
var MINIMUM_ENTROPY_BITS = 128;
|
||||
var BITS_TO_BYTES = 128;
|
||||
var MAXIMUM_ENTROPY_BITS = 512;
|
||||
|
||||
|
||||
function HDPrivateKey(arg) {
|
||||
/* jshint maxcomplexity: 10 */
|
||||
if (arg instanceof HDPrivateKey) {
|
||||
return arg;
|
||||
}
|
||||
if (!this instanceof HDPrivateKey) {
|
||||
return new HDPrivateKey(arg);
|
||||
}
|
||||
if (arg) {
|
||||
if (_.isString(arg) || arg instanceof buffer.Buffer) {
|
||||
if (_.isString(arg) || buffer.Buffer.isBuffer(arg)) {
|
||||
if (HDPrivateKey.isValidSerialized(arg)) {
|
||||
this._buildFromSerialized(arg);
|
||||
} else if (util.isValidJson(arg)) {
|
||||
|
@ -25,7 +38,7 @@ function HDPrivateKey(arg) {
|
|||
}
|
||||
} else {
|
||||
if (_.isObject(arg)) {
|
||||
this.buildFromObject(arg);
|
||||
this._buildFromObject(arg);
|
||||
} else {
|
||||
throw new Error(HDPrivateKey.Errors.UnrecognizedArgument);
|
||||
}
|
||||
|
@ -53,9 +66,9 @@ HDPrivateKey.prototype._deriveWithNumber = function deriveWithNumber(index, hard
|
|||
var indexBuffer = util.integerAsBuffer(index);
|
||||
var data;
|
||||
if (hardened) {
|
||||
data = Buffer.concat([new Buffer([0]), this.privateKey.toBuffer(), indexBuffer]);
|
||||
data = buffer.Buffer.concat([new buffer.Buffer([0]), this.privateKey.toBuffer(), indexBuffer]);
|
||||
} else {
|
||||
data = Buffer.concat([this.publicKey.toBuffer(), indexBuffer]);
|
||||
data = buffer.Buffer.concat([this.publicKey.toBuffer(), indexBuffer]);
|
||||
}
|
||||
var hash = Hash.sha512hmac(data, this.chainCode);
|
||||
var leftPart = BN().fromBuffer(hash.slice(0, 32), {size: 32});
|
||||
|
@ -94,10 +107,84 @@ HDPrivateKey.prototype._deriveFromString = function deriveFromString(path) {
|
|||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Verifies that a given serialized private key in base58 with checksum format
|
||||
* is valid.
|
||||
*
|
||||
* @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 {boolean}
|
||||
*/
|
||||
HDPrivateKey.isValidSerialized = function isValidSerialized(data, network) {
|
||||
return !HDPrivateKey.getSerializedError(data, network);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks what's the error that causes the validation of a serialized private key
|
||||
* in base58 with checksum to fail.
|
||||
*
|
||||
* @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.Errors|null}
|
||||
*/
|
||||
HDPrivateKey.getSerializedError = function getSerializedError(data, network) {
|
||||
/* jshint maxcomplexity: 10 */
|
||||
if (!(_.isString(data) || buffer.Buffer.isBuffer(data))) {
|
||||
return HDPrivateKey.Errors.InvalidArgument;
|
||||
}
|
||||
if (_.isString(data)) {
|
||||
data = new buffer.Buffer(data);
|
||||
}
|
||||
if (!Base58.validCharacters(data)) {
|
||||
return HDPrivateKey.Errors.InvalidB58Char;
|
||||
}
|
||||
if (!Base58Check.validChecksum(data)) {
|
||||
return HDPrivateKey.Errors.InvalidB58Checksum;
|
||||
}
|
||||
if (data.length !== 78) {
|
||||
return HDPrivateKey.Errors.InvalidLength;
|
||||
}
|
||||
if (!_.isUndefined(network)) {
|
||||
var error = HDPrivateKey._validateNetwork(data, network);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
HDPrivateKey._validateNetwork = function validateNetwork(data, network) {
|
||||
network = Network.get(network);
|
||||
if (!network) {
|
||||
return HDPrivateKey.Errors.InvalidNetworkArgument;
|
||||
}
|
||||
var version = data.slice(4);
|
||||
if (version.toString() !== network.xprivkey.toString()) {
|
||||
return HDPrivateKey.Errors.InvalidNetwork;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
HDPrivateKey.prototype._buildFromJson = function buildFromJson(arg) {
|
||||
return this._buildFromObject(JSON.parse(arg));
|
||||
};
|
||||
|
||||
HDPrivateKey.prototype._buildFromObject = function buildFromObject(arg) {
|
||||
// TODO: Type validation
|
||||
var buffers = {
|
||||
version: util.integerAsBuffer(Network.get(arg.network).xprivkey),
|
||||
depth: util.integerAsBuffer(arg.depth),
|
||||
parentFingerPrint: util.integerAsBuffer(arg.parentFingerPrint),
|
||||
childIndex: util.integerAsBuffer(arg.childIndex),
|
||||
chainCode: util.integerAsBuffer(arg.chainCode),
|
||||
privateKey: util.hexToBuffer(arg.privateKey),
|
||||
checksum: util.integerAsBuffer(arg.checksum)
|
||||
};
|
||||
return this._buildFromBuffers(buffers);
|
||||
};
|
||||
|
||||
HDPrivateKey.prototype._buildFromSerialized = function buildFromSerialized(arg) {
|
||||
var decoded = Base58Check.decode(arg);
|
||||
var buffers = {
|
||||
|
@ -109,12 +196,40 @@ HDPrivateKey.prototype._buildFromSerialized = function buildFromSerialized(arg)
|
|||
chainCode: decoded.slice(HDPrivateKey.ChainCodeStart, HDPrivateKey.ChainCodeEnd),
|
||||
privateKey: decoded.slice(HDPrivateKey.PrivateKeyStart, HDPrivateKey.PrivateKeyEnd),
|
||||
checksum: decoded.slice(HDPrivateKey.ChecksumStart, HDPrivateKey.ChecksumEnd),
|
||||
xprivkey: decoded
|
||||
xprivkey: decoded.toString()
|
||||
};
|
||||
return this._buildFromBuffers(buffers);
|
||||
};
|
||||
|
||||
HDPrivateKey.prototype._generateRandomly = function generateRandomly() {
|
||||
HDPrivateKey.prototype._generateRandomly = function generateRandomly(network) {
|
||||
return HDPrivateKey.fromSeed(Random.getRandomBytes(64), network);
|
||||
};
|
||||
|
||||
HDPrivateKey.fromSeed = function fromSeed(hexa, network) {
|
||||
/* jshint maxcomplexity: 8 */
|
||||
|
||||
if (util.isHexaString(hexa)) {
|
||||
hexa = util.hexToBuffer(hexa);
|
||||
}
|
||||
if (!Buffer.isBuffer(hexa)) {
|
||||
throw new Error(HDPrivateKey.InvalidEntropyArg);
|
||||
}
|
||||
if (hexa.length < MINIMUM_ENTROPY_BITS * BITS_TO_BYTES) {
|
||||
throw new Error(HDPrivateKey.NotEnoughEntropy);
|
||||
}
|
||||
if (hexa.length > MAXIMUM_ENTROPY_BITS * BITS_TO_BYTES) {
|
||||
throw new Error('More than 512 bytes of entropy is nonstandard');
|
||||
}
|
||||
var hash = Hash.sha512hmac(hexa, new buffer.Buffer('Bitcoin seed'));
|
||||
|
||||
return new HDPrivateKey({
|
||||
network: Network.get(network) || Network.livenet,
|
||||
depth: 0,
|
||||
parentFingerPrint: 0,
|
||||
childIndex: 0,
|
||||
chainCode: hash.slice(32, 64),
|
||||
privateKey: hash.slice(0, 32)
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -122,29 +237,37 @@ HDPrivateKey.prototype._generateRandomly = function generateRandomly() {
|
|||
* internal structure
|
||||
*
|
||||
* @param {Object} arg
|
||||
* @param {Buffer} arg.version
|
||||
* @param {Buffer} arg.depth
|
||||
* @param {Buffer} arg.parentFingerPrint
|
||||
* @param {Buffer} arg.childIndex
|
||||
* @param {Buffer} arg.chainCode
|
||||
* @param {Buffer} arg.privateKey
|
||||
* @param {Buffer} arg.checksum
|
||||
* @param {buffer.Buffer} arg.version
|
||||
* @param {buffer.Buffer} arg.depth
|
||||
* @param {buffer.Buffer} arg.parentFingerPrint
|
||||
* @param {buffer.Buffer} arg.childIndex
|
||||
* @param {buffer.Buffer} arg.chainCode
|
||||
* @param {buffer.Buffer} arg.privateKey
|
||||
* @param {buffer.Buffer} arg.checksum
|
||||
* @param {string=} arg.xprivkey - if set, don't recalculate the base58
|
||||
* representation
|
||||
* @return {HDPrivateKey} this
|
||||
*/
|
||||
HDPrivateKey.prototype._buildFromBuffers = function buildFromObject(arg) {
|
||||
HDPrivateKey.prototype._buildFromBuffers = function buildFromBuffers(arg) {
|
||||
|
||||
HDPrivateKey._validateBufferArguments(arg);
|
||||
this._buffers = arg;
|
||||
|
||||
var sequence = [
|
||||
arg.version, arg.depth, arg.parentFingerPrint, arg.childIndex, arg.chainCode,
|
||||
util.emptyBuffer(1), arg.privateKey,
|
||||
];
|
||||
if (!arg.checksum) {
|
||||
arg.checksum = Base58Check.checksum(sequence);
|
||||
} else {
|
||||
if (arg.checksum.toString() !== sequence.toString()) {
|
||||
throw new Error(HDPrivateKey.Errors.InvalidB58Checksum);
|
||||
}
|
||||
}
|
||||
|
||||
if (!arg.xprivkey) {
|
||||
var sequence = [
|
||||
arg.version, arg.depth, arg.parentFingerPrint, arg.childIndex, arg.chainCode,
|
||||
util.emptyBuffer(1), arg.privateKey,
|
||||
arg.checksum
|
||||
];
|
||||
this.xprivkey = Base58.encode(Buffer.concat(sequence));
|
||||
sequence.push(arg.checksum);
|
||||
this.xprivkey = Base58.encode(buffer.Buffer.concat(sequence));
|
||||
} else {
|
||||
this.xprivkey = arg.xprivkey;
|
||||
}
|
||||
|
@ -162,7 +285,7 @@ HDPrivateKey.prototype._buildFromBuffers = function buildFromObject(arg) {
|
|||
HDPrivateKey._validateBufferArguments = function validateBufferArguments(arg) {
|
||||
var checkBuffer = function(name, size) {
|
||||
var buffer = arg[name];
|
||||
assert(buffer instanceof buffer.Buffer, name + ' argument is not a buffer');
|
||||
assert(buffer.Buffer.isBuffer(buffer), name + ' argument is not a buffer');
|
||||
assert(
|
||||
buffer.length === size,
|
||||
name + ' has not the expected size: found ' + buffer.length + ', expected ' + size
|
||||
|
@ -181,7 +304,7 @@ HDPrivateKey.prototype.toString = function toString() {
|
|||
return this.xprivkey;
|
||||
};
|
||||
|
||||
HDPrivateKey.prototype.toObject = function tObject() {
|
||||
HDPrivateKey.prototype.toObject = function toObject() {
|
||||
return {
|
||||
network: Network.get(util.integerFromBuffer(this._buffers.version)),
|
||||
depth: util.integerFromBuffer(this._buffers.depth),
|
||||
|
@ -199,6 +322,13 @@ HDPrivateKey.prototype.toJson = function toJson() {
|
|||
return JSON.stringify(this.toObject());
|
||||
};
|
||||
|
||||
HDPrivateKey.DefaultDepth = 0;
|
||||
HDPrivateKey.DefaultFingerprint = 0;
|
||||
HDPrivateKey.DefaultChildIndex = 0;
|
||||
HDPrivateKey.DefaultNetwork = Network.livenet;
|
||||
HDPrivateKey.Hardened = 0x80000000;
|
||||
HDPrivateKey.RootElementAlias = ['m', 'M', 'm\'', 'M\''];
|
||||
|
||||
HDPrivateKey.VersionSize = 4;
|
||||
HDPrivateKey.DepthLength = 4;
|
||||
HDPrivateKey.ParentFingerPrintSize = 4;
|
||||
|
@ -207,13 +337,6 @@ HDPrivateKey.ChainCodeSize = 32;
|
|||
HDPrivateKey.PrivateKeySize = 32;
|
||||
HDPrivateKey.CheckSumSize = 4;
|
||||
|
||||
HDPrivateKey.DefaultDepth = 0;
|
||||
HDPrivateKey.DefaultFingerprint = 0;
|
||||
HDPrivateKey.DefaultChildIndex = 0;
|
||||
HDPrivateKey.DefaultNetwork = Network.livenet;
|
||||
HDPrivateKey.Hardened = 0x80000000;
|
||||
HDPrivateKey.RootElementAlias = ['m', 'M', 'm\'', 'M\''];
|
||||
|
||||
HDPrivateKey.VersionStart = 0;
|
||||
HDPrivateKey.VersionEnd = HDPrivateKey.DepthStart = 4;
|
||||
HDPrivateKey.DepthEnd = HDPrivateKey.ParentFingerPrintStart = 8;
|
||||
|
@ -232,62 +355,14 @@ HDPrivateKey.Errors.InvalidChildIndex = 'Invalid Child Index - must be a number'
|
|||
HDPrivateKey.Errors.InvalidConstant = 'Unrecognized xprivkey version';
|
||||
HDPrivateKey.Errors.InvalidDepth = 'Invalid depth parameter - must be a number';
|
||||
HDPrivateKey.Errors.InvalidDerivationArgument = 'Invalid argument, expected number and boolean or string';
|
||||
HDPrivateKey.Errors.InvalidEntropyArg = 'Invalid argument: entropy must be an hexa string or binary buffer';
|
||||
HDPrivateKey.Errors.InvalidLength = 'Invalid length for xprivkey format';
|
||||
HDPrivateKey.Errors.InvalidNetwork = 'Unexpected version for network';
|
||||
HDPrivateKey.Errors.InvalidNetworkArgument = 'Network argument must be \'livenet\' or \'testnet\'';
|
||||
HDPrivateKey.Errors.InvalidParentFingerPrint = 'Invalid Parent Fingerprint - must be a number';
|
||||
HDPrivateKey.Errors.InvalidPath = 'Invalid path for derivation: must start with "m"';
|
||||
HDPrivateKey.Errors.NotEnoughEntropy = 'Need more than 128 bytes of entropy';
|
||||
HDPrivateKey.Errors.TooMuchEntropy = 'More than 512 bytes of entropy is non standard';
|
||||
HDPrivateKey.Errors.UnrecognizedArgument = 'Creating a HDPrivateKey requires a string, a buffer, a json, or an object';
|
||||
|
||||
/**
|
||||
* Verifies that a given serialized private key in base58 with checksum format
|
||||
* is valid.
|
||||
*
|
||||
* @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 {boolean}
|
||||
*/
|
||||
HDPrivateKey.isValidSerialized = function isValidSerialized(data, network) {
|
||||
return !HDPrivateKey.geSerializedError(data, network);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks what's the error that causes the validation of a serialized private key
|
||||
* in base58 with checksum to fail.
|
||||
*
|
||||
* @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.Errors|null}
|
||||
*/
|
||||
HDPrivateKey.getSerializedError = function getSerializedError(data, network) {
|
||||
if (!(_.isString(data) || data instanceof buffer.Buffer)) {
|
||||
return HDPrivateKey.Errors.InvalidArgument;
|
||||
}
|
||||
if (_.isString(data)) {
|
||||
data = new Buffer(data);
|
||||
}
|
||||
if (!Base58.validCharacters(data)) {
|
||||
return HDPrivateKey.Errors.InvalidB58Char;
|
||||
}
|
||||
if (!Base58Check.validChecksum(data)) {
|
||||
return HDPrivateKey.Errors.InvalidB58Checksum;
|
||||
}
|
||||
if (data.length !== 78) {
|
||||
return HDPrivateKey.Errors.InvalidLength;
|
||||
}
|
||||
if (!_.isUndefined(network)) {
|
||||
network = Network.get(network);
|
||||
if (!network) {
|
||||
return HDPrivateKey.Errors.InvalidNetworkArgument;
|
||||
}
|
||||
var version = data.slice(4);
|
||||
if (version.toString() !== network.xprivkey.toString()) {
|
||||
return HDPrivateKey.Errors.InvalidNetwork;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports = HDPrivateKey;
|
||||
|
|
361
test/bip32.js
361
test/bip32.js
|
@ -1,361 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var should = require('chai').should();
|
||||
var bitcore = require('..');
|
||||
var BIP32 = bitcore.BIP32;
|
||||
|
||||
describe('BIP32', function() {
|
||||
|
||||
//test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
||||
var vector1_master = '000102030405060708090a0b0c0d0e0f';
|
||||
var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
|
||||
var vector1_m_private = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
|
||||
var vector1_m0h_public = 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw';
|
||||
var vector1_m0h_private = 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7';
|
||||
var vector1_m0h1_public = 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ';
|
||||
var vector1_m0h1_private = 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs';
|
||||
var vector1_m0h12h_public = 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5';
|
||||
var vector1_m0h12h_private = 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM';
|
||||
var vector1_m0h12h2_public = 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV';
|
||||
var vector1_m0h12h2_private = 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334';
|
||||
var vector1_m0h12h21000000000_public = 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy';
|
||||
var vector1_m0h12h21000000000_private = 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76';
|
||||
var vector2_master = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
|
||||
var vector2_m_public = 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB';
|
||||
var vector2_m_private = 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U';
|
||||
var vector2_m0_public = 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH';
|
||||
var vector2_m0_private = 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt';
|
||||
var vector2_m02147483647h_public = 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a';
|
||||
var vector2_m02147483647h_private = 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9';
|
||||
var vector2_m02147483647h1_public = 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon';
|
||||
var vector2_m02147483647h1_private = 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef';
|
||||
var vector2_m02147483647h12147483646h_public = 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL';
|
||||
var vector2_m02147483647h12147483646h_private = 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc';
|
||||
var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt';
|
||||
var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j';
|
||||
|
||||
it('should make a new a bip32', function() {
|
||||
var bip32;
|
||||
bip32 = new BIP32();
|
||||
should.exist(bip32);
|
||||
bip32 = BIP32();
|
||||
should.exist(bip32);
|
||||
new BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
|
||||
BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
|
||||
BIP32(BIP32(vector1_m_private)).toString().should.equal(vector1_m_private);
|
||||
});
|
||||
|
||||
it('should initialize test vector 1 from the extended public key', function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_public);
|
||||
should.exist(bip32);
|
||||
});
|
||||
|
||||
it('should initialize test vector 1 from the extended private key', function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
should.exist(bip32);
|
||||
});
|
||||
|
||||
it('should get the extended public key from the extended private key for test vector 1', function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
bip32.xpubkeyString().should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it("should get m/0' ext. private key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector1_m0h_private);
|
||||
});
|
||||
|
||||
it("should get m/0' ext. public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector1_m0h_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. private key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector1_m0h1_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector1_m0h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/1");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector1_m0h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h ext. private key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector1_m0h12h_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h ext. public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector1_m0h12h_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2 ext. private key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'/2");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector1_m0h12h2_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/2");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector1_m0h12h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'/2");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector1_m0h12h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'/2/1000000000");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector1_m0h12h21000000000_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'/2/1000000000");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector1_m0h12h21000000000_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() {
|
||||
var bip32 = new BIP32().fromString(vector1_m_private);
|
||||
var child = bip32.derive("m/0'/1/2'/2");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/1000000000");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector1_m0h12h21000000000_public);
|
||||
});
|
||||
|
||||
it('should initialize test vector 2 from the extended public key', function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_public);
|
||||
should.exist(bip32);
|
||||
});
|
||||
|
||||
it('should initialize test vector 2 from the extended private key', function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
should.exist(bip32);
|
||||
});
|
||||
|
||||
it('should get the extended public key from the extended private key for test vector 2', function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
bip32.xpubkeyString().should.equal(vector2_m_public);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. private key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector2_m0_private);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector2_m0_public);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. public key from m public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/0");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector2_m0_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h ext. private key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector2_m02147483647h_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h ext. public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector2_m02147483647h_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector2_m02147483647h1_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector2_m02147483647h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/1");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector2_m02147483647h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector2_m02147483647h12147483646h_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector2_m02147483647h12147483646h_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
|
||||
should.exist(child);
|
||||
child.xprivkeyString().should.equal(vector2_m02147483647h12147483646h2_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
|
||||
should.exist(child);
|
||||
child.xpubkeyString().should.equal(vector2_m02147483647h12147483646h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() {
|
||||
var bip32 = new BIP32().fromString(vector2_m_private);
|
||||
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
|
||||
var child_pub = new BIP32().fromString(child.xpubkeyString());
|
||||
var child2 = child_pub.derive("m/2");
|
||||
should.exist(child2);
|
||||
child2.xpubkeyString().should.equal(vector2_m02147483647h12147483646h2_public);
|
||||
});
|
||||
|
||||
describe('testnet', function() {
|
||||
it('should initialize a new BIP32 correctly from a random BIP32', function() {
|
||||
var b1 = new BIP32();
|
||||
b1.fromRandom('testnet');
|
||||
var b2 = new BIP32().fromString(b1.xpubkeyString());
|
||||
b2.xpubkeyString().should.equal(b1.xpubkeyString());
|
||||
});
|
||||
|
||||
it('should generate valid ext pub key for testnet', function() {
|
||||
var b = new BIP32();
|
||||
b.fromRandom('testnet');
|
||||
b.xpubkeyString().substring(0,4).should.equal('tpub');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set', function() {
|
||||
var bip32 = BIP32(vector1_m_private);
|
||||
var bip322 = BIP32().set({
|
||||
version: bip32.version,
|
||||
depth: bip32.depth,
|
||||
parentfingerprint: bip32.parentfingerprint,
|
||||
childindex: bip32.childindex,
|
||||
chaincode: bip32.chaincode,
|
||||
key: bip32.key,
|
||||
hasprivkey: bip32.hasprivkey,
|
||||
pubkeyhash: bip32.pubKeyhash,
|
||||
xpubkey: bip32.xpubkey,
|
||||
xprivkey: bip32.xprivkey
|
||||
});
|
||||
bip322.toString().should.equal(bip32.toString());
|
||||
bip322.set({}).toString().should.equal(bip32.toString());
|
||||
});
|
||||
|
||||
describe('#seed', function() {
|
||||
|
||||
it('should initialize a new BIP32 correctly from test vector 1 seed', function() {
|
||||
var hex = vector1_master;
|
||||
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
|
||||
should.exist(bip32);
|
||||
bip32.xprivkeyString().should.equal(vector1_m_private);
|
||||
bip32.xpubkeyString().should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it('should initialize a new BIP32 correctly from test vector 2 seed', function() {
|
||||
var hex = vector2_master;
|
||||
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
|
||||
should.exist(bip32);
|
||||
bip32.xprivkeyString().should.equal(vector2_m_private);
|
||||
bip32.xpubkeyString().should.equal(vector2_m_public);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#fromString', function() {
|
||||
|
||||
it('should make a bip32 from a string', function() {
|
||||
var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
|
||||
var bip32 = new BIP32().fromString(str);
|
||||
should.exist(bip32);
|
||||
bip32.toString().should.equal(str);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#toString', function() {
|
||||
var bip32 = new BIP32();
|
||||
bip32.fromRandom('mainnet');
|
||||
var tip32 = new BIP32();
|
||||
tip32.fromRandom('testnet');
|
||||
|
||||
it('should return an xprv string', function() {
|
||||
bip32.toString().slice(0, 4).should.equal('xprv');
|
||||
});
|
||||
|
||||
it('should return an xpub string', function() {
|
||||
var bip32b = new BIP32().fromString(bip32.xpubkeyString());
|
||||
bip32b.toString().slice(0, 4).should.equal('xpub');
|
||||
});
|
||||
|
||||
it('should return a tprv string', function() {
|
||||
tip32.toString().slice(0, 4).should.equal('tprv');
|
||||
});
|
||||
|
||||
it('should return a tpub string', function() {
|
||||
var tip32b = new BIP32().fromString(tip32.xpubkeyString());
|
||||
tip32b.toString().slice(0, 4).should.equal('tpub');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,230 @@
|
|||
'use strict';
|
||||
|
||||
// Relax some linter options:
|
||||
// * quote marks so "m/0'/1/2'/" doesn't need to be scaped
|
||||
// * too many tests, maxstatements -> 100
|
||||
// * store test vectors at the end, latedef: false
|
||||
// * should call is never defined
|
||||
/* jshint quotmark: false */
|
||||
/* jshint latedef: false */
|
||||
/* jshint maxstatements: 100 */
|
||||
/* jshint unused: false */
|
||||
|
||||
var should = require('chai').should();
|
||||
var bitcore = require('..');
|
||||
var HDPrivateKey = bitcore.HDPrivateKey;
|
||||
var HDPublicKey = bitcore.HDPublicKey;
|
||||
|
||||
describe('BIP32 compliance', function() {
|
||||
|
||||
it('should initialize test vector 1 from the extended public key', function() {
|
||||
new HDPublicKey(vector1_m_public).xpubkey.should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it('should initialize test vector 1 from the extended private key', function() {
|
||||
new HDPrivateKey(vector1_m_private).xprivkey.should.equal(vector1_m_private);
|
||||
});
|
||||
|
||||
it('can initialize a public key from an extended private key', function() {
|
||||
new HDPublicKey(vector1_m_private).xpubkey.should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it('toString should be equal to the `xpubkey` member', function() {
|
||||
var privateKey = new HDPrivateKey(vector1_m_private);
|
||||
privateKey.toString().should.equal(privateKey.xprivkey);
|
||||
});
|
||||
|
||||
it('toString should be equal to the `xpubkey` member', function() {
|
||||
var publicKey = new HDPublicKey(vector1_m_public);
|
||||
publicKey.toString().should.equal(publicKey.xpubkey);
|
||||
});
|
||||
|
||||
it('should get the extended public key from the extended private key for test vector 1', function() {
|
||||
HDPrivateKey(vector1_m_private).xpubkey.should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it("should get m/0' ext. private key from test vector 1", function() {
|
||||
var privateKey = new HDPrivateKey(vector1_m_private);
|
||||
privateKey.derive("m/0'").xprivkey.should.equal(vector1_m0h_private);
|
||||
});
|
||||
|
||||
it("should get m/0' ext. public key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'")
|
||||
.xpubkey.should.equal(vector1_m0h_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. private key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1")
|
||||
.xprivkey.should.equal(vector1_m0h1_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. public key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1")
|
||||
.xpubkey.should.equal(vector1_m0h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
|
||||
var derivedPublic = HDPrivateKey(vector1_m_private).derive("m/0'").hdPublicKey;
|
||||
derivedPublic.derive("m/1").xpubkey.should.equal(vector1_m0h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2' ext. private key from test vector 1", function() {
|
||||
var privateKey = new HDPrivateKey(vector1_m_private);
|
||||
var derived = privateKey.derive("m/0'/1/2'");
|
||||
derived.xprivkey.should.equal(vector1_m0h12h_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2' ext. public key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1/2'")
|
||||
.xpubkey.should.equal(vector1_m0h12h_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2'/2 ext. private key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2")
|
||||
.xprivkey.should.equal(vector1_m0h12h2_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() {
|
||||
var derived = HDPrivateKey(vector1_m_private).derive("m/0'/1/2'").hdPublicKey;
|
||||
derived.derive("m/2").xpubkey.should.equal(vector1_m0h12h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2")
|
||||
.xpubkey.should.equal(vector1_m0h12h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2/1000000000")
|
||||
.xprivkey.should.equal(vector1_m0h12h21000000000_private);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() {
|
||||
HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2/1000000000")
|
||||
.xpubkey.should.equal(vector1_m0h12h21000000000_public);
|
||||
});
|
||||
|
||||
it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() {
|
||||
var derived = HDPrivateKey(vector1_m_private).derive("m/0'/1/2'/2").hdPublicKey;
|
||||
derived.derive("m/1000000000").xpubkey.should.equal(vector1_m0h12h21000000000_public);
|
||||
});
|
||||
|
||||
it('should initialize test vector 2 from the extended public key', function() {
|
||||
HDPublicKey(vector2_m_public).xpubkey.should.equal(vector2_m_public);
|
||||
});
|
||||
|
||||
it('should initialize test vector 2 from the extended private key', function() {
|
||||
HDPrivateKey(vector2_m_private).xprivkey.should.equal(vector2_m_private);
|
||||
});
|
||||
|
||||
it('should get the extended public key from the extended private key for test vector 2', function() {
|
||||
HDPrivateKey(vector2_m_private).xpubkey.should.equal(vector2_m_public);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. private key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive(0).xprivkey.should.equal(vector2_m0_private);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive(0).xpubkey.should.equal(vector2_m0_public);
|
||||
});
|
||||
|
||||
it("should get m/0 ext. public key from m public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).hdPublicKey.derive(0).should.equal(vector2_m0_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h ext. private key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'")
|
||||
.xprivkey.should.equal(vector2_m02147483647h_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h ext. public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'")
|
||||
.xpubkey.should.equal(vector2_m02147483647h_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1")
|
||||
.xprivkey.should.equal(vector2_m02147483647h1_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1")
|
||||
.xpubkey.should.equal(vector2_m02147483647h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() {
|
||||
var derived = HDPrivateKey(vector2_m_private).derive("m/0/2147483647'").hdPublicKey;
|
||||
derived.derive(1).xpubkey.should.equal(vector2_m02147483647h1_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'")
|
||||
.xprivkey.should.equal(vector2_m02147483647h12147483646h_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'")
|
||||
.xpubkey.should.equal(vector2_m02147483647h12147483646h_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'/2")
|
||||
.xprivkey.should.equal(vector2_m02147483647h12147483646h2_private);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() {
|
||||
HDPrivateKey(vector2_m_private).derive("m/0/2147483647'/1/2147483646'/2")
|
||||
.xpubkey.should.equal(vector2_m02147483647h12147483646h2_public);
|
||||
});
|
||||
|
||||
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() {
|
||||
var derivedPublic = HDPrivateKey(vector2_m_private)
|
||||
.derive("m/0/2147483647'/1/2147483646'").hdPublicKey;
|
||||
derivedPublic.derive("m/2")
|
||||
.xpubkey.should.equal(vector2_m02147483647h12147483646h2_public);
|
||||
});
|
||||
|
||||
describe('seed', function() {
|
||||
|
||||
it('should initialize a new BIP32 correctly from test vector 1 seed', function() {
|
||||
var seededKey = HDPrivateKey.fromSeed(vector1_master);
|
||||
seededKey.xprivkey.should.equal(vector1_m_private);
|
||||
seededKey.xpubkey.should.equal(vector1_m_public);
|
||||
});
|
||||
|
||||
it('should initialize a new BIP32 correctly from test vector 2 seed', function() {
|
||||
var seededKey = HDPrivateKey.fromSeed(vector2_master);
|
||||
seededKey.xprivkey.should.equal(vector2_m_private);
|
||||
seededKey.xpubkey.should.equal(vector2_m_public);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
||||
var vector1_master = '000102030405060708090a0b0c0d0e0f';
|
||||
var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
|
||||
var vector1_m_private = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
|
||||
var vector1_m0h_public = 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw';
|
||||
var vector1_m0h_private = 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7';
|
||||
var vector1_m0h1_public = 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ';
|
||||
var vector1_m0h1_private = 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs';
|
||||
var vector1_m0h12h_public = 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5';
|
||||
var vector1_m0h12h_private = 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM';
|
||||
var vector1_m0h12h2_public = 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV';
|
||||
var vector1_m0h12h2_private = 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334';
|
||||
var vector1_m0h12h21000000000_public = 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy';
|
||||
var vector1_m0h12h21000000000_private = 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76';
|
||||
var vector2_master = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
|
||||
var vector2_m_public = 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB';
|
||||
var vector2_m_private = 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U';
|
||||
var vector2_m0_public = 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH';
|
||||
var vector2_m0_private = 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt';
|
||||
var vector2_m02147483647h_public = 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a';
|
||||
var vector2_m02147483647h_private = 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9';
|
||||
var vector2_m02147483647h1_public = 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon';
|
||||
var vector2_m02147483647h1_private = 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef';
|
||||
var vector2_m02147483647h12147483646h_public = 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL';
|
||||
var vector2_m02147483647h12147483646h_private = 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc';
|
||||
var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt';
|
||||
var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j';
|
|
@ -0,0 +1,31 @@
|
|||
'use strict';
|
||||
/* jshint unused: false */
|
||||
var should = require('chai').should();
|
||||
var bitcore = require('..');
|
||||
var HDPrivateKey = bitcore.HDPrivateKey;
|
||||
|
||||
var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
|
||||
|
||||
describe('HDPrivate key interface', function() {
|
||||
|
||||
it('should make a new private key from random', function() {
|
||||
new HDPrivateKey().should.exist();
|
||||
});
|
||||
|
||||
it('allows no-new calling', function() {
|
||||
HDPrivateKey(xprivkey).toString().should.equal(xprivkey);
|
||||
});
|
||||
|
||||
it('allows the use of a copy constructor', function() {
|
||||
HDPrivateKey(HDPrivateKey(xprivkey))
|
||||
.xprivkey.should.equal(xprivkey);
|
||||
});
|
||||
|
||||
it('shouldn\'t matter if derivations are made with strings or numbers', function() {
|
||||
var privateKey = new HDPrivateKey(xprivkey);
|
||||
var derivedByString = privateKey.derive('m/0\'/1/2\'');
|
||||
var derivedByNumber = privateKey.derive(0, true).derive(1).derive(2, true);
|
||||
derivedByNumber.xprivkey.should.equal(derivedByString.xprivkey);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue