@dignetwork/dig-sdk 0.0.1-alpha.186 → 0.0.1-alpha.187
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/Udi.d.ts +6 -5
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +50 -62
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
declare class Udi {
|
|
2
2
|
readonly chainName: string;
|
|
3
|
-
private readonly
|
|
4
|
-
private readonly
|
|
3
|
+
private readonly _storeIdHex;
|
|
4
|
+
private readonly _rootHashHex;
|
|
5
5
|
readonly resourceKey: string | null;
|
|
6
6
|
static readonly nid: string;
|
|
7
7
|
static readonly namespace: string;
|
|
8
|
-
constructor(chainName: string, storeId: string
|
|
9
|
-
static
|
|
8
|
+
constructor(chainName: string, storeId: string, rootHash?: string | null, resourceKey?: string | null);
|
|
9
|
+
static verifyAndFormatHex(input: string): string;
|
|
10
10
|
static fromUrn(urn: string): Udi;
|
|
11
|
+
static convertToHex(input: string): string;
|
|
11
12
|
static addBase32Padding(input: string): string;
|
|
12
13
|
static toStandardBase64(base64UrlSafe: string): string;
|
|
13
14
|
static addBase64Padding(base64: string): string;
|
|
14
15
|
toUrn(encoding?: "hex" | "base32" | "base64"): string;
|
|
15
|
-
|
|
16
|
+
private formatBufferAsEncoding;
|
|
16
17
|
static toBase64UrlSafe(base64Standard: string): string;
|
|
17
18
|
equals(other: Udi): boolean;
|
|
18
19
|
toString(): string;
|
package/dist/utils/Udi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Udi.d.ts","sourceRoot":"","sources":["../../src/utils/Udi.ts"],"names":[],"mappings":"AAIA,cAAM,GAAG;IACP,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"Udi.d.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,GAAG,IAAI,CAE5B;IAED,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED,IAAI,cAAc,IAAI,MAAM,GAAG,IAAI,CAElC;IAED,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED,IAAI,cAAc,IAAI,MAAM,GAAG,IAAI,CAElC;CACF;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
package/dist/utils/Udi.js
CHANGED
|
@@ -33,52 +33,17 @@ class Udi {
|
|
|
33
33
|
throw new Error("storeId cannot be empty");
|
|
34
34
|
}
|
|
35
35
|
this.chainName = chainName || "chia";
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
36
|
+
this._storeIdHex = Udi.verifyAndFormatHex(storeId);
|
|
37
|
+
this._rootHashHex = rootHash ? Udi.verifyAndFormatHex(rootHash) : null;
|
|
38
38
|
this.resourceKey = resourceKey;
|
|
39
39
|
}
|
|
40
|
-
static
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
throw new Error("Buffer must be exactly 32 bytes.");
|
|
44
|
-
}
|
|
45
|
-
return input;
|
|
40
|
+
static verifyAndFormatHex(input) {
|
|
41
|
+
if (!/^[a-fA-F0-9]{64}$/.test(input)) {
|
|
42
|
+
throw new Error("Input must be a 64-character hex string.");
|
|
46
43
|
}
|
|
47
|
-
|
|
48
|
-
if (/^[a-fA-F0-9]+$/.test(input) && input.length === 64) {
|
|
49
|
-
try {
|
|
50
|
-
const buffer = Buffer.from(input, "hex");
|
|
51
|
-
if (buffer.length === 32)
|
|
52
|
-
return buffer;
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
console.warn("Hex decoding failed, trying next encoding...");
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// Attempt Base32 decoding
|
|
59
|
-
try {
|
|
60
|
-
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
|
|
61
|
-
const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
|
|
62
|
-
if (buffer.length === 32)
|
|
63
|
-
return buffer;
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
console.warn("Base32 decoding failed, trying Base64 encoding...");
|
|
67
|
-
}
|
|
68
|
-
// Attempt Base64 (URL-safe) decoding
|
|
69
|
-
try {
|
|
70
|
-
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
|
|
71
|
-
const buffer = Buffer.from(standardBase64, "base64");
|
|
72
|
-
if (buffer.length === 32)
|
|
73
|
-
return buffer;
|
|
74
|
-
}
|
|
75
|
-
catch (e) {
|
|
76
|
-
throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string.");
|
|
77
|
-
}
|
|
78
|
-
throw new Error("Failed to decode input as a 32-byte buffer.");
|
|
44
|
+
return input.toLowerCase();
|
|
79
45
|
}
|
|
80
46
|
static fromUrn(urn) {
|
|
81
|
-
console.log(`Parsing URN: ${urn}`);
|
|
82
47
|
const parsedUrn = urns.parseURN(urn);
|
|
83
48
|
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
84
49
|
throw new Error(`Invalid nid: ${parsedUrn.nid}`);
|
|
@@ -88,24 +53,46 @@ class Udi {
|
|
|
88
53
|
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
89
54
|
}
|
|
90
55
|
const chainName = parts[0];
|
|
91
|
-
const
|
|
92
|
-
let
|
|
56
|
+
const storeIdHex = Udi.convertToHex(parts[1].split("/")[0]);
|
|
57
|
+
let rootHashHex = null;
|
|
93
58
|
if (parts.length > 2) {
|
|
94
|
-
|
|
59
|
+
rootHashHex = Udi.convertToHex(parts[2].split("/")[0]);
|
|
95
60
|
}
|
|
96
61
|
const pathParts = parsedUrn.nss.split("/");
|
|
97
62
|
let resourceKey = null;
|
|
98
63
|
if (pathParts.length > 1) {
|
|
99
64
|
resourceKey = pathParts.slice(1).join("/");
|
|
100
65
|
}
|
|
101
|
-
return new Udi(chainName,
|
|
66
|
+
return new Udi(chainName, storeIdHex, rootHashHex, resourceKey);
|
|
67
|
+
}
|
|
68
|
+
static convertToHex(input) {
|
|
69
|
+
// Attempt hex conversion first
|
|
70
|
+
if (/^[a-fA-F0-9]{64}$/.test(input))
|
|
71
|
+
return input.toLowerCase();
|
|
72
|
+
// Convert from Base32
|
|
73
|
+
try {
|
|
74
|
+
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
|
|
75
|
+
const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
|
|
76
|
+
return buffer.toString("hex");
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
console.warn("Base32 decoding failed, trying Base64 encoding...");
|
|
80
|
+
}
|
|
81
|
+
// Convert from Base64
|
|
82
|
+
try {
|
|
83
|
+
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
|
|
84
|
+
const buffer = Buffer.from(standardBase64, "base64");
|
|
85
|
+
return buffer.toString("hex");
|
|
86
|
+
}
|
|
87
|
+
catch (e) {
|
|
88
|
+
throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string.");
|
|
89
|
+
}
|
|
102
90
|
}
|
|
103
91
|
static addBase32Padding(input) {
|
|
104
92
|
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
105
93
|
return input + "=".repeat(paddingNeeded);
|
|
106
94
|
}
|
|
107
95
|
static toStandardBase64(base64UrlSafe) {
|
|
108
|
-
// Convert URL-safe Base64 to standard Base64
|
|
109
96
|
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
110
97
|
}
|
|
111
98
|
static addBase64Padding(base64) {
|
|
@@ -113,10 +100,10 @@ class Udi {
|
|
|
113
100
|
return base64 + "=".repeat(paddingNeeded);
|
|
114
101
|
}
|
|
115
102
|
toUrn(encoding = "hex") {
|
|
116
|
-
const storeIdStr = this.
|
|
103
|
+
const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
|
|
117
104
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
118
|
-
if (this.
|
|
119
|
-
const rootHashStr = this.
|
|
105
|
+
if (this._rootHashHex) {
|
|
106
|
+
const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
|
|
120
107
|
urn += `:${rootHashStr}`;
|
|
121
108
|
}
|
|
122
109
|
if (this.resourceKey) {
|
|
@@ -124,9 +111,10 @@ class Udi {
|
|
|
124
111
|
}
|
|
125
112
|
return urn;
|
|
126
113
|
}
|
|
127
|
-
|
|
114
|
+
formatBufferAsEncoding(hexString, encoding) {
|
|
115
|
+
const buffer = Buffer.from(hexString, "hex");
|
|
128
116
|
if (encoding === "hex") {
|
|
129
|
-
return
|
|
117
|
+
return hexString;
|
|
130
118
|
}
|
|
131
119
|
else if (encoding === "base32") {
|
|
132
120
|
return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
@@ -140,18 +128,18 @@ class Udi {
|
|
|
140
128
|
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
141
129
|
}
|
|
142
130
|
equals(other) {
|
|
143
|
-
return (this.
|
|
131
|
+
return (this._storeIdHex === other._storeIdHex &&
|
|
144
132
|
this.chainName === other.chainName &&
|
|
145
|
-
(this.
|
|
146
|
-
? this.
|
|
147
|
-
: this.
|
|
133
|
+
(this._rootHashHex && other._rootHashHex
|
|
134
|
+
? this._rootHashHex === other._rootHashHex
|
|
135
|
+
: this._rootHashHex === other._rootHashHex) &&
|
|
148
136
|
this.resourceKey === other.resourceKey);
|
|
149
137
|
}
|
|
150
138
|
toString() {
|
|
151
139
|
return this.toUrn();
|
|
152
140
|
}
|
|
153
141
|
clone() {
|
|
154
|
-
return new Udi(this.chainName, this.
|
|
142
|
+
return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
|
|
155
143
|
}
|
|
156
144
|
hashCode() {
|
|
157
145
|
const hash = (0, crypto_1.createHash)("sha256");
|
|
@@ -159,22 +147,22 @@ class Udi {
|
|
|
159
147
|
return hash.digest("hex");
|
|
160
148
|
}
|
|
161
149
|
get storeId() {
|
|
162
|
-
return this.
|
|
150
|
+
return this._storeIdHex;
|
|
163
151
|
}
|
|
164
152
|
get rootHash() {
|
|
165
|
-
return this.
|
|
153
|
+
return this._rootHashHex;
|
|
166
154
|
}
|
|
167
155
|
get storeIdBase32() {
|
|
168
|
-
return this.
|
|
156
|
+
return this.formatBufferAsEncoding(this._storeIdHex, "base32");
|
|
169
157
|
}
|
|
170
158
|
get rootHashBase32() {
|
|
171
|
-
return this.
|
|
159
|
+
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
|
|
172
160
|
}
|
|
173
161
|
get storeIdBase64() {
|
|
174
|
-
return this.
|
|
162
|
+
return this.formatBufferAsEncoding(this._storeIdHex, "base64");
|
|
175
163
|
}
|
|
176
164
|
get rootHashBase64() {
|
|
177
|
-
return this.
|
|
165
|
+
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
|
|
178
166
|
}
|
|
179
167
|
}
|
|
180
168
|
exports.Udi = Udi;
|