@dignetwork/dig-sdk 0.0.1-alpha.185 → 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 -61
- 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,17 +53,40 @@ 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;
|
|
@@ -112,10 +100,10 @@ class Udi {
|
|
|
112
100
|
return base64 + "=".repeat(paddingNeeded);
|
|
113
101
|
}
|
|
114
102
|
toUrn(encoding = "hex") {
|
|
115
|
-
const storeIdStr = this.
|
|
103
|
+
const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
|
|
116
104
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
117
|
-
if (this.
|
|
118
|
-
const rootHashStr = this.
|
|
105
|
+
if (this._rootHashHex) {
|
|
106
|
+
const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
|
|
119
107
|
urn += `:${rootHashStr}`;
|
|
120
108
|
}
|
|
121
109
|
if (this.resourceKey) {
|
|
@@ -123,9 +111,10 @@ class Udi {
|
|
|
123
111
|
}
|
|
124
112
|
return urn;
|
|
125
113
|
}
|
|
126
|
-
|
|
114
|
+
formatBufferAsEncoding(hexString, encoding) {
|
|
115
|
+
const buffer = Buffer.from(hexString, "hex");
|
|
127
116
|
if (encoding === "hex") {
|
|
128
|
-
return
|
|
117
|
+
return hexString;
|
|
129
118
|
}
|
|
130
119
|
else if (encoding === "base32") {
|
|
131
120
|
return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
@@ -139,18 +128,18 @@ class Udi {
|
|
|
139
128
|
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
140
129
|
}
|
|
141
130
|
equals(other) {
|
|
142
|
-
return (this.
|
|
131
|
+
return (this._storeIdHex === other._storeIdHex &&
|
|
143
132
|
this.chainName === other.chainName &&
|
|
144
|
-
(this.
|
|
145
|
-
? this.
|
|
146
|
-
: this.
|
|
133
|
+
(this._rootHashHex && other._rootHashHex
|
|
134
|
+
? this._rootHashHex === other._rootHashHex
|
|
135
|
+
: this._rootHashHex === other._rootHashHex) &&
|
|
147
136
|
this.resourceKey === other.resourceKey);
|
|
148
137
|
}
|
|
149
138
|
toString() {
|
|
150
139
|
return this.toUrn();
|
|
151
140
|
}
|
|
152
141
|
clone() {
|
|
153
|
-
return new Udi(this.chainName, this.
|
|
142
|
+
return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
|
|
154
143
|
}
|
|
155
144
|
hashCode() {
|
|
156
145
|
const hash = (0, crypto_1.createHash)("sha256");
|
|
@@ -158,22 +147,22 @@ class Udi {
|
|
|
158
147
|
return hash.digest("hex");
|
|
159
148
|
}
|
|
160
149
|
get storeId() {
|
|
161
|
-
return this.
|
|
150
|
+
return this._storeIdHex;
|
|
162
151
|
}
|
|
163
152
|
get rootHash() {
|
|
164
|
-
return this.
|
|
153
|
+
return this._rootHashHex;
|
|
165
154
|
}
|
|
166
155
|
get storeIdBase32() {
|
|
167
|
-
return this.
|
|
156
|
+
return this.formatBufferAsEncoding(this._storeIdHex, "base32");
|
|
168
157
|
}
|
|
169
158
|
get rootHashBase32() {
|
|
170
|
-
return this.
|
|
159
|
+
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
|
|
171
160
|
}
|
|
172
161
|
get storeIdBase64() {
|
|
173
|
-
return this.
|
|
162
|
+
return this.formatBufferAsEncoding(this._storeIdHex, "base64");
|
|
174
163
|
}
|
|
175
164
|
get rootHashBase64() {
|
|
176
|
-
return this.
|
|
165
|
+
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
|
|
177
166
|
}
|
|
178
167
|
}
|
|
179
168
|
exports.Udi = Udi;
|