@dignetwork/dig-sdk 0.0.1-alpha.196 → 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.
|
@@ -2,14 +2,12 @@
|
|
|
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
9
|
private static base62;
|
|
10
|
-
private static readonly
|
|
11
|
-
private static readonly HMAC_LENGTH;
|
|
12
|
-
private static readonly COMPRESSION_KEY;
|
|
10
|
+
private static readonly STORE_ID_LENGTH;
|
|
13
11
|
readonly chain: string;
|
|
14
12
|
readonly storeId: string;
|
|
15
13
|
readonly encodedId: string;
|
|
@@ -22,18 +20,18 @@ declare class SubDomain {
|
|
|
22
20
|
*/
|
|
23
21
|
constructor(chain: string, storeId: string);
|
|
24
22
|
/**
|
|
25
|
-
* Encodes the provided chain and storeId into a DNS-friendly identifier
|
|
23
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
26
24
|
*
|
|
27
|
-
* @returns The Base62-encoded identifier
|
|
25
|
+
* @returns The Base62-encoded identifier.
|
|
28
26
|
* @throws Will throw an error if encoding fails.
|
|
29
27
|
*/
|
|
30
28
|
private encode;
|
|
31
29
|
/**
|
|
32
|
-
* Decodes the provided identifier back into the original chain and storeId
|
|
30
|
+
* Decodes the provided identifier back into the original chain and storeId.
|
|
33
31
|
*
|
|
34
|
-
* @param encodedId - The Base62-encoded identifier
|
|
32
|
+
* @param encodedId - The Base62-encoded identifier.
|
|
35
33
|
* @returns An object containing the original chain and storeId.
|
|
36
|
-
* @throws Will throw an error if decoding fails
|
|
34
|
+
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
37
35
|
*/
|
|
38
36
|
static decode(encodedId: string): {
|
|
39
37
|
chain: string;
|
|
@@ -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,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"}
|
package/dist/utils/Subdomain.js
CHANGED
|
@@ -6,12 +6,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.SubDomain = void 0;
|
|
8
8
|
const base_x_1 = __importDefault(require("base-x"));
|
|
9
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
10
9
|
/**
|
|
11
10
|
* SubDomain Class
|
|
12
11
|
*
|
|
13
12
|
* Encapsulates the logic for encoding and decoding a combination of
|
|
14
|
-
* chain and storeId into a DNS-friendly identifier using Base62 encoding
|
|
13
|
+
* chain and storeId into a DNS-friendly identifier using Base62 encoding.
|
|
15
14
|
*/
|
|
16
15
|
class SubDomain {
|
|
17
16
|
/**
|
|
@@ -27,9 +26,9 @@ class SubDomain {
|
|
|
27
26
|
this.encodedId = this.encode();
|
|
28
27
|
}
|
|
29
28
|
/**
|
|
30
|
-
* Encodes the provided chain and storeId into a DNS-friendly identifier
|
|
29
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier.
|
|
31
30
|
*
|
|
32
|
-
* @returns The Base62-encoded identifier
|
|
31
|
+
* @returns The Base62-encoded identifier.
|
|
33
32
|
* @throws Will throw an error if encoding fails.
|
|
34
33
|
*/
|
|
35
34
|
encode() {
|
|
@@ -54,8 +53,8 @@ class SubDomain {
|
|
|
54
53
|
// Convert storeId from hex string to Buffer
|
|
55
54
|
const storeIdBuffer = Buffer.from(this.storeId, "hex");
|
|
56
55
|
// Validate storeId byte length
|
|
57
|
-
if (storeIdBuffer.length !== SubDomain.
|
|
58
|
-
throw new Error(`Invalid storeId length: Expected ${SubDomain.
|
|
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.`);
|
|
59
58
|
}
|
|
60
59
|
// Concatenate chain_length, chain, and storeId buffers
|
|
61
60
|
const dataBuffer = Buffer.concat([
|
|
@@ -63,14 +62,8 @@ class SubDomain {
|
|
|
63
62
|
chainBuffer,
|
|
64
63
|
storeIdBuffer,
|
|
65
64
|
]);
|
|
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);
|
|
65
|
+
// Encode the data buffer using Base62
|
|
66
|
+
const encodedId = SubDomain.base62.encode(dataBuffer);
|
|
74
67
|
// Ensure DNS label length does not exceed 63 characters
|
|
75
68
|
if (encodedId.length > 63) {
|
|
76
69
|
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
@@ -78,11 +71,11 @@ class SubDomain {
|
|
|
78
71
|
return encodedId;
|
|
79
72
|
}
|
|
80
73
|
/**
|
|
81
|
-
* Decodes the provided identifier back into the original chain and storeId
|
|
74
|
+
* Decodes the provided identifier back into the original chain and storeId.
|
|
82
75
|
*
|
|
83
|
-
* @param encodedId - The Base62-encoded identifier
|
|
76
|
+
* @param encodedId - The Base62-encoded identifier.
|
|
84
77
|
* @returns An object containing the original chain and storeId.
|
|
85
|
-
* @throws Will throw an error if decoding fails
|
|
78
|
+
* @throws Will throw an error if decoding fails or data lengths mismatch.
|
|
86
79
|
*/
|
|
87
80
|
static decode(encodedId) {
|
|
88
81
|
// Validate input
|
|
@@ -94,34 +87,20 @@ class SubDomain {
|
|
|
94
87
|
if (!decodedBuffer) {
|
|
95
88
|
throw new Error("Failed to decode Base62 string.");
|
|
96
89
|
}
|
|
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) {
|
|
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) {
|
|
100
92
|
throw new Error("Decoded data is too short to contain required fields.");
|
|
101
93
|
}
|
|
102
94
|
// Extract chain_length (1 byte)
|
|
103
95
|
const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
|
|
104
96
|
// Define the expected total length
|
|
105
|
-
const expected_length = 1 +
|
|
106
|
-
chain_length +
|
|
107
|
-
SubDomain.DEFAULT_STORE_ID_LENGTH +
|
|
108
|
-
SubDomain.HMAC_LENGTH;
|
|
97
|
+
const expected_length = 1 + chain_length + SubDomain.STORE_ID_LENGTH;
|
|
109
98
|
if (decodedBuffer.length !== expected_length) {
|
|
110
99
|
throw new Error(`Decoded data length mismatch: expected ${expected_length} bytes, got ${decodedBuffer.length} bytes.`);
|
|
111
100
|
}
|
|
112
|
-
// Extract chain
|
|
101
|
+
// Extract chain and storeId from the buffer
|
|
113
102
|
const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
|
|
114
|
-
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.
|
|
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
|
-
}
|
|
103
|
+
const storeIdBuffer = decodedBuffer.slice(1 + chain_length, 1 + chain_length + SubDomain.STORE_ID_LENGTH);
|
|
125
104
|
// Convert storeId buffer to hex string
|
|
126
105
|
const storeId = Buffer.from(storeIdBuffer).toString("hex");
|
|
127
106
|
return { chain, storeId };
|
|
@@ -133,7 +112,4 @@ SubDomain.BASE62_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop
|
|
|
133
112
|
// Initialize the Base62 encoder/decoder
|
|
134
113
|
SubDomain.base62 = (0, base_x_1.default)(SubDomain.BASE62_CHARSET);
|
|
135
114
|
// 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";
|
|
115
|
+
SubDomain.STORE_ID_LENGTH = 32; // bytes
|