@dignetwork/dig-sdk 0.0.1-alpha.195 → 0.0.1-alpha.196
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.
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SubDomain Class
|
|
3
|
+
*
|
|
4
|
+
* Encapsulates the logic for encoding and decoding a combination of
|
|
5
|
+
* chain and storeId into a DNS-friendly identifier using Base62 encoding and HMAC.
|
|
6
|
+
*/
|
|
7
|
+
declare class SubDomain {
|
|
8
|
+
private static readonly BASE62_CHARSET;
|
|
9
|
+
private static base62;
|
|
10
|
+
private static readonly DEFAULT_STORE_ID_LENGTH;
|
|
11
|
+
private static readonly HMAC_LENGTH;
|
|
12
|
+
private static readonly COMPRESSION_KEY;
|
|
13
|
+
readonly chain: string;
|
|
14
|
+
readonly storeId: string;
|
|
15
|
+
readonly encodedId: string;
|
|
16
|
+
/**
|
|
17
|
+
* Constructor for SubDomain
|
|
18
|
+
*
|
|
19
|
+
* @param chain - The chain name (e.g., "CHAIN1234").
|
|
20
|
+
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
21
|
+
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
22
|
+
*/
|
|
23
|
+
constructor(chain: string, storeId: string);
|
|
24
|
+
/**
|
|
25
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier with HMAC.
|
|
26
|
+
*
|
|
27
|
+
* @returns The Base62-encoded identifier with appended HMAC.
|
|
28
|
+
* @throws Will throw an error if encoding fails.
|
|
29
|
+
*/
|
|
30
|
+
private encode;
|
|
31
|
+
/**
|
|
32
|
+
* Decodes the provided identifier back into the original chain and storeId after verifying HMAC.
|
|
33
|
+
*
|
|
34
|
+
* @param encodedId - The Base62-encoded identifier with appended HMAC.
|
|
35
|
+
* @returns An object containing the original chain and storeId.
|
|
36
|
+
* @throws Will throw an error if decoding fails, HMAC verification fails, or data lengths mismatch.
|
|
37
|
+
*/
|
|
38
|
+
static decode(encodedId: string): {
|
|
39
|
+
chain: string;
|
|
40
|
+
storeId: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export { SubDomain };
|
|
44
|
+
//# sourceMappingURL=Subdomain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Subdomain.d.ts","sourceRoot":"","sources":["../../src/utils/Subdomain.ts"],"names":[],"mappings":"AAKA;;;;;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,uBAAuB,CAAM;IACrD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAM;IAGzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAC6B;IAGpE,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;IAoEd;;;;;;OAMG;WACW,MAAM,CAAC,SAAS,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;CAuE5E;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SubDomain.ts
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.SubDomain = void 0;
|
|
8
|
+
const base_x_1 = __importDefault(require("base-x"));
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
/**
|
|
11
|
+
* SubDomain Class
|
|
12
|
+
*
|
|
13
|
+
* Encapsulates the logic for encoding and decoding a combination of
|
|
14
|
+
* chain and storeId into a DNS-friendly identifier using Base62 encoding and HMAC.
|
|
15
|
+
*/
|
|
16
|
+
class SubDomain {
|
|
17
|
+
/**
|
|
18
|
+
* Constructor for SubDomain
|
|
19
|
+
*
|
|
20
|
+
* @param chain - The chain name (e.g., "CHAIN1234").
|
|
21
|
+
* @param storeId - The store ID as a 64-character hexadecimal string.
|
|
22
|
+
* @throws Will throw an error if inputs are invalid or encoding exceeds DNS limits.
|
|
23
|
+
*/
|
|
24
|
+
constructor(chain, storeId) {
|
|
25
|
+
this.chain = chain;
|
|
26
|
+
this.storeId = storeId;
|
|
27
|
+
this.encodedId = this.encode();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Encodes the provided chain and storeId into a DNS-friendly identifier with HMAC.
|
|
31
|
+
*
|
|
32
|
+
* @returns The Base62-encoded identifier with appended HMAC.
|
|
33
|
+
* @throws Will throw an error if encoding fails.
|
|
34
|
+
*/
|
|
35
|
+
encode() {
|
|
36
|
+
// Validate inputs
|
|
37
|
+
if (!this.chain || typeof this.chain !== "string") {
|
|
38
|
+
throw new Error("Invalid chain: Chain must be a non-empty string.");
|
|
39
|
+
}
|
|
40
|
+
if (!this.storeId ||
|
|
41
|
+
typeof this.storeId !== "string" ||
|
|
42
|
+
!/^[0-9a-fA-F]{64}$/.test(this.storeId)) {
|
|
43
|
+
throw new Error("Invalid storeId: StoreId must be a 64-character hexadecimal string.");
|
|
44
|
+
}
|
|
45
|
+
// Ensure the chain length is within 1-255 characters to fit in one byte
|
|
46
|
+
const chainLength = this.chain.length;
|
|
47
|
+
if (chainLength < 1 || chainLength > 255) {
|
|
48
|
+
throw new Error("Invalid chain: Length must be between 1 and 255 characters.");
|
|
49
|
+
}
|
|
50
|
+
// Convert chain length to a single byte Buffer
|
|
51
|
+
const chainLengthBuffer = Buffer.from([chainLength]);
|
|
52
|
+
// Convert chain to a Buffer (UTF-8)
|
|
53
|
+
const chainBuffer = Buffer.from(this.chain, "utf8");
|
|
54
|
+
// Convert storeId from hex string to Buffer
|
|
55
|
+
const storeIdBuffer = Buffer.from(this.storeId, "hex");
|
|
56
|
+
// Validate storeId byte length
|
|
57
|
+
if (storeIdBuffer.length !== SubDomain.DEFAULT_STORE_ID_LENGTH) {
|
|
58
|
+
throw new Error(`Invalid storeId length: Expected ${SubDomain.DEFAULT_STORE_ID_LENGTH} bytes, got ${storeIdBuffer.length} bytes.`);
|
|
59
|
+
}
|
|
60
|
+
// Concatenate chain_length, chain, and storeId buffers
|
|
61
|
+
const dataBuffer = Buffer.concat([
|
|
62
|
+
chainLengthBuffer,
|
|
63
|
+
chainBuffer,
|
|
64
|
+
storeIdBuffer,
|
|
65
|
+
]);
|
|
66
|
+
// Create HMAC using SHA256
|
|
67
|
+
const hmac = crypto_1.default.createHmac("sha256", SubDomain.COMPRESSION_KEY);
|
|
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);
|
|
74
|
+
// Ensure DNS label length does not exceed 63 characters
|
|
75
|
+
if (encodedId.length > 63) {
|
|
76
|
+
throw new Error(`Encoded identifier length (${encodedId.length}) exceeds DNS label limit of 63 characters.`);
|
|
77
|
+
}
|
|
78
|
+
return encodedId;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Decodes the provided identifier back into the original chain and storeId after verifying HMAC.
|
|
82
|
+
*
|
|
83
|
+
* @param encodedId - The Base62-encoded identifier with appended HMAC.
|
|
84
|
+
* @returns An object containing the original chain and storeId.
|
|
85
|
+
* @throws Will throw an error if decoding fails, HMAC verification fails, or data lengths mismatch.
|
|
86
|
+
*/
|
|
87
|
+
static decode(encodedId) {
|
|
88
|
+
// Validate input
|
|
89
|
+
if (!encodedId || typeof encodedId !== "string") {
|
|
90
|
+
throw new Error("Invalid encodedId: encodedId must be a non-empty string.");
|
|
91
|
+
}
|
|
92
|
+
// Decode the Base62 string back to a Buffer
|
|
93
|
+
const decodedBuffer = SubDomain.base62.decode(encodedId);
|
|
94
|
+
if (!decodedBuffer) {
|
|
95
|
+
throw new Error("Failed to decode Base62 string.");
|
|
96
|
+
}
|
|
97
|
+
// Ensure there's at least 1 byte for chain_length and 64 bytes for storeId and HMAC
|
|
98
|
+
if (decodedBuffer.length <
|
|
99
|
+
1 + SubDomain.DEFAULT_STORE_ID_LENGTH + SubDomain.HMAC_LENGTH) {
|
|
100
|
+
throw new Error("Decoded data is too short to contain required fields.");
|
|
101
|
+
}
|
|
102
|
+
// Extract chain_length (1 byte)
|
|
103
|
+
const chain_length = Buffer.from(decodedBuffer).readUInt8(0);
|
|
104
|
+
// Define the expected total length
|
|
105
|
+
const expected_length = 1 +
|
|
106
|
+
chain_length +
|
|
107
|
+
SubDomain.DEFAULT_STORE_ID_LENGTH +
|
|
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
|
+
}
|
|
125
|
+
// Convert storeId buffer to hex string
|
|
126
|
+
const storeId = Buffer.from(storeIdBuffer).toString("hex");
|
|
127
|
+
return { chain, storeId };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.SubDomain = SubDomain;
|
|
131
|
+
// Define the Base62 character set
|
|
132
|
+
SubDomain.BASE62_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
133
|
+
// Initialize the Base62 encoder/decoder
|
|
134
|
+
SubDomain.base62 = (0, base_x_1.default)(SubDomain.BASE62_CHARSET);
|
|
135
|
+
// Define expected byte length for storeId
|
|
136
|
+
SubDomain.DEFAULT_STORE_ID_LENGTH = 32; // bytes
|
|
137
|
+
SubDomain.HMAC_LENGTH = 32; // bytes for HMAC-SHA256
|
|
138
|
+
// Hardcoded compression key
|
|
139
|
+
SubDomain.COMPRESSION_KEY = "7a4e8d2f6b1c9a3f5d8e2c4b7a1f9d3e6b8c5a2f4d7e9b1c8a3f5d2e6b9c4a7";
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,OAAO,CAAC;AACtB,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC"}
|
package/dist/utils/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dignetwork/dig-sdk",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.196",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"archiver": "^7.0.1",
|
|
31
31
|
"async-mutex": "^0.5.0",
|
|
32
32
|
"axios": "^1.7.7",
|
|
33
|
+
"base-x": "^5.0.0",
|
|
33
34
|
"bip39": "^3.1.0",
|
|
34
35
|
"bottleneck": "^2.19.5",
|
|
35
36
|
"chia-bls": "^1.0.2",
|