@dignetwork/dig-sdk 0.0.1-alpha.180 → 0.0.1-alpha.182
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 +9 -2
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +57 -30
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -9,11 +9,16 @@ declare class Udi {
|
|
|
9
9
|
static convertToBuffer(input: string | Buffer): Buffer;
|
|
10
10
|
static isHex(input: string): boolean;
|
|
11
11
|
static isBase32(input: string): boolean;
|
|
12
|
+
static isBase64Safe(input: string): boolean;
|
|
13
|
+
static addBase32Padding(input: string): string;
|
|
14
|
+
static toStandardBase64(base64Safe: string): string;
|
|
15
|
+
static toBase64Safe(base64Standard: string): string;
|
|
16
|
+
static addBase64Padding(base64: string): string;
|
|
12
17
|
withRootHash(rootHash: string | Buffer | null): Udi;
|
|
13
18
|
withResourceKey(resourceKey: string | null): Udi;
|
|
14
19
|
static fromUrn(urn: string): Udi;
|
|
15
|
-
toUrn(encoding?:
|
|
16
|
-
bufferToString(buffer: Buffer, encoding:
|
|
20
|
+
toUrn(encoding?: "hex" | "base32" | "base64"): string;
|
|
21
|
+
bufferToString(buffer: Buffer, encoding: "hex" | "base32" | "base64"): string;
|
|
17
22
|
equals(other: Udi): boolean;
|
|
18
23
|
toString(): string;
|
|
19
24
|
clone(): Udi;
|
|
@@ -22,6 +27,8 @@ declare class Udi {
|
|
|
22
27
|
get rootHash(): string | null;
|
|
23
28
|
get storeIdBase32(): string;
|
|
24
29
|
get rootHashBase32(): string | null;
|
|
30
|
+
get storeIdBase64(): string;
|
|
31
|
+
get rootHashBase64(): string | null;
|
|
25
32
|
}
|
|
26
33
|
export { Udi };
|
|
27
34
|
//# sourceMappingURL=Udi.d.ts.map
|
package/dist/utils/Udi.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file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AAG,IAAI,GAAG,GAAG;IAIhD,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;IA4BhC,KAAK,CAAC,QAAQ,GAAE,KAAK,GAAG,QAAQ,GAAG,QAAgB,GAAG,MAAM;IAgB5D,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM;IAW7E,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAW3B,QAAQ,IAAI,MAAM;IAIlB,KAAK,IAAI,GAAG;IAIZ,QAAQ,IAAI,MAAM;IAMlB,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,QAAQ,IAAI,MAAM,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
|
@@ -27,12 +27,6 @@ exports.Udi = void 0;
|
|
|
27
27
|
const urns = __importStar(require("urns"));
|
|
28
28
|
const crypto_1 = require("crypto");
|
|
29
29
|
const hi_base32_1 = require("hi-base32");
|
|
30
|
-
//
|
|
31
|
-
// This class encapsulates the concept of a Universal Data Identifier (UDI), which is a
|
|
32
|
-
// standardized way to identify resources across the distributed DIG mesh network.
|
|
33
|
-
// The UDI format: urn:dig:chainName:storeId:rootHash/resourceKey
|
|
34
|
-
// This allows unique resource identification across the DIG network.
|
|
35
|
-
//
|
|
36
30
|
class Udi {
|
|
37
31
|
constructor(chainName, storeId, rootHash = null, resourceKey = null) {
|
|
38
32
|
if (!storeId) {
|
|
@@ -48,12 +42,17 @@ class Udi {
|
|
|
48
42
|
return input;
|
|
49
43
|
}
|
|
50
44
|
if (Udi.isHex(input)) {
|
|
51
|
-
return Buffer.from(input,
|
|
45
|
+
return Buffer.from(input, "hex");
|
|
52
46
|
}
|
|
53
47
|
if (Udi.isBase32(input)) {
|
|
54
|
-
|
|
48
|
+
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
|
|
49
|
+
return Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
|
|
55
50
|
}
|
|
56
|
-
|
|
51
|
+
if (Udi.isBase64Safe(input)) {
|
|
52
|
+
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
|
|
53
|
+
return Buffer.from(standardBase64, "base64");
|
|
54
|
+
}
|
|
55
|
+
throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 URL-safe string.");
|
|
57
56
|
}
|
|
58
57
|
static isHex(input) {
|
|
59
58
|
return /^[a-fA-F0-9]{64}$/.test(input);
|
|
@@ -61,6 +60,23 @@ class Udi {
|
|
|
61
60
|
static isBase32(input) {
|
|
62
61
|
return /^[a-z2-7]{52}$/.test(input.toLowerCase());
|
|
63
62
|
}
|
|
63
|
+
static isBase64Safe(input) {
|
|
64
|
+
return /^[A-Za-z0-9\-_]+$/.test(input);
|
|
65
|
+
}
|
|
66
|
+
static addBase32Padding(input) {
|
|
67
|
+
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
68
|
+
return input + "=".repeat(paddingNeeded);
|
|
69
|
+
}
|
|
70
|
+
static toStandardBase64(base64Safe) {
|
|
71
|
+
return base64Safe.replace(/-/g, "+").replace(/_/g, "/");
|
|
72
|
+
}
|
|
73
|
+
static toBase64Safe(base64Standard) {
|
|
74
|
+
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
75
|
+
}
|
|
76
|
+
static addBase64Padding(base64) {
|
|
77
|
+
const paddingNeeded = (4 - (base64.length % 4)) % 4;
|
|
78
|
+
return base64 + "=".repeat(paddingNeeded);
|
|
79
|
+
}
|
|
64
80
|
withRootHash(rootHash) {
|
|
65
81
|
return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey);
|
|
66
82
|
}
|
|
@@ -70,26 +86,26 @@ class Udi {
|
|
|
70
86
|
static fromUrn(urn) {
|
|
71
87
|
const parsedUrn = urns.parseURN(urn);
|
|
72
88
|
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
73
|
-
throw new Error(`Invalid
|
|
89
|
+
throw new Error(`Invalid UDI: ${parsedUrn.nid}`);
|
|
74
90
|
}
|
|
75
|
-
const parts = parsedUrn.nss.split(
|
|
91
|
+
const parts = parsedUrn.nss.split(":");
|
|
76
92
|
if (parts.length < 2) {
|
|
77
93
|
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
78
94
|
}
|
|
79
95
|
const chainName = parts[0];
|
|
80
|
-
const storeId = parts[1].split(
|
|
96
|
+
const storeId = parts[1].split("/")[0];
|
|
81
97
|
let rootHash = null;
|
|
82
98
|
if (parts.length > 2) {
|
|
83
|
-
rootHash = parts[2].split(
|
|
99
|
+
rootHash = parts[2].split("/")[0];
|
|
84
100
|
}
|
|
85
|
-
const pathParts = parsedUrn.nss.split(
|
|
101
|
+
const pathParts = parsedUrn.nss.split("/");
|
|
86
102
|
let resourceKey = null;
|
|
87
103
|
if (pathParts.length > 1) {
|
|
88
|
-
resourceKey = pathParts.slice(1).join(
|
|
104
|
+
resourceKey = pathParts.slice(1).join("/");
|
|
89
105
|
}
|
|
90
106
|
return new Udi(chainName, storeId, rootHash, resourceKey);
|
|
91
107
|
}
|
|
92
|
-
toUrn(encoding =
|
|
108
|
+
toUrn(encoding = "hex") {
|
|
93
109
|
const storeIdStr = this.bufferToString(this._storeId, encoding);
|
|
94
110
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
95
111
|
if (this._rootHash) {
|
|
@@ -102,14 +118,23 @@ class Udi {
|
|
|
102
118
|
return urn;
|
|
103
119
|
}
|
|
104
120
|
bufferToString(buffer, encoding) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
121
|
+
if (encoding === "hex") {
|
|
122
|
+
return buffer.toString("hex");
|
|
123
|
+
}
|
|
124
|
+
else if (encoding === "base32") {
|
|
125
|
+
return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
126
|
+
}
|
|
127
|
+
else if (encoding === "base64") {
|
|
128
|
+
return Udi.toBase64Safe(buffer.toString("base64"));
|
|
129
|
+
}
|
|
130
|
+
throw new Error("Unsupported encoding type");
|
|
108
131
|
}
|
|
109
132
|
equals(other) {
|
|
110
133
|
return (this._storeId.equals(other._storeId) &&
|
|
111
134
|
this.chainName === other.chainName &&
|
|
112
|
-
(this._rootHash && other._rootHash
|
|
135
|
+
(this._rootHash && other._rootHash
|
|
136
|
+
? this._rootHash.equals(other._rootHash)
|
|
137
|
+
: this._rootHash === other._rootHash) &&
|
|
113
138
|
this.resourceKey === other.resourceKey);
|
|
114
139
|
}
|
|
115
140
|
toString() {
|
|
@@ -119,25 +144,27 @@ class Udi {
|
|
|
119
144
|
return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
|
|
120
145
|
}
|
|
121
146
|
hashCode() {
|
|
122
|
-
const hash = (0, crypto_1.createHash)(
|
|
147
|
+
const hash = (0, crypto_1.createHash)("sha256");
|
|
123
148
|
hash.update(this.toUrn());
|
|
124
|
-
return hash.digest(
|
|
149
|
+
return hash.digest("hex");
|
|
125
150
|
}
|
|
126
|
-
// Getter for storeId as a hex string
|
|
127
151
|
get storeId() {
|
|
128
|
-
return this._storeId.toString(
|
|
152
|
+
return this._storeId.toString("hex");
|
|
129
153
|
}
|
|
130
|
-
// Getter for rootHash as a hex string
|
|
131
154
|
get rootHash() {
|
|
132
|
-
return this._rootHash ? this._rootHash.toString(
|
|
155
|
+
return this._rootHash ? this._rootHash.toString("hex") : null;
|
|
133
156
|
}
|
|
134
|
-
// Getter for storeId as a hex string
|
|
135
157
|
get storeIdBase32() {
|
|
136
|
-
return this.bufferToString(this._storeId,
|
|
158
|
+
return this.bufferToString(this._storeId, "base32");
|
|
137
159
|
}
|
|
138
|
-
// Getter for rootHash as a hex string
|
|
139
160
|
get rootHashBase32() {
|
|
140
|
-
return this._rootHash ? this.bufferToString(this._rootHash,
|
|
161
|
+
return this._rootHash ? this.bufferToString(this._rootHash, "base32") : null;
|
|
162
|
+
}
|
|
163
|
+
get storeIdBase64() {
|
|
164
|
+
return this.bufferToString(this._storeId, "base64");
|
|
165
|
+
}
|
|
166
|
+
get rootHashBase64() {
|
|
167
|
+
return this._rootHash ? this.bufferToString(this._rootHash, "base64") : null;
|
|
141
168
|
}
|
|
142
169
|
}
|
|
143
170
|
exports.Udi = Udi;
|