@dignetwork/dig-sdk 0.0.1-alpha.195 → 0.0.1-alpha.197

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * SubDomain Class
3
+ *
4
+ * Encapsulates the logic for encoding and decoding a combination of
5
+ * chain and storeId into a DNS-friendly identifier using Base62 encoding.
6
+ */
7
+ declare class SubDomain {
8
+ private static readonly BASE62_CHARSET;
9
+ private static base62;
10
+ private static readonly STORE_ID_LENGTH;
11
+ readonly chain: string;
12
+ readonly storeId: string;
13
+ readonly encodedId: string;
14
+ /**
15
+ * Constructor for SubDomain
16
+ *
17
+ * @param chain - The chain name (e.g., "CHAIN1234").
18
+ * @param storeId - The store ID as a 64-character hexadecimal string.
19
+ * @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
20
+ */
21
+ constructor(chain: string, storeId: string);
22
+ /**
23
+ * Encodes the provided chain and storeId into a DNS-friendly identifier.
24
+ *
25
+ * @returns The Base62-encoded identifier.
26
+ * @throws Will throw an error if encoding fails.
27
+ */
28
+ private encode;
29
+ /**
30
+ * Decodes the provided identifier back into the original chain and storeId.
31
+ *
32
+ * @param encodedId - The Base62-encoded identifier.
33
+ * @returns An object containing the original chain and storeId.
34
+ * @throws Will throw an error if decoding fails or data lengths mismatch.
35
+ */
36
+ static decode(encodedId: string): {
37
+ chain: string;
38
+ storeId: string;
39
+ };
40
+ }
41
+ export { SubDomain };
42
+ //# sourceMappingURL=Subdomain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Subdomain.d.ts","sourceRoot":"","sources":["../../src/utils/Subdomain.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,cAAM,SAAS;IAEb,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAC6B;IAGnE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAmC;IAGxD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAM;IAG7C,SAAgB,KAAK,EAAE,MAAM,CAAC;IAC9B,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC;;;;;;OAMG;gBACS,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAM1C;;;;;OAKG;IACH,OAAO,CAAC,MAAM;IA4Dd;;;;;;OAMG;WACW,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;CA4C5E;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ // SubDomain.ts
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SubDomain = void 0;
8
+ const base_x_1 = __importDefault(require("base-x"));
9
+ /**
10
+ * SubDomain Class
11
+ *
12
+ * Encapsulates the logic for encoding and decoding a combination of
13
+ * chain and storeId into a DNS-friendly identifier using Base62 encoding.
14
+ */
15
+ class SubDomain {
16
+ /**
17
+ * Constructor for SubDomain
18
+ *
19
+ * @param chain - The chain name (e.g., "CHAIN1234").
20
+ * @param storeId - The store ID as a 64-character hexadecimal string.
21
+ * @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
22
+ */
23
+ constructor(chain, storeId) {
24
+ this.chain = chain;
25
+ this.storeId = storeId;
26
+ this.encodedId = this.encode();
27
+ }
28
+ /**
29
+ * Encodes the provided chain and storeId into a DNS-friendly identifier.
30
+ *
31
+ * @returns The Base62-encoded identifier.
32
+ * @throws Will throw an error if encoding fails.
33
+ */
34
+ encode() {
35
+ // Validate inputs
36
+ if (!this.chain || typeof this.chain !== "string") {
37
+ throw new Error("Invalid chain: Chain must be a non-empty string.");
38
+ }
39
+ if (!this.storeId ||
40
+ typeof this.storeId !== "string" ||
41
+ !/^[0-9a-fA-F]{64}$/.test(this.storeId)) {
42
+ throw new Error("Invalid storeId: StoreId must be a 64-character hexadecimal string.");
43
+ }
44
+ // Ensure the chain length is within 1-255 characters to fit in one byte
45
+ const chainLength = this.chain.length;
46
+ if (chainLength < 1 || chainLength > 255) {
47
+ throw new Error("Invalid chain: Length must be between 1 and 255 characters.");
48
+ }
49
+ // Convert chain length to a single byte Buffer
50
+ const chainLengthBuffer = Buffer.from([chainLength]);
51
+ // Convert chain to a Buffer (UTF-8)
52
+ const chainBuffer = Buffer.from(this.chain, "utf8");
53
+ // Convert storeId from hex string to Buffer
54
+ const storeIdBuffer = Buffer.from(this.storeId, "hex");
55
+ // Validate storeId byte length
56
+ if (storeIdBuffer.length !== SubDomain.STORE_ID_LENGTH) {
57
+ throw new Error(`Invalid storeId length: Expected ${SubDomain.STORE_ID_LENGTH} bytes, got ${storeIdBuffer.length} bytes.`);
58
+ }
59
+ // Concatenate chain_length, chain, and storeId buffers
60
+ const dataBuffer = Buffer.concat([
61
+ chainLengthBuffer,
62
+ chainBuffer,
63
+ storeIdBuffer,
64
+ ]);
65
+ // Encode the data buffer using Base62
66
+ const encodedId = SubDomain.base62.encode(dataBuffer);
67
+ // Ensure DNS label length does not exceed 63 characters
68
+ if (encodedId.length > 63) {
69
+ throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
70
+ }
71
+ return encodedId;
72
+ }
73
+ /**
74
+ * Decodes the provided identifier back into the original chain and storeId.
75
+ *
76
+ * @param encodedId - The Base62-encoded identifier.
77
+ * @returns An object containing the original chain and storeId.
78
+ * @throws Will throw an error if decoding fails or data lengths mismatch.
79
+ */
80
+ static decode(encodedId) {
81
+ // Validate input
82
+ if (!encodedId || typeof encodedId !== "string") {
83
+ throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
84
+ }
85
+ // Decode the Base62 string back to a Buffer
86
+ const decodedBuffer = SubDomain.base62.decode(encodedId);
87
+ if (!decodedBuffer) {
88
+ throw new Error("Failed to decode Base62 string.");
89
+ }
90
+ // Ensure there's at least 1 byte for chain_length and STORE_ID_LENGTH bytes for storeId
91
+ if (decodedBuffer.length < 1 + SubDomain.STORE_ID_LENGTH) {
92
+ throw new Error("Decoded data is too short to contain required fields.");
93
+ }
94
+ // Extract chain_length (1 byte)
95
+ const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
96
+ // Define the expected total length
97
+ const expected_length = 1 + chain_length + SubDomain.STORE_ID_LENGTH;
98
+ if (decodedBuffer.length !== expected_length) {
99
+ throw new Error(`Decoded data length mismatch: expected ${expected_length} bytes, got ${decodedBuffer.length} bytes.`);
100
+ }
101
+ // Extract chain and storeId from the buffer
102
+ const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
103
+ const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
104
+ // Convert storeId buffer to hex string
105
+ const storeId = Buffer.from(storeIdBuffer).toString("hex");
106
+ return { chain, storeId };
107
+ }
108
+ }
109
+ exports.SubDomain = SubDomain;
110
+ // Define the Base62 character set
111
+ SubDomain.BASE62_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
112
+ // Initialize the Base62 encoder/decoder
113
+ SubDomain.base62 = (0, base_x_1.default)(SubDomain.BASE62_CHARSET);
114
+ // Define expected byte length for storeId
115
+ SubDomain.STORE_ID_LENGTH = 32; // bytes
@@ -15,4 +15,5 @@ export * from './promiseUtils';
15
15
  export * from './PeerRanker';
16
16
  export * from './DigCache';
17
17
  export * from './Udi';
18
+ export * from './Subdomain';
18
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC"}
@@ -31,3 +31,4 @@ __exportStar(require("./promiseUtils"), exports);
31
31
  __exportStar(require("./PeerRanker"), exports);
32
32
  __exportStar(require("./DigCache"), exports);
33
33
  __exportStar(require("./Udi"), exports);
34
+ __exportStar(require("./Subdomain"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.195",
3
+ "version": "0.0.1-alpha.197",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -30,6 +30,7 @@
30
30
  "archiver": "^7.0.1",
31
31
  "async-mutex": "^0.5.0",
32
32
  "axios": "^1.7.7",
33
+ "base-x": "^5.0.0",
33
34
  "bip39": "^3.1.0",
34
35
  "bottleneck": "^2.19.5",
35
36
  "chia-bls": "^1.0.2",