@machinomy/hdwallet-provider
Version:
HD Wallet-enabled Web3 provider
31 lines • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.componentsFromPath = exports.ensurePath = exports.InvalidDerivationPathError = exports.normalizePath = exports.DEFAULT_PATH = void 0;
const ALLOWED_PATHS = ["44'/0'", "44'/1'", "44'/60'", "44'/61'"];
exports.DEFAULT_PATH = "m/44'/60'/0'/0";
function normalizePath(path) {
const usedPath = path || exports.DEFAULT_PATH;
return usedPath.replace(/^m\//, "");
}
exports.normalizePath = normalizePath;
class InvalidDerivationPathError extends Error {
}
exports.InvalidDerivationPathError = InvalidDerivationPathError;
function ensurePath(path) {
if (!ALLOWED_PATHS.some(hdPref => path.startsWith(hdPref))) {
throw new InvalidDerivationPathError(`Ledger derivation path allowed are ${ALLOWED_PATHS.join(", ")}. ${path} is not supported`);
}
}
exports.ensurePath = ensurePath;
function componentsFromPath(path) {
ensurePath(path);
// check if derivation path follows 44'/60'/x'/n or 44'/60'/x'/y/n pattern
const regExp = /^(44'\/(?:0|1|60|61)'\/\d+'?\/(?:\d+\/)?)(\d+)$/;
const matchResult = regExp.exec(path);
if (matchResult === null) {
throw new InvalidDerivationPathError("To get multiple accounts your derivation path must follow pattern 44'/60|61'/x'/n or 44'/60'/x'/y/n");
}
return { basePath: matchResult[1], index: parseInt(matchResult[2], 10) };
}
exports.componentsFromPath = componentsFromPath;
//# sourceMappingURL=path.util.js.map