2019-02-01 19:50:10 -05:00
|
|
|
"use strict";
|
2018-06-13 15:39:39 -04:00
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
// See: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
|
|
|
// See: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
|
2018-09-24 16:07:14 -04:00
|
|
|
var errors = __importStar(require("../errors"));
|
2018-06-20 20:29:54 -04:00
|
|
|
// The English language word list.
|
|
|
|
// For additional word lists, please see /src.tc/wordlists/
|
|
|
|
var lang_en_1 = require("../wordlists/lang-en");
|
2018-07-16 00:24:50 -04:00
|
|
|
// Automatically register English?
|
|
|
|
//import { register } from '../wordlists/wordlist';
|
|
|
|
//register(langEn);
|
2019-02-01 19:50:10 -05:00
|
|
|
var basex_1 = require("./basex");
|
2018-09-24 16:07:14 -04:00
|
|
|
var bytes_1 = require("./bytes");
|
|
|
|
var bignumber_1 = require("./bignumber");
|
|
|
|
var utf8_1 = require("./utf8");
|
|
|
|
var pbkdf2_1 = require("./pbkdf2");
|
|
|
|
var hmac_1 = require("./hmac");
|
|
|
|
var properties_1 = require("./properties");
|
|
|
|
var secp256k1_1 = require("./secp256k1");
|
|
|
|
var sha2_1 = require("./sha2");
|
2018-07-23 02:59:01 -04:00
|
|
|
var N = bignumber_1.bigNumberify("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
|
2018-06-13 15:39:39 -04:00
|
|
|
// "Bitcoin seed"
|
|
|
|
var MasterSecret = utf8_1.toUtf8Bytes('Bitcoin seed');
|
|
|
|
var HardenedBit = 0x80000000;
|
|
|
|
// Returns a byte with the MSB bits set
|
|
|
|
function getUpperMask(bits) {
|
|
|
|
return ((1 << bits) - 1) << (8 - bits);
|
|
|
|
}
|
|
|
|
// Returns a byte with the LSB bits set
|
|
|
|
function getLowerMask(bits) {
|
|
|
|
return (1 << bits) - 1;
|
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
function bytes32(value) {
|
|
|
|
return bytes_1.hexZeroPad(bytes_1.hexlify(value), 32);
|
|
|
|
}
|
|
|
|
function base58check(data) {
|
|
|
|
var checksum = bytes_1.hexDataSlice(sha2_1.sha256(sha2_1.sha256(data)), 0, 4);
|
|
|
|
return basex_1.Base58.encode(bytes_1.concat([data, checksum]));
|
|
|
|
}
|
2018-07-30 18:59:52 -04:00
|
|
|
var _constructorGuard = {};
|
2018-06-18 05:42:41 -04:00
|
|
|
exports.defaultPath = "m/44'/60'/0'/0/0";
|
2018-07-30 18:59:52 -04:00
|
|
|
var HDNode = /** @class */ (function () {
|
2018-06-19 02:12:57 -04:00
|
|
|
/**
|
|
|
|
* This constructor should not be called directly.
|
|
|
|
*
|
|
|
|
* Please use:
|
|
|
|
* - fromMnemonic
|
|
|
|
* - fromSeed
|
|
|
|
*/
|
2019-02-01 19:50:10 -05:00
|
|
|
function HDNode(constructorGuard, privateKey, publicKey, parentFingerprint, chainCode, index, depth, mnemonic, path) {
|
2018-07-30 18:59:52 -04:00
|
|
|
errors.checkNew(this, HDNode);
|
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error('HDNode constructor cannot be called directly');
|
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
if (privateKey) {
|
|
|
|
var keyPair = new secp256k1_1.KeyPair(privateKey);
|
|
|
|
properties_1.defineReadOnly(this, 'privateKey', keyPair.privateKey);
|
|
|
|
properties_1.defineReadOnly(this, 'publicKey', keyPair.compressedPublicKey);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
properties_1.defineReadOnly(this, 'privateKey', null);
|
|
|
|
properties_1.defineReadOnly(this, 'publicKey', bytes_1.hexlify(publicKey));
|
|
|
|
}
|
|
|
|
properties_1.defineReadOnly(this, 'parentFingerprint', parentFingerprint);
|
|
|
|
properties_1.defineReadOnly(this, 'fingerprint', bytes_1.hexDataSlice(sha2_1.ripemd160(sha2_1.sha256(this.publicKey)), 0, 4));
|
2018-10-07 01:13:10 -04:00
|
|
|
properties_1.defineReadOnly(this, 'address', secp256k1_1.computeAddress(this.publicKey));
|
2019-02-01 19:50:10 -05:00
|
|
|
properties_1.defineReadOnly(this, 'chainCode', chainCode);
|
2018-07-30 18:59:52 -04:00
|
|
|
properties_1.defineReadOnly(this, 'index', index);
|
|
|
|
properties_1.defineReadOnly(this, 'depth', depth);
|
|
|
|
properties_1.defineReadOnly(this, 'mnemonic', mnemonic);
|
|
|
|
properties_1.defineReadOnly(this, 'path', path);
|
|
|
|
properties_1.setType(this, 'HDNode');
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
Object.defineProperty(HDNode.prototype, "extendedKey", {
|
|
|
|
get: function () {
|
|
|
|
// We only support the mainnet values for now, but if anyone needs
|
|
|
|
// testnet values, let me know. I believe current senitment is that
|
|
|
|
// we should always use mainnet, and use BIP-44 to derive the network
|
|
|
|
// - Mainnet: public=0x0488B21E, private=0x0488ADE4
|
|
|
|
// - Testnet: public=0x043587CF, private=0x04358394
|
|
|
|
if (this.depth >= 256) {
|
|
|
|
throw new Error("Depth too large!");
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
return base58check(bytes_1.concat([
|
|
|
|
((this.privateKey != null) ? "0x0488ADE4" : "0x0488B21E"),
|
|
|
|
bytes_1.hexlify(this.depth),
|
|
|
|
this.parentFingerprint,
|
|
|
|
bytes_1.hexZeroPad(bytes_1.hexlify(this.index), 4),
|
|
|
|
this.chainCode,
|
|
|
|
((this.privateKey != null) ? bytes_1.concat(["0x00", this.privateKey]) : this.publicKey),
|
|
|
|
]));
|
|
|
|
},
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
HDNode.prototype.neuter = function () {
|
|
|
|
return new HDNode(_constructorGuard, null, this.publicKey, this.parentFingerprint, this.chainCode, this.index, this.depth, null, this.path);
|
|
|
|
};
|
|
|
|
HDNode.prototype._derive = function (index) {
|
|
|
|
if (index > 0xffffffff) {
|
|
|
|
throw new Error("invalid index - " + String(index));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
// Base path
|
|
|
|
var path = this.path;
|
|
|
|
if (path) {
|
2019-01-17 16:34:17 -05:00
|
|
|
path += '/' + (index & ~HardenedBit);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
var data = new Uint8Array(37);
|
2018-06-13 15:39:39 -04:00
|
|
|
if (index & HardenedBit) {
|
2019-02-01 19:50:10 -05:00
|
|
|
if (!this.privateKey) {
|
|
|
|
throw new Error('cannot derive child of neutered node');
|
|
|
|
}
|
2018-06-13 15:39:39 -04:00
|
|
|
// Data = 0x00 || ser_256(k_par)
|
2018-06-17 16:47:28 -04:00
|
|
|
data.set(bytes_1.arrayify(this.privateKey), 1);
|
2018-06-13 15:39:39 -04:00
|
|
|
// Hardened path
|
|
|
|
if (path) {
|
|
|
|
path += "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Data = ser_p(point(k_par))
|
2019-02-01 19:50:10 -05:00
|
|
|
data.set(bytes_1.arrayify(this.publicKey));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
// Data += ser_32(i)
|
|
|
|
for (var i = 24; i >= 0; i -= 8) {
|
|
|
|
data[33 + (i >> 3)] = ((index >> (24 - i)) & 0xff);
|
|
|
|
}
|
2018-08-01 18:05:19 -04:00
|
|
|
var I = hmac_1.computeHmac(hmac_1.SupportedAlgorithms.sha512, this.chainCode, data);
|
2019-02-01 19:50:10 -05:00
|
|
|
var IL = I.slice(0, 32);
|
2018-06-13 15:39:39 -04:00
|
|
|
var IR = I.slice(32);
|
2019-02-01 19:50:10 -05:00
|
|
|
// The private key
|
|
|
|
var ki = null;
|
|
|
|
// The public key
|
|
|
|
var Ki = null;
|
|
|
|
if (this.privateKey) {
|
|
|
|
ki = bytes32(bignumber_1.bigNumberify(IL).add(this.privateKey).mod(N));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var ek = new secp256k1_1.KeyPair(bytes_1.hexlify(IL));
|
|
|
|
Ki = ek._addPoint(this.publicKey);
|
|
|
|
}
|
|
|
|
return new HDNode(_constructorGuard, ki, Ki, this.fingerprint, bytes32(IR), index, this.depth + 1, this.mnemonic, path);
|
2018-06-13 15:39:39 -04:00
|
|
|
};
|
|
|
|
HDNode.prototype.derivePath = function (path) {
|
|
|
|
var components = path.split('/');
|
|
|
|
if (components.length === 0 || (components[0] === 'm' && this.depth !== 0)) {
|
2019-02-01 19:50:10 -05:00
|
|
|
throw new Error('invalid path - ' + path);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
if (components[0] === 'm') {
|
|
|
|
components.shift();
|
|
|
|
}
|
|
|
|
var result = this;
|
|
|
|
for (var i = 0; i < components.length; i++) {
|
|
|
|
var component = components[i];
|
|
|
|
if (component.match(/^[0-9]+'$/)) {
|
|
|
|
var index = parseInt(component.substring(0, component.length - 1));
|
|
|
|
if (index >= HardenedBit) {
|
|
|
|
throw new Error('invalid path index - ' + component);
|
|
|
|
}
|
|
|
|
result = result._derive(HardenedBit + index);
|
|
|
|
}
|
|
|
|
else if (component.match(/^[0-9]+$/)) {
|
|
|
|
var index = parseInt(component);
|
|
|
|
if (index >= HardenedBit) {
|
|
|
|
throw new Error('invalid path index - ' + component);
|
|
|
|
}
|
|
|
|
result = result._derive(index);
|
|
|
|
}
|
|
|
|
else {
|
2019-08-26 17:31:40 -04:00
|
|
|
throw new Error('invalid path component - ' + component);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2018-07-30 18:59:52 -04:00
|
|
|
HDNode.isHDNode = function (value) {
|
|
|
|
return properties_1.isType(value, 'HDNode');
|
|
|
|
};
|
2018-06-13 15:39:39 -04:00
|
|
|
return HDNode;
|
2018-07-30 18:59:52 -04:00
|
|
|
}());
|
|
|
|
exports.HDNode = HDNode;
|
2019-02-01 19:50:10 -05:00
|
|
|
function fromExtendedKey(extendedKey) {
|
|
|
|
var bytes = basex_1.Base58.decode(extendedKey);
|
|
|
|
if (bytes.length !== 82 || base58check(bytes.slice(0, 78)) !== extendedKey) {
|
|
|
|
errors.throwError("invalid extended key", errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "extendedKey",
|
|
|
|
value: "[REDACTED]"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var depth = bytes[4];
|
|
|
|
var parentFingerprint = bytes_1.hexlify(bytes.slice(5, 9));
|
|
|
|
var index = parseInt(bytes_1.hexlify(bytes.slice(9, 13)).substring(2), 16);
|
|
|
|
var chainCode = bytes_1.hexlify(bytes.slice(13, 45));
|
|
|
|
var key = bytes.slice(45, 78);
|
|
|
|
switch (bytes_1.hexlify(bytes.slice(0, 4))) {
|
|
|
|
// Public Key
|
|
|
|
case "0x0488b21e":
|
|
|
|
case "0x043587cf":
|
|
|
|
return new HDNode(_constructorGuard, null, bytes_1.hexlify(key), parentFingerprint, chainCode, index, depth, null, null);
|
|
|
|
// Private Key
|
|
|
|
case "0x0488ade4":
|
2019-06-28 15:42:04 -04:00
|
|
|
case "0x04358394":
|
2019-02-01 19:50:10 -05:00
|
|
|
if (key[0] !== 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return new HDNode(_constructorGuard, bytes_1.hexlify(key.slice(1)), null, parentFingerprint, chainCode, index, depth, null, null);
|
|
|
|
}
|
|
|
|
return errors.throwError("invalid extended key", errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "extendedKey",
|
|
|
|
value: "[REDACTED]"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
exports.fromExtendedKey = fromExtendedKey;
|
2018-06-13 15:39:39 -04:00
|
|
|
function _fromSeed(seed, mnemonic) {
|
2018-06-17 16:47:28 -04:00
|
|
|
var seedArray = bytes_1.arrayify(seed);
|
2018-06-15 04:18:17 -04:00
|
|
|
if (seedArray.length < 16 || seedArray.length > 64) {
|
2018-06-13 15:39:39 -04:00
|
|
|
throw new Error('invalid seed');
|
|
|
|
}
|
2018-08-01 18:05:19 -04:00
|
|
|
var I = bytes_1.arrayify(hmac_1.computeHmac(hmac_1.SupportedAlgorithms.sha512, MasterSecret, seedArray));
|
2019-02-01 19:50:10 -05:00
|
|
|
return new HDNode(_constructorGuard, bytes32(I.slice(0, 32)), null, "0x00000000", bytes32(I.slice(32)), 0, 0, mnemonic, 'm');
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
2019-02-01 19:50:10 -05:00
|
|
|
function fromMnemonic(mnemonic, wordlist, password) {
|
2019-07-09 16:56:49 -04:00
|
|
|
// Normalize the mnemonic (also throws if the checksum is invalid)
|
|
|
|
mnemonic = entropyToMnemonic(mnemonicToEntropy(mnemonic, wordlist), wordlist);
|
2019-02-01 19:50:10 -05:00
|
|
|
return _fromSeed(mnemonicToSeed(mnemonic, password), mnemonic);
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.fromMnemonic = fromMnemonic;
|
|
|
|
function fromSeed(seed) {
|
|
|
|
return _fromSeed(seed, null);
|
|
|
|
}
|
|
|
|
exports.fromSeed = fromSeed;
|
|
|
|
function mnemonicToSeed(mnemonic, password) {
|
|
|
|
if (!password) {
|
|
|
|
password = '';
|
|
|
|
}
|
|
|
|
var salt = utf8_1.toUtf8Bytes('mnemonic' + password, utf8_1.UnicodeNormalizationForm.NFKD);
|
2018-06-19 02:12:57 -04:00
|
|
|
return bytes_1.hexlify(pbkdf2_1.pbkdf2(utf8_1.toUtf8Bytes(mnemonic, utf8_1.UnicodeNormalizationForm.NFKD), salt, 2048, 64, 'sha512'));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.mnemonicToSeed = mnemonicToSeed;
|
2018-06-20 20:29:54 -04:00
|
|
|
function mnemonicToEntropy(mnemonic, wordlist) {
|
|
|
|
if (!wordlist) {
|
|
|
|
wordlist = lang_en_1.langEn;
|
|
|
|
}
|
2018-11-09 14:42:29 -05:00
|
|
|
errors.checkNormalize();
|
2018-06-20 20:29:54 -04:00
|
|
|
var words = wordlist.split(mnemonic);
|
2018-06-13 15:39:39 -04:00
|
|
|
if ((words.length % 3) !== 0) {
|
|
|
|
throw new Error('invalid mnemonic');
|
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
var entropy = bytes_1.arrayify(new Uint8Array(Math.ceil(11 * words.length / 8)));
|
2018-06-13 15:39:39 -04:00
|
|
|
var offset = 0;
|
|
|
|
for (var i = 0; i < words.length; i++) {
|
2018-06-20 20:29:54 -04:00
|
|
|
var index = wordlist.getWordIndex(words[i].normalize('NFKD'));
|
2018-06-13 15:39:39 -04:00
|
|
|
if (index === -1) {
|
|
|
|
throw new Error('invalid mnemonic');
|
|
|
|
}
|
|
|
|
for (var bit = 0; bit < 11; bit++) {
|
|
|
|
if (index & (1 << (10 - bit))) {
|
|
|
|
entropy[offset >> 3] |= (1 << (7 - (offset % 8)));
|
|
|
|
}
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var entropyBits = 32 * words.length / 3;
|
|
|
|
var checksumBits = words.length / 3;
|
|
|
|
var checksumMask = getUpperMask(checksumBits);
|
2018-06-17 16:47:28 -04:00
|
|
|
var checksum = bytes_1.arrayify(sha2_1.sha256(entropy.slice(0, entropyBits / 8)))[0];
|
2018-06-13 15:39:39 -04:00
|
|
|
checksum &= checksumMask;
|
|
|
|
if (checksum !== (entropy[entropy.length - 1] & checksumMask)) {
|
|
|
|
throw new Error('invalid checksum');
|
|
|
|
}
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.hexlify(entropy.slice(0, entropyBits / 8));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.mnemonicToEntropy = mnemonicToEntropy;
|
2018-06-20 20:29:54 -04:00
|
|
|
function entropyToMnemonic(entropy, wordlist) {
|
2018-06-17 16:47:28 -04:00
|
|
|
entropy = bytes_1.arrayify(entropy);
|
2018-06-13 15:39:39 -04:00
|
|
|
if ((entropy.length % 4) !== 0 || entropy.length < 16 || entropy.length > 32) {
|
|
|
|
throw new Error('invalid entropy');
|
|
|
|
}
|
2018-06-15 04:18:17 -04:00
|
|
|
var indices = [0];
|
2018-06-13 15:39:39 -04:00
|
|
|
var remainingBits = 11;
|
|
|
|
for (var i = 0; i < entropy.length; i++) {
|
|
|
|
// Consume the whole byte (with still more to go)
|
|
|
|
if (remainingBits > 8) {
|
2018-06-15 04:18:17 -04:00
|
|
|
indices[indices.length - 1] <<= 8;
|
|
|
|
indices[indices.length - 1] |= entropy[i];
|
2018-06-13 15:39:39 -04:00
|
|
|
remainingBits -= 8;
|
|
|
|
// This byte will complete an 11-bit index
|
|
|
|
}
|
|
|
|
else {
|
2018-06-15 04:18:17 -04:00
|
|
|
indices[indices.length - 1] <<= remainingBits;
|
|
|
|
indices[indices.length - 1] |= entropy[i] >> (8 - remainingBits);
|
2018-06-13 15:39:39 -04:00
|
|
|
// Start the next word
|
2018-06-15 04:18:17 -04:00
|
|
|
indices.push(entropy[i] & getLowerMask(8 - remainingBits));
|
2018-06-13 15:39:39 -04:00
|
|
|
remainingBits += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Compute the checksum bits
|
2018-06-17 16:47:28 -04:00
|
|
|
var checksum = bytes_1.arrayify(sha2_1.sha256(entropy))[0];
|
2018-06-13 15:39:39 -04:00
|
|
|
var checksumBits = entropy.length / 4;
|
|
|
|
checksum &= getUpperMask(checksumBits);
|
|
|
|
// Shift the checksum into the word indices
|
2018-06-15 04:18:17 -04:00
|
|
|
indices[indices.length - 1] <<= checksumBits;
|
|
|
|
indices[indices.length - 1] |= (checksum >> (8 - checksumBits));
|
2018-06-20 20:29:54 -04:00
|
|
|
if (!wordlist) {
|
|
|
|
wordlist = lang_en_1.langEn;
|
|
|
|
}
|
|
|
|
return wordlist.join(indices.map(function (index) { return wordlist.getWord(index); }));
|
2018-06-13 15:39:39 -04:00
|
|
|
}
|
|
|
|
exports.entropyToMnemonic = entropyToMnemonic;
|
2018-06-20 20:29:54 -04:00
|
|
|
function isValidMnemonic(mnemonic, wordlist) {
|
2018-06-13 15:39:39 -04:00
|
|
|
try {
|
2018-06-20 20:29:54 -04:00
|
|
|
mnemonicToEntropy(mnemonic, wordlist);
|
2018-06-13 15:39:39 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
exports.isValidMnemonic = isValidMnemonic;
|