@dignetwork/dig-sdk 0.0.1-alpha.197 → 0.0.1-alpha.199
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.
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* SubDomain Class
|
|
3
3
|
*
|
|
4
4
|
* Encapsulates the logic for encoding and decoding a combination of
|
|
5
|
-
* chain and storeId into a DNS-friendly identifier using
|
|
5
|
+
* chain and storeId into a DNS-friendly identifier using Base36 encoding.
|
|
6
6
|
*/
|
|
7
7
|
declare class SubDomain {
|
|
8
|
-
private static readonly
|
|
9
|
-
private static
|
|
8
|
+
private static readonly BASE36_CHARSET;
|
|
9
|
+
private static base36;
|
|
10
10
|
private static readonly STORE_ID_LENGTH;
|
|
11
11
|
readonly chain: string;
|
|
12
12
|
readonly storeId: string;
|
|
@@ -14,7 +14,7 @@ declare class SubDomain {
|
|
|
14
14
|
/**
|
|
15
15
|
* Constructor for SubDomain
|
|
16
16
|
*
|
|
17
|
-
* @param chain - The chain name (e.g., "
|
|
17
|
+
* @param chain - The chain name (e.g., "chain1234").
|
|
18
18
|
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
19
19
|
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
20
20
|
*/
|
|
@@ -22,14 +22,14 @@ declare class SubDomain {
|
|
|
22
22
|
/**
|
|
23
23
|
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
24
24
|
*
|
|
25
|
-
* @returns The
|
|
25
|
+
* @returns The Base36-encoded identifier.
|
|
26
26
|
* @throws Will throw an error if encoding fails.
|
|
27
27
|
*/
|
|
28
28
|
private encode;
|
|
29
29
|
/**
|
|
30
30
|
* Decodes the provided identifier back into the original chain and storeId.
|
|
31
31
|
*
|
|
32
|
-
* @param encodedId - The
|
|
32
|
+
* @param encodedId - The Base36-encoded identifier.
|
|
33
33
|
* @returns An object containing the original chain and storeId.
|
|
34
34
|
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
35
35
|
*/
|
|
@@ -1 +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,
|
|
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,CAA0C;IAGhF,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;CAqC5E;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/utils/Subdomain.js
CHANGED
|
@@ -10,13 +10,13 @@ const base_x_1 = __importDefault(require("base-x"));
|
|
|
10
10
|
* SubDomain Class
|
|
11
11
|
*
|
|
12
12
|
* Encapsulates the logic for encoding and decoding a combination of
|
|
13
|
-
* chain and storeId into a DNS-friendly identifier using
|
|
13
|
+
* chain and storeId into a DNS-friendly identifier using Base36 encoding.
|
|
14
14
|
*/
|
|
15
15
|
class SubDomain {
|
|
16
16
|
/**
|
|
17
17
|
* Constructor for SubDomain
|
|
18
18
|
*
|
|
19
|
-
* @param chain - The chain name (e.g., "
|
|
19
|
+
* @param chain - The chain name (e.g., "chain1234").
|
|
20
20
|
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
21
21
|
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
22
22
|
*/
|
|
@@ -28,7 +28,7 @@ class SubDomain {
|
|
|
28
28
|
/**
|
|
29
29
|
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
30
30
|
*
|
|
31
|
-
* @returns The
|
|
31
|
+
* @returns The Base36-encoded identifier.
|
|
32
32
|
* @throws Will throw an error if encoding fails.
|
|
33
33
|
*/
|
|
34
34
|
encode() {
|
|
@@ -46,7 +46,7 @@ class SubDomain {
|
|
|
46
46
|
if (chainLength < 1 || chainLength > 255) {
|
|
47
47
|
throw new Error("Invalid chain: Length must be between 1 and 255 characters.");
|
|
48
48
|
}
|
|
49
|
-
// Convert chain length to a single byte
|
|
49
|
+
// Convert chain length to a single byte
|
|
50
50
|
const chainLengthBuffer = Buffer.from([chainLength]);
|
|
51
51
|
// Convert chain to a Buffer (UTF-8)
|
|
52
52
|
const chainBuffer = Buffer.from(this.chain, "utf8");
|
|
@@ -62,8 +62,8 @@ class SubDomain {
|
|
|
62
62
|
chainBuffer,
|
|
63
63
|
storeIdBuffer,
|
|
64
64
|
]);
|
|
65
|
-
// Encode the data buffer using
|
|
66
|
-
const encodedId = SubDomain.
|
|
65
|
+
// Encode the data buffer using Base36
|
|
66
|
+
const encodedId = SubDomain.base36.encode(dataBuffer);
|
|
67
67
|
// Ensure DNS label length does not exceed 63 characters
|
|
68
68
|
if (encodedId.length > 63) {
|
|
69
69
|
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
@@ -73,7 +73,7 @@ class SubDomain {
|
|
|
73
73
|
/**
|
|
74
74
|
* Decodes the provided identifier back into the original chain and storeId.
|
|
75
75
|
*
|
|
76
|
-
* @param encodedId - The
|
|
76
|
+
* @param encodedId - The Base36-encoded identifier.
|
|
77
77
|
* @returns An object containing the original chain and storeId.
|
|
78
78
|
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
79
79
|
*/
|
|
@@ -82,10 +82,10 @@ class SubDomain {
|
|
|
82
82
|
if (!encodedId || typeof encodedId !== "string") {
|
|
83
83
|
throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
|
|
84
84
|
}
|
|
85
|
-
// Decode the
|
|
86
|
-
const decodedBuffer = SubDomain.
|
|
85
|
+
// Decode the Base36 string back to a Buffer
|
|
86
|
+
const decodedBuffer = SubDomain.base36.decode(encodedId);
|
|
87
87
|
if (!decodedBuffer) {
|
|
88
|
-
throw new Error("Failed to decode
|
|
88
|
+
throw new Error("Failed to decode Base36 string.");
|
|
89
89
|
}
|
|
90
90
|
// Ensure there's at least 1 byte for chain_length and STORE_ID_LENGTH bytes for storeId
|
|
91
91
|
if (decodedBuffer.length < 1 + SubDomain.STORE_ID_LENGTH) {
|
|
@@ -93,13 +93,9 @@ class SubDomain {
|
|
|
93
93
|
}
|
|
94
94
|
// Extract chain_length (1 byte)
|
|
95
95
|
const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
|
|
96
|
-
//
|
|
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
|
|
96
|
+
// Extract chain
|
|
102
97
|
const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
|
|
98
|
+
// Extract storeId
|
|
103
99
|
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
|
|
104
100
|
// Convert storeId buffer to hex string
|
|
105
101
|
const storeId = Buffer.from(storeIdBuffer).toString("hex");
|
|
@@ -107,9 +103,9 @@ class SubDomain {
|
|
|
107
103
|
}
|
|
108
104
|
}
|
|
109
105
|
exports.SubDomain = SubDomain;
|
|
110
|
-
// Define the
|
|
111
|
-
SubDomain.
|
|
112
|
-
// Initialize the
|
|
113
|
-
SubDomain.
|
|
106
|
+
// Define the Base36 character set (only lowercase letters and digits)
|
|
107
|
+
SubDomain.BASE36_CHARSET = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
108
|
+
// Initialize the Base36 encoder/decoder
|
|
109
|
+
SubDomain.base36 = (0, base_x_1.default)(SubDomain.BASE36_CHARSET);
|
|
114
110
|
// Define expected byte length for storeId
|
|
115
111
|
SubDomain.STORE_ID_LENGTH = 32; // bytes
|