UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.Path = exports.Bip44Path = void 0;
23const TransportErrors = __importStar(require("./errors"));
24/**
25 * Static builder for handling the parsing of Bip44 Paths.
26 *
27 * BIP44 is a particular application of BIP43.
28 * It defines a hierarchy for deterministic wallets based on BIP32,
29 * and the purpose scheme described in BIP43.
30 *
31 * A Bip44 path defines the following levels:
32 * - m / purpose' / coin_type' / account' / change / address_index
33 *
34 * https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
35 * https://github.com/bitcoin/bips/blob/master/bip-0043.mediawiki
36 * https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
37 *
38 * @example const bip44Bytes = Bip44Path.fromString("44'/111'/0'/0/0").toBytes()
39 * @example const bip44Bytes = Bip44Path.fromString("44'/111'/0'").toBytes()
40 * @example const bip44Bytes = Bip44Path.fromString("m/44'/111'/0'/0/0").toBytes()
41 */
42class Bip44Path {
43 /**
44 * Private constructor.
45 * Ensures precondition that 'fromString' is called before 'toBytes'.
46 *
47 * @param {number[]} elements a bip44 path as an array of elements
48 */
49 constructor(elements) {
50 Object.defineProperty(this, "_elements", {
51 enumerable: true,
52 configurable: true,
53 writable: true,
54 value: []
55 });
56 this._elements = elements;
57 }
58 /**
59 * Parses a Bip44 path-string, storing the path as elements,
60 * and returns a Bip44Path instance.
61 *
62 * Elements are stored as a 4-byte/uint32 Big-Endian-packed number array.
63 *
64 * @param {string} path a bip44 path as a string
65 * @throws {Error} if the path-string is null
66 * @throws {Error} if the path formatting is invalid
67 * @returns {Bip44Path} a new instance containing parsed path elements
68 */
69 static fromString(path) {
70 if (!path.toString().match(new RegExp(this.REGEXP_VALID_BIP44, "g"))) {
71 throw new TransportErrors.Bip44PathError(path);
72 }
73 return this.pathToElements(path.replace("m/", ""));
74 }
75 /**
76 * Parses and stores a Bip44 Path-string as an array of elements to the 'Bip44Path' instance.
77 *
78 * @param {string} path a bip44 path as a string
79 * @throws {Error} if the path-string is null
80 * @throws {Error} if the path-string has a length of '0'
81 * @returns {Bip44Path} a new instance containing parsed path elements
82 */
83 static pathToElements(path) {
84 const _elements = [];
85 for (const level of path.split("/")) {
86 let element = parseInt(level, 10);
87 if (level.length > 1 && level.endsWith("'")) {
88 // Use hardening
89 element += this.HARDENING;
90 }
91 _elements.push(element);
92 }
93 return new Bip44Path(_elements);
94 }
95 /**
96 * Get the bytes of a Parsed Bip44 Element Array.
97 *
98 * @returns {Buffer} a buffer of bytes representing the path
99 * @throws {Error} if the internal bip44 element array has a length of '0'
100 * @returns {Buffer} a byte buffer of parsed bip44 path elements
101 */
102 toBytes() {
103 const payload = Buffer.alloc(1 + this._elements.length * 4);
104 payload[0] = this._elements.length;
105 let index = 0;
106 for (const element of this._elements) {
107 payload.writeUInt32BE(element, 1 + index * 4);
108 index += 1;
109 }
110 return payload;
111 }
112}
113exports.Bip44Path = Bip44Path;
114exports.Path = Bip44Path;
115Object.defineProperty(Bip44Path, "HARDENING", {
116 enumerable: true,
117 configurable: true,
118 writable: true,
119 value: 0x80000000
120});
121Object.defineProperty(Bip44Path, "REGEXP_VALID_BIP44", {
122 enumerable: true,
123 configurable: true,
124 writable: true,
125 value: "^((m/)?(44'?))(/[0-9]+'?){2}((/[0-9]+){2})?$"
126});
127//# sourceMappingURL=bip44.js.map
\No newline at end of file