Skip to content

Commit 83e9951

Browse files
Merge pull request #186 from DIG-Network/release/v0.0.1-alpha.197
Release/v0.0.1 alpha.197
2 parents ea382c0 + e8b28b2 commit 83e9951

File tree

4 files changed

+27
-61
lines changed

4 files changed

+27
-61
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
### [0.0.1-alpha.197](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.196...v0.0.1-alpha.197) (2024-11-17)
6+
7+
8+
### Features
9+
10+
* add subdomain encoder ([e7be359](https://github.com/DIG-Network/dig-chia-sdk/commit/e7be3598c228251ad5a79cc1f478445414a669f7))
11+
512
### [0.0.1-alpha.196](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.195...v0.0.1-alpha.196) (2024-11-17)
613

714

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dignetwork/dig-sdk",
3-
"version": "0.0.1-alpha.196",
3+
"version": "0.0.1-alpha.197",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/utils/Subdomain.ts

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// SubDomain.ts
22

33
import baseX from "base-x";
4-
import crypto from "crypto";
54

65
/**
76
* SubDomain Class
87
*
98
* Encapsulates the logic for encoding and decoding a combination of
10-
* chain and storeId into a DNS-friendly identifier using Base62 encoding and HMAC.
9+
* chain and storeId into a DNS-friendly identifier using Base62 encoding.
1110
*/
1211
class SubDomain {
1312
// Define the Base62 character set
@@ -18,12 +17,7 @@ class SubDomain {
1817
private static base62 = baseX(SubDomain.BASE62_CHARSET);
1918

2019
// Define expected byte length for storeId
21-
private static readonly DEFAULT_STORE_ID_LENGTH = 32; // bytes
22-
private static readonly HMAC_LENGTH = 32; // bytes for HMAC-SHA256
23-
24-
// Hardcoded compression key
25-
private static readonly COMPRESSION_KEY =
26-
"7a4e8d2f6b1c9a3f5d8e2c4b7a1f9d3e6b8c5a2f4d7e9b1c8a3f5d2e6b9c4a7";
20+
private static readonly STORE_ID_LENGTH = 32; // bytes
2721

2822
// Properties
2923
public readonly chain: string;
@@ -44,9 +38,9 @@ class SubDomain {
4438
}
4539

4640
/**
47-
* Encodes the provided chain and storeId into a DNS-friendly identifier with HMAC.
41+
* Encodes the provided chain and storeId into a DNS-friendly identifier.
4842
*
49-
* @returns The Base62-encoded identifier with appended HMAC.
43+
* @returns The Base62-encoded identifier.
5044
* @throws Will throw an error if encoding fails.
5145
*/
5246
private encode(): string {
@@ -83,9 +77,9 @@ class SubDomain {
8377
const storeIdBuffer = Buffer.from(this.storeId, "hex");
8478

8579
// Validate storeId byte length
86-
if (storeIdBuffer.length !== SubDomain.DEFAULT_STORE_ID_LENGTH) {
80+
if (storeIdBuffer.length !== SubDomain.STORE_ID_LENGTH) {
8781
throw new Error(
88-
`Invalid storeId length: Expected ${SubDomain.DEFAULT_STORE_ID_LENGTH} bytes, got ${storeIdBuffer.length} bytes.`
82+
`Invalid storeId length: Expected ${SubDomain.STORE_ID_LENGTH} bytes, got ${storeIdBuffer.length} bytes.`
8983
);
9084
}
9185

@@ -96,16 +90,8 @@ class SubDomain {
9690
storeIdBuffer,
9791
]);
9892

99-
// Create HMAC using SHA256
100-
const hmac = crypto.createHmac("sha256", SubDomain.COMPRESSION_KEY);
101-
hmac.update(dataBuffer);
102-
const hmacDigest = hmac.digest(); // 32 bytes
103-
104-
// Concatenate dataBuffer and hmacDigest
105-
const finalBuffer = Buffer.concat([dataBuffer, hmacDigest]);
106-
107-
// Encode the final buffer using Base62
108-
const encodedId = SubDomain.base62.encode(finalBuffer);
93+
// Encode the data buffer using Base62
94+
const encodedId = SubDomain.base62.encode(dataBuffer);
10995

11096
// Ensure DNS label length does not exceed 63 characters
11197
if (encodedId.length > 63) {
@@ -118,11 +104,11 @@ class SubDomain {
118104
}
119105

120106
/**
121-
* Decodes the provided identifier back into the original chain and storeId after verifying HMAC.
107+
* Decodes the provided identifier back into the original chain and storeId.
122108
*
123-
* @param encodedId - The Base62-encoded identifier with appended HMAC.
109+
* @param encodedId - The Base62-encoded identifier.
124110
* @returns An object containing the original chain and storeId.
125-
* @throws Will throw an error if decoding fails, HMAC verification fails, or data lengths mismatch.
111+
* @throws Will throw an error if decoding fails or data lengths mismatch.
126112
*/
127113
public static decode(encodedId: string): { chain: string; storeId: string } {
128114
// Validate input
@@ -139,56 +125,29 @@ class SubDomain {
139125
throw new Error("Failed to decode Base62 string.");
140126
}
141127

142-
// Ensure there's at least 1 byte for chain_length and 64 bytes for storeId and HMAC
143-
if (
144-
decodedBuffer.length <
145-
1 + SubDomain.DEFAULT_STORE_ID_LENGTH + SubDomain.HMAC_LENGTH
146-
) {
128+
// Ensure there's at least 1 byte for chain_length and STORE_ID_LENGTH bytes for storeId
129+
if (decodedBuffer.length < 1 + SubDomain.STORE_ID_LENGTH) {
147130
throw new Error("Decoded data is too short to contain required fields.");
148131
}
149132

150133
// Extract chain_length (1 byte)
151134
const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
152135

153136
// Define the expected total length
154-
const expected_length =
155-
1 +
156-
chain_length +
157-
SubDomain.DEFAULT_STORE_ID_LENGTH +
158-
SubDomain.HMAC_LENGTH;
137+
const expected_length = 1 + chain_length + SubDomain.STORE_ID_LENGTH;
159138

160139
if (decodedBuffer.length !== expected_length) {
161140
throw new Error(
162141
`Decoded data length mismatch: expected ${expected_length} bytes, got ${decodedBuffer.length} bytes.`
163142
);
164143
}
165144

166-
// Extract chain, storeId, and received HMAC from the buffer
167-
const chain = Buffer.from(
168-
decodedBuffer.slice(1, 1 + chain_length)
169-
).toString("utf8");
145+
// Extract chain and storeId from the buffer
146+
const chain = Buffer.from(decodedBuffer.slice(1, 1 + chain_length)).toString("utf8");
170147
const storeIdBuffer = decodedBuffer.slice(
171148
1 + chain_length,
172-
1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH
149+
1 + chain_length + SubDomain.STORE_ID_LENGTH
173150
);
174-
const receivedHmac = decodedBuffer.slice(
175-
1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH,
176-
expected_length
177-
);
178-
179-
// Recompute HMAC over [chain_length][chain][storeId]
180-
const dataBuffer = decodedBuffer.slice(
181-
0,
182-
1 + chain_length + SubDomain.DEFAULT_STORE_ID_LENGTH
183-
);
184-
const hmac = crypto.createHmac("sha256", SubDomain.COMPRESSION_KEY);
185-
hmac.update(dataBuffer);
186-
const expectedHmac = hmac.digest(); // 32 bytes
187-
188-
// Compare HMACs securely
189-
if (!crypto.timingSafeEqual(receivedHmac, expectedHmac)) {
190-
throw new Error("HMAC verification failed: Invalid identifier.");
191-
}
192151

193152
// Convert storeId buffer to hex string
194153
const storeId = Buffer.from(storeIdBuffer).toString("hex");

0 commit comments

Comments
 (0)