@dignetwork/dig-sdk 0.0.1-alpha.197 → 0.0.1-alpha.198
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.
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
declare class SubDomain {
|
|
8
8
|
private static readonly BASE62_CHARSET;
|
|
9
|
-
private static base62;
|
|
10
9
|
private static readonly STORE_ID_LENGTH;
|
|
11
10
|
readonly chain: string;
|
|
12
11
|
readonly storeId: string;
|
|
@@ -37,6 +36,20 @@ declare class SubDomain {
|
|
|
37
36
|
chain: string;
|
|
38
37
|
storeId: string;
|
|
39
38
|
};
|
|
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;
|
|
40
53
|
}
|
|
41
54
|
export { SubDomain };
|
|
42
55
|
//# 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":"AAEA;;;;;GAKG;AACH,cAAM,SAAS;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAC6B;IAGnE,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;IAsC3E;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;IAuC3B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;CAoC5B;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
package/dist/utils/Subdomain.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
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
|
-
};
|
|
6
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
4
|
exports.SubDomain = void 0;
|
|
8
|
-
const base_x_1 = __importDefault(require("base-x"));
|
|
9
5
|
/**
|
|
10
6
|
* SubDomain Class
|
|
11
7
|
*
|
|
@@ -46,7 +42,7 @@ class SubDomain {
|
|
|
46
42
|
if (chainLength < 1 || chainLength > 255) {
|
|
47
43
|
throw new Error("Invalid chain: Length must be between 1 and 255 characters.");
|
|
48
44
|
}
|
|
49
|
-
// Convert chain length to a single byte
|
|
45
|
+
// Convert chain length to a single byte
|
|
50
46
|
const chainLengthBuffer = Buffer.from([chainLength]);
|
|
51
47
|
// Convert chain to a Buffer (UTF-8)
|
|
52
48
|
const chainBuffer = Buffer.from(this.chain, "utf8");
|
|
@@ -63,7 +59,7 @@ class SubDomain {
|
|
|
63
59
|
storeIdBuffer,
|
|
64
60
|
]);
|
|
65
61
|
// Encode the data buffer using Base62
|
|
66
|
-
const encodedId = SubDomain.
|
|
62
|
+
const encodedId = SubDomain.encodeBase62(dataBuffer);
|
|
67
63
|
// Ensure DNS label length does not exceed 63 characters
|
|
68
64
|
if (encodedId.length > 63) {
|
|
69
65
|
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
@@ -83,7 +79,7 @@ class SubDomain {
|
|
|
83
79
|
throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
|
|
84
80
|
}
|
|
85
81
|
// Decode the Base62 string back to a Buffer
|
|
86
|
-
const decodedBuffer = SubDomain.
|
|
82
|
+
const decodedBuffer = SubDomain.decodeBase62(encodedId);
|
|
87
83
|
if (!decodedBuffer) {
|
|
88
84
|
throw new Error("Failed to decode Base62 string.");
|
|
89
85
|
}
|
|
@@ -92,24 +88,98 @@ class SubDomain {
|
|
|
92
88
|
throw new Error("Decoded data is too short to contain required fields.");
|
|
93
89
|
}
|
|
94
90
|
// Extract chain_length (1 byte)
|
|
95
|
-
const chain_length =
|
|
96
|
-
//
|
|
97
|
-
const
|
|
98
|
-
|
|
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");
|
|
91
|
+
const chain_length = decodedBuffer.readUInt8(0);
|
|
92
|
+
// Extract chain
|
|
93
|
+
const chain = decodedBuffer.slice(1, 1 + chain_length).toString("utf8");
|
|
94
|
+
// Extract storeId
|
|
103
95
|
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
|
|
104
96
|
// Convert storeId buffer to hex string
|
|
105
|
-
const storeId =
|
|
97
|
+
const storeId = storeIdBuffer.toString("hex");
|
|
106
98
|
return { chain, storeId };
|
|
107
99
|
}
|
|
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
|
+
}
|
|
108
180
|
}
|
|
109
181
|
exports.SubDomain = SubDomain;
|
|
110
182
|
// Define the Base62 character set
|
|
111
183
|
SubDomain.BASE62_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
112
|
-
// Initialize the Base62 encoder/decoder
|
|
113
|
-
SubDomain.base62 = (0, base_x_1.default)(SubDomain.BASE62_CHARSET);
|
|
114
184
|
// Define expected byte length for storeId
|
|
115
185
|
SubDomain.STORE_ID_LENGTH = 32; // bytes
|