@dignetwork/dig-sdk 0.0.1-alpha.198 → 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,10 +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
|
|
8
|
+
private static readonly BASE36_CHARSET;
|
|
9
|
+
private static base36;
|
|
9
10
|
private static readonly STORE_ID_LENGTH;
|
|
10
11
|
readonly chain: string;
|
|
11
12
|
readonly storeId: string;
|
|
@@ -13,7 +14,7 @@ declare class SubDomain {
|
|
|
13
14
|
/**
|
|
14
15
|
* Constructor for SubDomain
|
|
15
16
|
*
|
|
16
|
-
* @param chain - The chain name (e.g., "
|
|
17
|
+
* @param chain - The chain name (e.g., "chain1234").
|
|
17
18
|
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
18
19
|
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
19
20
|
*/
|
|
@@ -21,14 +22,14 @@ declare class SubDomain {
|
|
|
21
22
|
/**
|
|
22
23
|
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
23
24
|
*
|
|
24
|
-
* @returns The
|
|
25
|
+
* @returns The Base36-encoded identifier.
|
|
25
26
|
* @throws Will throw an error if encoding fails.
|
|
26
27
|
*/
|
|
27
28
|
private encode;
|
|
28
29
|
/**
|
|
29
30
|
* Decodes the provided identifier back into the original chain and storeId.
|
|
30
31
|
*
|
|
31
|
-
* @param encodedId - The
|
|
32
|
+
* @param encodedId - The Base36-encoded identifier.
|
|
32
33
|
* @returns An object containing the original chain and storeId.
|
|
33
34
|
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
34
35
|
*/
|
|
@@ -36,20 +37,6 @@ declare class SubDomain {
|
|
|
36
37
|
chain: string;
|
|
37
38
|
storeId: string;
|
|
38
39
|
};
|
|
39
|
-
/**
|
|
40
|
-
* Encodes a Buffer into a Base62 string.
|
|
41
|
-
*
|
|
42
|
-
* @param buffer - The Buffer to encode.
|
|
43
|
-
* @returns The Base62-encoded string.
|
|
44
|
-
*/
|
|
45
|
-
private static encodeBase62;
|
|
46
|
-
/**
|
|
47
|
-
* Decodes a Base62 string into a Buffer.
|
|
48
|
-
*
|
|
49
|
-
* @param str - The Base62 string to decode.
|
|
50
|
-
* @returns The decoded Buffer.
|
|
51
|
-
*/
|
|
52
|
-
private static decodeBase62;
|
|
53
40
|
}
|
|
54
41
|
export { SubDomain };
|
|
55
42
|
//# sourceMappingURL=Subdomain.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Subdomain.d.ts","sourceRoot":"","sources":["../../src/utils/Subdomain.ts"],"names":[],"mappings":"
|
|
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
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// SubDomain.ts
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
3
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
7
|
exports.SubDomain = void 0;
|
|
8
|
+
const base_x_1 = __importDefault(require("base-x"));
|
|
5
9
|
/**
|
|
6
10
|
* SubDomain Class
|
|
7
11
|
*
|
|
8
12
|
* Encapsulates the logic for encoding and decoding a combination of
|
|
9
|
-
* chain and storeId into a DNS-friendly identifier using
|
|
13
|
+
* chain and storeId into a DNS-friendly identifier using Base36 encoding.
|
|
10
14
|
*/
|
|
11
15
|
class SubDomain {
|
|
12
16
|
/**
|
|
13
17
|
* Constructor for SubDomain
|
|
14
18
|
*
|
|
15
|
-
* @param chain - The chain name (e.g., "
|
|
19
|
+
* @param chain - The chain name (e.g., "chain1234").
|
|
16
20
|
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
17
21
|
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
18
22
|
*/
|
|
@@ -24,7 +28,7 @@ class SubDomain {
|
|
|
24
28
|
/**
|
|
25
29
|
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
26
30
|
*
|
|
27
|
-
* @returns The
|
|
31
|
+
* @returns The Base36-encoded identifier.
|
|
28
32
|
* @throws Will throw an error if encoding fails.
|
|
29
33
|
*/
|
|
30
34
|
encode() {
|
|
@@ -58,8 +62,8 @@ class SubDomain {
|
|
|
58
62
|
chainBuffer,
|
|
59
63
|
storeIdBuffer,
|
|
60
64
|
]);
|
|
61
|
-
// Encode the data buffer using
|
|
62
|
-
const encodedId = SubDomain.
|
|
65
|
+
// Encode the data buffer using Base36
|
|
66
|
+
const encodedId = SubDomain.base36.encode(dataBuffer);
|
|
63
67
|
// Ensure DNS label length does not exceed 63 characters
|
|
64
68
|
if (encodedId.length > 63) {
|
|
65
69
|
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
@@ -69,7 +73,7 @@ class SubDomain {
|
|
|
69
73
|
/**
|
|
70
74
|
* Decodes the provided identifier back into the original chain and storeId.
|
|
71
75
|
*
|
|
72
|
-
* @param encodedId - The
|
|
76
|
+
* @param encodedId - The Base36-encoded identifier.
|
|
73
77
|
* @returns An object containing the original chain and storeId.
|
|
74
78
|
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
75
79
|
*/
|
|
@@ -78,108 +82,30 @@ class SubDomain {
|
|
|
78
82
|
if (!encodedId || typeof encodedId !== "string") {
|
|
79
83
|
throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
|
|
80
84
|
}
|
|
81
|
-
// Decode the
|
|
82
|
-
const decodedBuffer = SubDomain.
|
|
85
|
+
// Decode the Base36 string back to a Buffer
|
|
86
|
+
const decodedBuffer = SubDomain.base36.decode(encodedId);
|
|
83
87
|
if (!decodedBuffer) {
|
|
84
|
-
throw new Error("Failed to decode
|
|
88
|
+
throw new Error("Failed to decode Base36 string.");
|
|
85
89
|
}
|
|
86
90
|
// Ensure there's at least 1 byte for chain_length and STORE_ID_LENGTH bytes for storeId
|
|
87
91
|
if (decodedBuffer.length < 1 + SubDomain.STORE_ID_LENGTH) {
|
|
88
92
|
throw new Error("Decoded data is too short to contain required fields.");
|
|
89
93
|
}
|
|
90
94
|
// Extract chain_length (1 byte)
|
|
91
|
-
const chain_length = decodedBuffer.readUInt8(0);
|
|
95
|
+
const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
|
|
92
96
|
// Extract chain
|
|
93
|
-
const chain = decodedBuffer.slice(1, 1 + chain_length).toString("utf8");
|
|
97
|
+
const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
|
|
94
98
|
// Extract storeId
|
|
95
99
|
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
|
|
96
100
|
// Convert storeId buffer to hex string
|
|
97
|
-
const storeId = storeIdBuffer.toString("hex");
|
|
101
|
+
const storeId = Buffer.from(storeIdBuffer).toString("hex");
|
|
98
102
|
return { chain, storeId };
|
|
99
103
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Encodes a Buffer into a Base62 string.
|
|
102
|
-
*
|
|
103
|
-
* @param buffer - The Buffer to encode.
|
|
104
|
-
* @returns The Base62-encoded string.
|
|
105
|
-
*/
|
|
106
|
-
static encodeBase62(buffer) {
|
|
107
|
-
if (buffer.length === 0)
|
|
108
|
-
return "";
|
|
109
|
-
// Convert Buffer to BigInt
|
|
110
|
-
let num = BigInt(0);
|
|
111
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
112
|
-
num = (num << BigInt(8)) + BigInt(buffer[i]);
|
|
113
|
-
}
|
|
114
|
-
// Base62 encoding
|
|
115
|
-
let encoded = "";
|
|
116
|
-
const base = BigInt(62);
|
|
117
|
-
if (num === BigInt(0)) {
|
|
118
|
-
encoded = SubDomain.BASE62_CHARSET[0];
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
while (num > 0) {
|
|
122
|
-
const remainder = num % base;
|
|
123
|
-
encoded = SubDomain.BASE62_CHARSET[Number(remainder)] + encoded;
|
|
124
|
-
num = num / base;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
// Handle leading zero bytes
|
|
128
|
-
let leadingZeros = 0;
|
|
129
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
130
|
-
if (buffer[i] === 0) {
|
|
131
|
-
leadingZeros++;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (let i = 0; i < leadingZeros; i++) {
|
|
138
|
-
encoded = SubDomain.BASE62_CHARSET[0] + encoded;
|
|
139
|
-
}
|
|
140
|
-
return encoded;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Decodes a Base62 string into a Buffer.
|
|
144
|
-
*
|
|
145
|
-
* @param str - The Base62 string to decode.
|
|
146
|
-
* @returns The decoded Buffer.
|
|
147
|
-
*/
|
|
148
|
-
static decodeBase62(str) {
|
|
149
|
-
if (str.length === 0)
|
|
150
|
-
return Buffer.alloc(0);
|
|
151
|
-
// Handle leading '0's
|
|
152
|
-
let leadingZeros = 0;
|
|
153
|
-
while (leadingZeros < str.length && str[leadingZeros] === SubDomain.BASE62_CHARSET[0]) {
|
|
154
|
-
leadingZeros++;
|
|
155
|
-
}
|
|
156
|
-
// Decode the Base62 string to BigInt
|
|
157
|
-
let num = BigInt(0);
|
|
158
|
-
const base = BigInt(62);
|
|
159
|
-
for (let i = leadingZeros; i < str.length; i++) {
|
|
160
|
-
const char = str[i];
|
|
161
|
-
const value = SubDomain.BASE62_CHARSET.indexOf(char);
|
|
162
|
-
if (value === -1) {
|
|
163
|
-
throw new Error(`Invalid character in Base62 string: ${char}`);
|
|
164
|
-
}
|
|
165
|
-
num = num * base + BigInt(value);
|
|
166
|
-
}
|
|
167
|
-
// Convert BigInt to Buffer
|
|
168
|
-
let hex = num.toString(16);
|
|
169
|
-
if (hex.length % 2 !== 0) {
|
|
170
|
-
hex = "0" + hex;
|
|
171
|
-
}
|
|
172
|
-
let decoded = Buffer.from(hex, "hex");
|
|
173
|
-
// Prepend leading zero bytes
|
|
174
|
-
if (leadingZeros > 0) {
|
|
175
|
-
const zeroBuffer = Buffer.alloc(leadingZeros, 0);
|
|
176
|
-
decoded = Buffer.concat([zeroBuffer, decoded]);
|
|
177
|
-
}
|
|
178
|
-
return decoded;
|
|
179
|
-
}
|
|
180
104
|
}
|
|
181
105
|
exports.SubDomain = SubDomain;
|
|
182
|
-
// Define the
|
|
183
|
-
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);
|
|
184
110
|
// Define expected byte length for storeId
|
|
185
111
|
SubDomain.STORE_ID_LENGTH = 32; // bytes
|