@dignetwork/dig-sdk 0.0.1-alpha.180 → 0.0.1-alpha.181
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 +3 -2
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +28 -28
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -9,11 +9,12 @@ 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 addBase32Padding(input: string): string;
|
|
12
13
|
withRootHash(rootHash: string | Buffer | null): Udi;
|
|
13
14
|
withResourceKey(resourceKey: string | null): Udi;
|
|
14
15
|
static fromUrn(urn: string): Udi;
|
|
15
|
-
toUrn(encoding?:
|
|
16
|
-
bufferToString(buffer: Buffer, encoding:
|
|
16
|
+
toUrn(encoding?: "hex" | "base32"): string;
|
|
17
|
+
bufferToString(buffer: Buffer, encoding: "hex" | "base32"): string;
|
|
17
18
|
equals(other: Udi): boolean;
|
|
18
19
|
toString(): string;
|
|
19
20
|
clone(): Udi;
|
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":"
|
|
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,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgB;IAC1C,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CA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
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,10 +42,11 @@ 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)); // Decode as UTF-8
|
|
55
50
|
}
|
|
56
51
|
throw new Error("Invalid input encoding. Must be 32-byte hex or Base32 string.");
|
|
57
52
|
}
|
|
@@ -61,6 +56,11 @@ class Udi {
|
|
|
61
56
|
static isBase32(input) {
|
|
62
57
|
return /^[a-z2-7]{52}$/.test(input.toLowerCase());
|
|
63
58
|
}
|
|
59
|
+
static addBase32Padding(input) {
|
|
60
|
+
// Calculate required padding
|
|
61
|
+
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
62
|
+
return input + "=".repeat(paddingNeeded);
|
|
63
|
+
}
|
|
64
64
|
withRootHash(rootHash) {
|
|
65
65
|
return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey);
|
|
66
66
|
}
|
|
@@ -72,24 +72,24 @@ class Udi {
|
|
|
72
72
|
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
73
73
|
throw new Error(`Invalid nid: ${parsedUrn.nid}`);
|
|
74
74
|
}
|
|
75
|
-
const parts = parsedUrn.nss.split(
|
|
75
|
+
const parts = parsedUrn.nss.split(":");
|
|
76
76
|
if (parts.length < 2) {
|
|
77
77
|
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
78
78
|
}
|
|
79
79
|
const chainName = parts[0];
|
|
80
|
-
const storeId = parts[1].split(
|
|
80
|
+
const storeId = parts[1].split("/")[0];
|
|
81
81
|
let rootHash = null;
|
|
82
82
|
if (parts.length > 2) {
|
|
83
|
-
rootHash = parts[2].split(
|
|
83
|
+
rootHash = parts[2].split("/")[0];
|
|
84
84
|
}
|
|
85
|
-
const pathParts = parsedUrn.nss.split(
|
|
85
|
+
const pathParts = parsedUrn.nss.split("/");
|
|
86
86
|
let resourceKey = null;
|
|
87
87
|
if (pathParts.length > 1) {
|
|
88
|
-
resourceKey = pathParts.slice(1).join(
|
|
88
|
+
resourceKey = pathParts.slice(1).join("/");
|
|
89
89
|
}
|
|
90
90
|
return new Udi(chainName, storeId, rootHash, resourceKey);
|
|
91
91
|
}
|
|
92
|
-
toUrn(encoding =
|
|
92
|
+
toUrn(encoding = "hex") {
|
|
93
93
|
const storeIdStr = this.bufferToString(this._storeId, encoding);
|
|
94
94
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
95
95
|
if (this._rootHash) {
|
|
@@ -102,14 +102,16 @@ class Udi {
|
|
|
102
102
|
return urn;
|
|
103
103
|
}
|
|
104
104
|
bufferToString(buffer, encoding) {
|
|
105
|
-
return encoding ===
|
|
106
|
-
? buffer.toString(
|
|
107
|
-
: (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/,
|
|
105
|
+
return encoding === "hex"
|
|
106
|
+
? buffer.toString("hex")
|
|
107
|
+
: (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
108
108
|
}
|
|
109
109
|
equals(other) {
|
|
110
110
|
return (this._storeId.equals(other._storeId) &&
|
|
111
111
|
this.chainName === other.chainName &&
|
|
112
|
-
(this._rootHash && other._rootHash
|
|
112
|
+
(this._rootHash && other._rootHash
|
|
113
|
+
? this._rootHash.equals(other._rootHash)
|
|
114
|
+
: this._rootHash === other._rootHash) &&
|
|
113
115
|
this.resourceKey === other.resourceKey);
|
|
114
116
|
}
|
|
115
117
|
toString() {
|
|
@@ -119,25 +121,23 @@ class Udi {
|
|
|
119
121
|
return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
|
|
120
122
|
}
|
|
121
123
|
hashCode() {
|
|
122
|
-
const hash = (0, crypto_1.createHash)(
|
|
124
|
+
const hash = (0, crypto_1.createHash)("sha256");
|
|
123
125
|
hash.update(this.toUrn());
|
|
124
|
-
return hash.digest(
|
|
126
|
+
return hash.digest("hex");
|
|
125
127
|
}
|
|
126
|
-
// Getter for storeId as a hex string
|
|
127
128
|
get storeId() {
|
|
128
|
-
return this._storeId.toString(
|
|
129
|
+
return this._storeId.toString("hex");
|
|
129
130
|
}
|
|
130
|
-
// Getter for rootHash as a hex string
|
|
131
131
|
get rootHash() {
|
|
132
|
-
return this._rootHash ? this._rootHash.toString(
|
|
132
|
+
return this._rootHash ? this._rootHash.toString("hex") : null;
|
|
133
133
|
}
|
|
134
|
-
// Getter for storeId as a hex string
|
|
135
134
|
get storeIdBase32() {
|
|
136
|
-
return this.bufferToString(this._storeId,
|
|
135
|
+
return this.bufferToString(this._storeId, "base32");
|
|
137
136
|
}
|
|
138
|
-
// Getter for rootHash as a hex string
|
|
139
137
|
get rootHashBase32() {
|
|
140
|
-
return this._rootHash
|
|
138
|
+
return this._rootHash
|
|
139
|
+
? this.bufferToString(this._rootHash, "base32")
|
|
140
|
+
: null;
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
exports.Udi = Udi;
|