Skip to content

Commit

Permalink
feat<hd>: added KeyOriginInfo class
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasu-08 committed Jul 17, 2023
1 parent bb0375e commit ff1ef80
Show file tree
Hide file tree
Showing 4 changed files with 625 additions and 21 deletions.
85 changes: 64 additions & 21 deletions lib/hd/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,27 @@ common.MAX_ENTROPY = 512;
common.cache = new LRU(500);

/**
* Parse a derivation path and return an array of indexes.
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
* @param {String} path
* Parse a derivation path.
* @param {Array} path
* @param {Boolean} hard
* @returns {Number[]}
*/

common.parsePath = function parsePath(path, hard) {
assert(typeof path === 'string');
common.parsePathFromArray = function parsePathFromArray(path, hard) {
assert(Array.isArray(path), 'Path must be an array.');
assert(typeof hard === 'boolean');
assert(path.length >= 1);
assert(path.length <= 3062);

const parts = path.split('/');
const root = parts[0];

if (root !== 'm'
&& root !== 'M'
&& root !== 'm\''
&& root !== 'M\'') {
throw new Error('Invalid path root.');
}

const result = [];
for (let i = 0; i < path.length; i++) {
let part = path[i];

for (let i = 1; i < parts.length; i++) {
let part = parts[i];
const last = part[part.length - 1];

const hardened = part[part.length - 1] === '\'';
const hardened = last === '\'' || last === 'h';

if (hardened)
if (hardened) {
part = part.slice(0, -1);
}

if (part.length > 10)
throw new Error('Path index too large.');
Expand All @@ -86,6 +75,10 @@ common.parsePath = function parsePath(path, hard) {
if ((index >>> 0) !== index)
throw new Error('Path index out of range.');

if (index > 0x7fffffff) {
throw new Error(`Key path value ${index} is out of range`);
}

if (hardened) {
index |= common.HARDENED;
index >>>= 0;
Expand All @@ -100,6 +93,56 @@ common.parsePath = function parsePath(path, hard) {
return result;
};

/**
* Parse a derivation path and return an array of indexes.
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
* @param {String} path
* @param {Boolean} hard
* @returns {Number[]}
*/

common.parsePath = function parsePath(path, hard) {
assert(typeof path === 'string');
assert(typeof hard === 'boolean');
assert(path.length >= 1);
assert(path.length <= 3062);

let parts = path.split('/');
const root = parts[0];
parts = parts.slice(1);

if (root !== 'm'
&& root !== 'M'
&& root !== 'm\''
&& root !== 'M\'') {
throw new Error('Invalid path root.');
}

return this.parsePathFromArray(parts, hard);
};

/**
* Format the derivation path (indexes).
* @param {Array} path
* @param {String} hardenedMarker `h` or `'`
* Whether to format path using apostrophes (e.g. `m/44'/0'/0'`)
* or with h (e.g. `m/44h/0h/0h`).
* @returns {String}
*/

common.format = function format(path, hardenedMarker) {
assert(Array.isArray(path));
assert(typeof hardenedMarker === 'string');
assert(hardenedMarker === '\'' || hardenedMarker === 'h');

let res = '';
for (const p of path) {
const hardened = p & common.HARDENED ? hardenedMarker : '';
res += `/${p & 0x7fffffff}${hardened}`;
}
return res;
};

/**
* Test whether the key is a master key.
* @param {HDPrivateKey|HDPublicKey} key
Expand Down
2 changes: 2 additions & 0 deletions lib/hd/hd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const common = require('./common');
const Mnemonic = require('./mnemonic');
const HDPrivateKey = require('./private');
const HDPublicKey = require('./public');
const KeyOriginInfo = require('./keyorigininfo');
const wordlist = require('./wordlist');

/**
Expand Down Expand Up @@ -182,4 +183,5 @@ HD.PrivateKey = HDPrivateKey;
HD.PublicKey = HDPublicKey;
HD.HDPrivateKey = HDPrivateKey;
HD.HDPublicKey = HDPublicKey;
HD.KeyOriginInfo = KeyOriginInfo;
HD.wordlist = wordlist;
Loading

0 comments on commit ff1ef80

Please sign in to comment.