UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DeriveJunction = void 0;
4const util_1 = require("@polkadot/util");
5const asU8a_js_1 = require("../blake2/asU8a.js");
6const bn_js_1 = require("../bn.js");
7const RE_NUMBER = /^\d+$/;
8const JUNCTION_ID_LEN = 32;
9class DeriveJunction {
10 __internal__chainCode = new Uint8Array(32);
11 __internal__isHard = false;
12 static from(value) {
13 const result = new DeriveJunction();
14 const [code, isHard] = value.startsWith('/')
15 ? [value.substring(1), true]
16 : [value, false];
17 result.soft(RE_NUMBER.test(code)
18 ? new util_1.BN(code, 10)
19 : code);
20 return isHard
21 ? result.harden()
22 : result;
23 }
24 get chainCode() {
25 return this.__internal__chainCode;
26 }
27 get isHard() {
28 return this.__internal__isHard;
29 }
30 get isSoft() {
31 return !this.__internal__isHard;
32 }
33 hard(value) {
34 return this.soft(value).harden();
35 }
36 harden() {
37 this.__internal__isHard = true;
38 return this;
39 }
40 soft(value) {
41 if ((0, util_1.isNumber)(value) || (0, util_1.isBn)(value) || (0, util_1.isBigInt)(value)) {
42 return this.soft((0, util_1.bnToU8a)(value, bn_js_1.BN_LE_256_OPTS));
43 }
44 else if ((0, util_1.isHex)(value)) {
45 return this.soft((0, util_1.hexToU8a)(value));
46 }
47 else if ((0, util_1.isString)(value)) {
48 return this.soft((0, util_1.compactAddLength)((0, util_1.stringToU8a)(value)));
49 }
50 else if (value.length > JUNCTION_ID_LEN) {
51 return this.soft((0, asU8a_js_1.blake2AsU8a)(value));
52 }
53 this.__internal__chainCode.fill(0);
54 this.__internal__chainCode.set(value, 0);
55 return this;
56 }
57 soften() {
58 this.__internal__isHard = false;
59 return this;
60 }
61}
62exports.DeriveJunction = DeriveJunction;