ethers.js/packages/hdnode/lib/index.js

345 lines
15 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2021-05-17 16:19:36 -04:00
exports.getAccountPath = exports.isValidMnemonic = exports.entropyToMnemonic = exports.mnemonicToEntropy = exports.mnemonicToSeed = exports.HDNode = exports.defaultPath = void 0;
2019-05-14 18:48:48 -04:00
var basex_1 = require("@ethersproject/basex");
var bytes_1 = require("@ethersproject/bytes");
var bignumber_1 = require("@ethersproject/bignumber");
var strings_1 = require("@ethersproject/strings");
var pbkdf2_1 = require("@ethersproject/pbkdf2");
var properties_1 = require("@ethersproject/properties");
var signing_key_1 = require("@ethersproject/signing-key");
var sha2_1 = require("@ethersproject/sha2");
var transactions_1 = require("@ethersproject/transactions");
2019-06-12 09:38:15 -04:00
var wordlists_1 = require("@ethersproject/wordlists");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var N = bignumber_1.BigNumber.from("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// "Bitcoin seed"
2021-10-16 02:29:27 -04:00
var MasterSecret = (0, strings_1.toUtf8Bytes)("Bitcoin seed");
2019-05-14 18:48:48 -04:00
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;
}
function bytes32(value) {
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexZeroPad)((0, bytes_1.hexlify)(value), 32);
2019-05-14 18:48:48 -04:00
}
function base58check(data) {
2021-10-16 02:29:27 -04:00
return basex_1.Base58.encode((0, bytes_1.concat)([data, (0, bytes_1.hexDataSlice)((0, sha2_1.sha256)((0, sha2_1.sha256)(data)), 0, 4)]));
2019-05-14 18:48:48 -04:00
}
2020-01-18 21:48:12 -05:00
function getWordlist(wordlist) {
if (wordlist == null) {
return wordlists_1.wordlists["en"];
}
if (typeof (wordlist) === "string") {
var words = wordlists_1.wordlists[wordlist];
if (words == null) {
logger.throwArgumentError("unknown locale", "wordlist", wordlist);
}
return words;
}
return wordlist;
}
2019-05-14 18:48:48 -04:00
var _constructorGuard = {};
exports.defaultPath = "m/44'/60'/0'/0/0";
2020-01-18 21:48:12 -05:00
;
2019-05-14 18:48:48 -04:00
var HDNode = /** @class */ (function () {
/**
* This constructor should not be called directly.
*
* Please use:
* - fromMnemonic
* - fromSeed
*/
2020-01-18 21:48:12 -05:00
function HDNode(constructorGuard, privateKey, publicKey, parentFingerprint, chainCode, index, depth, mnemonicOrPath) {
2019-05-14 18:48:48 -04:00
var _newTarget = this.constructor;
2019-08-02 02:10:58 -04:00
logger.checkNew(_newTarget, HDNode);
2020-07-13 08:03:56 -04:00
/* istanbul ignore if */
2019-05-14 18:48:48 -04:00
if (constructorGuard !== _constructorGuard) {
throw new Error("HDNode constructor cannot be called directly");
}
if (privateKey) {
var signingKey = new signing_key_1.SigningKey(privateKey);
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "privateKey", signingKey.privateKey);
(0, properties_1.defineReadOnly)(this, "publicKey", signingKey.compressedPublicKey);
2019-05-14 18:48:48 -04:00
}
else {
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "privateKey", null);
(0, properties_1.defineReadOnly)(this, "publicKey", (0, bytes_1.hexlify)(publicKey));
2019-05-14 18:48:48 -04:00
}
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "parentFingerprint", parentFingerprint);
(0, properties_1.defineReadOnly)(this, "fingerprint", (0, bytes_1.hexDataSlice)((0, sha2_1.ripemd160)((0, sha2_1.sha256)(this.publicKey)), 0, 4));
(0, properties_1.defineReadOnly)(this, "address", (0, transactions_1.computeAddress)(this.publicKey));
(0, properties_1.defineReadOnly)(this, "chainCode", chainCode);
(0, properties_1.defineReadOnly)(this, "index", index);
(0, properties_1.defineReadOnly)(this, "depth", depth);
2020-01-18 21:48:12 -05:00
if (mnemonicOrPath == null) {
// From a source that does not preserve the path (e.g. extended keys)
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "mnemonic", null);
(0, properties_1.defineReadOnly)(this, "path", null);
2020-01-18 21:48:12 -05:00
}
else if (typeof (mnemonicOrPath) === "string") {
// From a source that does not preserve the mnemonic (e.g. neutered)
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "mnemonic", null);
(0, properties_1.defineReadOnly)(this, "path", mnemonicOrPath);
2020-01-18 21:48:12 -05:00
}
else {
// From a fully qualified source
2021-10-16 02:29:27 -04:00
(0, properties_1.defineReadOnly)(this, "mnemonic", mnemonicOrPath);
(0, properties_1.defineReadOnly)(this, "path", mnemonicOrPath.path);
2020-01-18 21:48:12 -05:00
}
2019-05-14 18:48:48 -04:00
}
Object.defineProperty(HDNode.prototype, "extendedKey", {
get: function () {
// We only support the mainnet values for now, but if anyone needs
2021-10-16 02:29:27 -04:00
// testnet values, let me know. I believe current sentiment is that
2019-05-14 18:48:48 -04:00
// 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!");
}
2021-10-16 02:29:27 -04:00
return base58check((0, bytes_1.concat)([
2019-05-14 18:48:48 -04:00
((this.privateKey != null) ? "0x0488ADE4" : "0x0488B21E"),
2021-10-16 02:29:27 -04:00
(0, bytes_1.hexlify)(this.depth),
2019-05-14 18:48:48 -04:00
this.parentFingerprint,
2021-10-16 02:29:27 -04:00
(0, bytes_1.hexZeroPad)((0, bytes_1.hexlify)(this.index), 4),
2019-05-14 18:48:48 -04:00
this.chainCode,
2021-10-16 02:29:27 -04:00
((this.privateKey != null) ? (0, bytes_1.concat)(["0x00", this.privateKey]) : this.publicKey),
2019-05-14 18:48:48 -04:00
]));
},
2021-03-07 18:24:04 -05:00
enumerable: false,
2019-05-14 18:48:48 -04:00
configurable: true
});
HDNode.prototype.neuter = function () {
2020-01-18 21:48:12 -05:00
return new HDNode(_constructorGuard, null, this.publicKey, this.parentFingerprint, this.chainCode, this.index, this.depth, this.path);
2019-05-14 18:48:48 -04:00
};
HDNode.prototype._derive = function (index) {
if (index > 0xffffffff) {
throw new Error("invalid index - " + String(index));
}
// Base path
var path = this.path;
if (path) {
path += "/" + (index & ~HardenedBit);
}
var data = new Uint8Array(37);
if (index & HardenedBit) {
if (!this.privateKey) {
throw new Error("cannot derive child of neutered node");
}
// Data = 0x00 || ser_256(k_par)
2021-10-16 02:29:27 -04:00
data.set((0, bytes_1.arrayify)(this.privateKey), 1);
2019-05-14 18:48:48 -04:00
// Hardened path
if (path) {
path += "'";
}
}
else {
// Data = ser_p(point(k_par))
2021-10-16 02:29:27 -04:00
data.set((0, bytes_1.arrayify)(this.publicKey));
2019-05-14 18:48:48 -04:00
}
// Data += ser_32(i)
for (var i = 24; i >= 0; i -= 8) {
data[33 + (i >> 3)] = ((index >> (24 - i)) & 0xff);
}
2021-10-16 02:29:27 -04:00
var I = (0, bytes_1.arrayify)((0, sha2_1.computeHmac)(sha2_1.SupportedAlgorithm.sha512, this.chainCode, data));
2019-05-14 18:48:48 -04:00
var IL = I.slice(0, 32);
var IR = I.slice(32);
// The private key
var ki = null;
// The public key
var Ki = null;
if (this.privateKey) {
ki = bytes32(bignumber_1.BigNumber.from(IL).add(this.privateKey).mod(N));
}
else {
2021-10-16 02:29:27 -04:00
var ek = new signing_key_1.SigningKey((0, bytes_1.hexlify)(IL));
2019-05-14 18:48:48 -04:00
Ki = ek._addPoint(this.publicKey);
}
2020-01-18 21:48:12 -05:00
var mnemonicOrPath = path;
var srcMnemonic = this.mnemonic;
if (srcMnemonic) {
mnemonicOrPath = Object.freeze({
phrase: srcMnemonic.phrase,
path: path,
locale: (srcMnemonic.locale || "en")
});
}
return new HDNode(_constructorGuard, ki, Ki, this.fingerprint, bytes32(IR), index, this.depth + 1, mnemonicOrPath);
2019-05-14 18:48:48 -04:00
};
HDNode.prototype.derivePath = function (path) {
var components = path.split("/");
if (components.length === 0 || (components[0] === "m" && this.depth !== 0)) {
throw new Error("invalid path - " + path);
}
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 {
throw new Error("invalid path component - " + component);
2019-05-14 18:48:48 -04:00
}
}
return result;
};
HDNode._fromSeed = function (seed, mnemonic) {
2021-10-16 02:29:27 -04:00
var seedArray = (0, bytes_1.arrayify)(seed);
2019-05-14 18:48:48 -04:00
if (seedArray.length < 16 || seedArray.length > 64) {
throw new Error("invalid seed");
}
2021-10-16 02:29:27 -04:00
var I = (0, bytes_1.arrayify)((0, sha2_1.computeHmac)(sha2_1.SupportedAlgorithm.sha512, MasterSecret, seedArray));
2020-01-18 21:48:12 -05:00
return new HDNode(_constructorGuard, bytes32(I.slice(0, 32)), null, "0x00000000", bytes32(I.slice(32)), 0, 0, mnemonic);
2019-05-14 18:48:48 -04:00
};
HDNode.fromMnemonic = function (mnemonic, password, wordlist) {
2020-01-18 21:48:12 -05:00
// If a locale name was passed in, find the associated wordlist
wordlist = getWordlist(wordlist);
2019-07-09 17:30:10 -04:00
// Normalize the case and spacing in the mnemonic (throws if the mnemonic is invalid)
mnemonic = entropyToMnemonic(mnemonicToEntropy(mnemonic, wordlist), wordlist);
2020-01-18 21:48:12 -05:00
return HDNode._fromSeed(mnemonicToSeed(mnemonic, password), {
phrase: mnemonic,
path: "m",
locale: wordlist.locale
});
2019-05-14 18:48:48 -04:00
};
HDNode.fromSeed = function (seed) {
return HDNode._fromSeed(seed, null);
};
HDNode.fromExtendedKey = function (extendedKey) {
var bytes = basex_1.Base58.decode(extendedKey);
if (bytes.length !== 82 || base58check(bytes.slice(0, 78)) !== extendedKey) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid extended key", "extendedKey", "[REDACTED]");
2019-05-14 18:48:48 -04:00
}
var depth = bytes[4];
2021-10-16 02:29:27 -04:00
var parentFingerprint = (0, bytes_1.hexlify)(bytes.slice(5, 9));
var index = parseInt((0, bytes_1.hexlify)(bytes.slice(9, 13)).substring(2), 16);
var chainCode = (0, bytes_1.hexlify)(bytes.slice(13, 45));
2019-05-14 18:48:48 -04:00
var key = bytes.slice(45, 78);
2021-10-16 02:29:27 -04:00
switch ((0, bytes_1.hexlify)(bytes.slice(0, 4))) {
2019-05-14 18:48:48 -04:00
// Public Key
case "0x0488b21e":
case "0x043587cf":
2021-10-16 02:29:27 -04:00
return new HDNode(_constructorGuard, null, (0, bytes_1.hexlify)(key), parentFingerprint, chainCode, index, depth, null);
2019-05-14 18:48:48 -04:00
// Private Key
case "0x0488ade4":
case "0x04358394 ":
if (key[0] !== 0) {
break;
}
2021-10-16 02:29:27 -04:00
return new HDNode(_constructorGuard, (0, bytes_1.hexlify)(key.slice(1)), null, parentFingerprint, chainCode, index, depth, null);
2019-05-14 18:48:48 -04:00
}
2020-04-25 03:54:54 -04:00
return logger.throwArgumentError("invalid extended key", "extendedKey", "[REDACTED]");
2019-05-14 18:48:48 -04:00
};
return HDNode;
}());
exports.HDNode = HDNode;
function mnemonicToSeed(mnemonic, password) {
if (!password) {
password = "";
}
2021-10-16 02:29:27 -04:00
var salt = (0, strings_1.toUtf8Bytes)("mnemonic" + password, strings_1.UnicodeNormalizationForm.NFKD);
return (0, pbkdf2_1.pbkdf2)((0, strings_1.toUtf8Bytes)(mnemonic, strings_1.UnicodeNormalizationForm.NFKD), salt, 2048, 64, "sha512");
2019-05-14 18:48:48 -04:00
}
exports.mnemonicToSeed = mnemonicToSeed;
function mnemonicToEntropy(mnemonic, wordlist) {
2020-01-18 21:48:12 -05:00
wordlist = getWordlist(wordlist);
2019-08-02 02:10:58 -04:00
logger.checkNormalize();
2019-05-14 18:48:48 -04:00
var words = wordlist.split(mnemonic);
if ((words.length % 3) !== 0) {
throw new Error("invalid mnemonic");
}
2021-10-16 02:29:27 -04:00
var entropy = (0, bytes_1.arrayify)(new Uint8Array(Math.ceil(11 * words.length / 8)));
2019-05-14 18:48:48 -04:00
var offset = 0;
for (var i = 0; i < words.length; i++) {
var index = wordlist.getWordIndex(words[i].normalize("NFKD"));
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);
2021-10-16 02:29:27 -04:00
var checksum = (0, bytes_1.arrayify)((0, sha2_1.sha256)(entropy.slice(0, entropyBits / 8)))[0] & checksumMask;
2019-05-14 18:48:48 -04:00
if (checksum !== (entropy[entropy.length - 1] & checksumMask)) {
throw new Error("invalid checksum");
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexlify)(entropy.slice(0, entropyBits / 8));
2019-05-14 18:48:48 -04:00
}
exports.mnemonicToEntropy = mnemonicToEntropy;
function entropyToMnemonic(entropy, wordlist) {
2020-01-18 21:48:12 -05:00
wordlist = getWordlist(wordlist);
2021-10-16 02:29:27 -04:00
entropy = (0, bytes_1.arrayify)(entropy);
2019-05-14 18:48:48 -04:00
if ((entropy.length % 4) !== 0 || entropy.length < 16 || entropy.length > 32) {
throw new Error("invalid entropy");
}
var indices = [0];
var remainingBits = 11;
for (var i = 0; i < entropy.length; i++) {
// Consume the whole byte (with still more to go)
if (remainingBits > 8) {
indices[indices.length - 1] <<= 8;
indices[indices.length - 1] |= entropy[i];
remainingBits -= 8;
// This byte will complete an 11-bit index
}
else {
indices[indices.length - 1] <<= remainingBits;
indices[indices.length - 1] |= entropy[i] >> (8 - remainingBits);
// Start the next word
indices.push(entropy[i] & getLowerMask(8 - remainingBits));
remainingBits += 3;
}
}
// Compute the checksum bits
var checksumBits = entropy.length / 4;
2021-10-16 02:29:27 -04:00
var checksum = (0, bytes_1.arrayify)((0, sha2_1.sha256)(entropy))[0] & getUpperMask(checksumBits);
2019-05-14 18:48:48 -04:00
// Shift the checksum into the word indices
indices[indices.length - 1] <<= checksumBits;
indices[indices.length - 1] |= (checksum >> (8 - checksumBits));
return wordlist.join(indices.map(function (index) { return wordlist.getWord(index); }));
}
exports.entropyToMnemonic = entropyToMnemonic;
function isValidMnemonic(mnemonic, wordlist) {
try {
mnemonicToEntropy(mnemonic, wordlist);
return true;
}
catch (error) { }
return false;
}
exports.isValidMnemonic = isValidMnemonic;
2021-05-17 16:19:36 -04:00
function getAccountPath(index) {
if (typeof (index) !== "number" || index < 0 || index >= HardenedBit || index % 1) {
logger.throwArgumentError("invalid account index", "index", index);
}
return "m/44'/60'/" + index + "'/0/0";
}
exports.getAccountPath = getAccountPath;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=index.js.map