UNPKG

1.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const bip32_1 = require("bip32");
4const bip39_1 = require("bip39");
5const managers_1 = require("../managers");
6class HDWallet {
7 /**
8 * Get root node from the given mnemonic with an optional passphrase.
9 */
10 static fromMnemonic(mnemonic, passphrase) {
11 return bip32_1.fromSeed(bip39_1.mnemonicToSeedSync(mnemonic, passphrase), managers_1.configManager.get("network"));
12 }
13 /**
14 * Get bip32 node from keys.
15 */
16 static fromKeys(keys, chainCode) {
17 if (!keys.compressed) {
18 throw new TypeError("BIP32 only allows compressed keys.");
19 }
20 return bip32_1.fromPrivateKey(Buffer.from(keys.privateKey, "hex"), chainCode, managers_1.configManager.get("network"));
21 }
22 /**
23 * Get key pair from the given node.
24 */
25 static getKeys(node) {
26 return {
27 publicKey: node.publicKey.toString("hex"),
28 privateKey: node.privateKey.toString("hex"),
29 compressed: true,
30 };
31 }
32 /**
33 * Derives a node from the coin type as specified by slip44.
34 */
35 static deriveSlip44(root, hardened = true) {
36 return root.derivePath(`m/44'/${this.slip44}${hardened ? "'" : ""}`);
37 }
38 /**
39 * Derives a node from the network as specified by AIP20.
40 */
41 static deriveNetwork(root) {
42 return this.deriveSlip44(root).deriveHardened(managers_1.configManager.get("network.aip20") || 1);
43 }
44}
45HDWallet.slip44 = 111;
46exports.HDWallet = HDWallet;
47//# sourceMappingURL=hdwallet.js.map
\No newline at end of file