@dignetwork/dig-sdk 0.0.1-alpha.196 → 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.
- package/dist/utils/Subdomain.d.ts +21 -10
- package/dist/utils/Subdomain.d.ts.map +1 -1
- package/dist/utils/Subdomain.js +101 -55
- package/package.json +1 -1
|
@@ -2,14 +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 Base62 encoding
|
|
5
|
+
* chain and storeId into a DNS-friendly identifier using Base62 encoding.
|
|
6
6
|
*/
|
|
7
7
|
declare class SubDomain {
|
|
8
8
|
private static readonly BASE62_CHARSET;
|
|
9
|
-
private static
|
|
10
|
-
private static readonly DEFAULT_STORE_ID_LENGTH;
|
|
11
|
-
private static readonly HMAC_LENGTH;
|
|
12
|
-
private static readonly COMPRESSION_KEY;
|
|
9
|
+
private static readonly STORE_ID_LENGTH;
|
|
13
10
|
readonly chain: string;
|
|
14
11
|
readonly storeId: string;
|
|
15
12
|
readonly encodedId: string;
|
|
@@ -22,23 +19,37 @@ declare class SubDomain {
|
|
|
22
19
|
*/
|
|
23
20
|
constructor(chain: string, storeId: string);
|
|
24
21
|
/**
|
|
25
|
-
* Encodes the provided chain and storeId into a DNS-friendly identifier
|
|
22
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
26
23
|
*
|
|
27
|
-
* @returns The Base62-encoded identifier
|
|
24
|
+
* @returns The Base62-encoded identifier.
|
|
28
25
|
* @throws Will throw an error if encoding fails.
|
|
29
26
|
*/
|
|
30
27
|
private encode;
|
|
31
28
|
/**
|
|
32
|
-
* Decodes the provided identifier back into the original chain and storeId
|
|
29
|
+
* Decodes the provided identifier back into the original chain and storeId.
|
|
33
30
|
*
|
|
34
|
-
* @param encodedId - The Base62-encoded identifier
|
|
31
|
+
* @param encodedId - The Base62-encoded identifier.
|
|
35
32
|
* @returns An object containing the original chain and storeId.
|
|
36
|
-
* @throws Will throw an error if decoding fails
|
|
33
|
+
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
37
34
|
*/
|
|
38
35
|
static decode(encodedId: string): {
|
|
39
36
|
chain: string;
|
|
40
37
|
storeId: string;
|
|
41
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;
|
|
42
53
|
}
|
|
43
54
|
export { SubDomain };
|
|
44
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,17 +1,12 @@
|
|
|
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
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
10
5
|
/**
|
|
11
6
|
* SubDomain Class
|
|
12
7
|
*
|
|
13
8
|
* Encapsulates the logic for encoding and decoding a combination of
|
|
14
|
-
* chain and storeId into a DNS-friendly identifier using Base62 encoding
|
|
9
|
+
* chain and storeId into a DNS-friendly identifier using Base62 encoding.
|
|
15
10
|
*/
|
|
16
11
|
class SubDomain {
|
|
17
12
|
/**
|
|
@@ -27,9 +22,9 @@ class SubDomain {
|
|
|
27
22
|
this.encodedId = this.encode();
|
|
28
23
|
}
|
|
29
24
|
/**
|
|
30
|
-
* Encodes the provided chain and storeId into a DNS-friendly identifier
|
|
25
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
31
26
|
*
|
|
32
|
-
* @returns The Base62-encoded identifier
|
|
27
|
+
* @returns The Base62-encoded identifier.
|
|
33
28
|
* @throws Will throw an error if encoding fails.
|
|
34
29
|
*/
|
|
35
30
|
encode() {
|
|
@@ -47,15 +42,15 @@ class SubDomain {
|
|
|
47
42
|
if (chainLength < 1 || chainLength > 255) {
|
|
48
43
|
throw new Error("Invalid chain: Length must be between 1 and 255 characters.");
|
|
49
44
|
}
|
|
50
|
-
// Convert chain length to a single byte
|
|
45
|
+
// Convert chain length to a single byte
|
|
51
46
|
const chainLengthBuffer = Buffer.from([chainLength]);
|
|
52
47
|
// Convert chain to a Buffer (UTF-8)
|
|
53
48
|
const chainBuffer = Buffer.from(this.chain, "utf8");
|
|
54
49
|
// Convert storeId from hex string to Buffer
|
|
55
50
|
const storeIdBuffer = Buffer.from(this.storeId, "hex");
|
|
56
51
|
// Validate storeId byte length
|
|
57
|
-
if (storeIdBuffer.length !== SubDomain.
|
|
58
|
-
throw new Error(`Invalid storeId length: Expected ${SubDomain.
|
|
52
|
+
if (storeIdBuffer.length !== SubDomain.STORE_ID_LENGTH) {
|
|
53
|
+
throw new Error(`Invalid storeId length: Expected ${SubDomain.STORE_ID_LENGTH} bytes, got ${storeIdBuffer.length} bytes.`);
|
|
59
54
|
}
|
|
60
55
|
// Concatenate chain_length, chain, and storeId buffers
|
|
61
56
|
const dataBuffer = Buffer.concat([
|
|
@@ -63,14 +58,8 @@ class SubDomain {
|
|
|
63
58
|
chainBuffer,
|
|
64
59
|
storeIdBuffer,
|
|
65
60
|
]);
|
|
66
|
-
//
|
|
67
|
-
const
|
|
68
|
-
hmac.update(dataBuffer);
|
|
69
|
-
const hmacDigest = hmac.digest(); // 32 bytes
|
|
70
|
-
// Concatenate dataBuffer and hmacDigest
|
|
71
|
-
const finalBuffer = Buffer.concat([dataBuffer, hmacDigest]);
|
|
72
|
-
// Encode the final buffer using Base62
|
|
73
|
-
const encodedId = SubDomain.base62.encode(finalBuffer);
|
|
61
|
+
// Encode the data buffer using Base62
|
|
62
|
+
const encodedId = SubDomain.encodeBase62(dataBuffer);
|
|
74
63
|
// Ensure DNS label length does not exceed 63 characters
|
|
75
64
|
if (encodedId.length > 63) {
|
|
76
65
|
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
@@ -78,11 +67,11 @@ class SubDomain {
|
|
|
78
67
|
return encodedId;
|
|
79
68
|
}
|
|
80
69
|
/**
|
|
81
|
-
* Decodes the provided identifier back into the original chain and storeId
|
|
70
|
+
* Decodes the provided identifier back into the original chain and storeId.
|
|
82
71
|
*
|
|
83
|
-
* @param encodedId - The Base62-encoded identifier
|
|
72
|
+
* @param encodedId - The Base62-encoded identifier.
|
|
84
73
|
* @returns An object containing the original chain and storeId.
|
|
85
|
-
* @throws Will throw an error if decoding fails
|
|
74
|
+
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
86
75
|
*/
|
|
87
76
|
static decode(encodedId) {
|
|
88
77
|
// Validate input
|
|
@@ -90,50 +79,107 @@ class SubDomain {
|
|
|
90
79
|
throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
|
|
91
80
|
}
|
|
92
81
|
// Decode the Base62 string back to a Buffer
|
|
93
|
-
const decodedBuffer = SubDomain.
|
|
82
|
+
const decodedBuffer = SubDomain.decodeBase62(encodedId);
|
|
94
83
|
if (!decodedBuffer) {
|
|
95
84
|
throw new Error("Failed to decode Base62 string.");
|
|
96
85
|
}
|
|
97
|
-
// Ensure there's at least 1 byte for chain_length and
|
|
98
|
-
if (decodedBuffer.length <
|
|
99
|
-
1 + SubDomain.DEFAULT_STORE_ID_LENGTH + SubDomain.HMAC_LENGTH) {
|
|
86
|
+
// Ensure there's at least 1 byte for chain_length and STORE_ID_LENGTH bytes for storeId
|
|
87
|
+
if (decodedBuffer.length < 1 + SubDomain.STORE_ID_LENGTH) {
|
|
100
88
|
throw new Error("Decoded data is too short to contain required fields.");
|
|
101
89
|
}
|
|
102
90
|
// Extract chain_length (1 byte)
|
|
103
|
-
const chain_length =
|
|
104
|
-
//
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
SubDomain.HMAC_LENGTH;
|
|
109
|
-
if (decodedBuffer.length !== expected_length) {
|
|
110
|
-
throw new Error(`Decoded data length mismatch: expected ${expected_length} bytes, got ${decodedBuffer.length} bytes.`);
|
|
111
|
-
}
|
|
112
|
-
// Extract chain, storeId, and received HMAC from the buffer
|
|
113
|
-
const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
|
|
114
|
-
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH);
|
|
115
|
-
const receivedHmac = decodedBuffer.slice(1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH, expected_length);
|
|
116
|
-
// Recompute HMAC over [chain_length][chain][storeId]
|
|
117
|
-
const dataBuffer = decodedBuffer.slice(0, 1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH);
|
|
118
|
-
const hmac = crypto_1.default.createHmac("sha256", SubDomain.COMPRESSION_KEY);
|
|
119
|
-
hmac.update(dataBuffer);
|
|
120
|
-
const expectedHmac = hmac.digest(); // 32 bytes
|
|
121
|
-
// Compare HMACs securely
|
|
122
|
-
if (!crypto_1.default.timingSafeEqual(receivedHmac, expectedHmac)) {
|
|
123
|
-
throw new Error("HMAC verification failed: Invalid identifier.");
|
|
124
|
-
}
|
|
91
|
+
const chain_length = decodedBuffer.readUInt8(0);
|
|
92
|
+
// Extract chain
|
|
93
|
+
const chain = decodedBuffer.slice(1, 1 + chain_length).toString("utf8");
|
|
94
|
+
// Extract storeId
|
|
95
|
+
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
|
|
125
96
|
// Convert storeId buffer to hex string
|
|
126
|
-
const storeId =
|
|
97
|
+
const storeId = storeIdBuffer.toString("hex");
|
|
127
98
|
return { chain, storeId };
|
|
128
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
|
+
}
|
|
129
180
|
}
|
|
130
181
|
exports.SubDomain = SubDomain;
|
|
131
182
|
// Define the Base62 character set
|
|
132
183
|
SubDomain.BASE62_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
133
|
-
// Initialize the Base62 encoder/decoder
|
|
134
|
-
SubDomain.base62 = (0, base_x_1.default)(SubDomain.BASE62_CHARSET);
|
|
135
184
|
// Define expected byte length for storeId
|
|
136
|
-
SubDomain.
|
|
137
|
-
SubDomain.HMAC_LENGTH = 32; // bytes for HMAC-SHA256
|
|
138
|
-
// Hardcoded compression key
|
|
139
|
-
SubDomain.COMPRESSION_KEY = "7a4e8d2f6b1c9a3f5d8e2c4b7a1f9d3e6b8c5a2f4d7e9b1c8a3f5d2e6b9c4a7";
|
|
185
|
+
SubDomain.STORE_ID_LENGTH = 32; // bytes
|