@dignetwork/dig-sdk 0.0.1-alpha.189 → 0.0.1-alpha.190
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 +10 -15
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +61 -87
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
1
|
declare class Udi {
|
|
2
2
|
readonly chainName: string;
|
|
3
|
-
private readonly
|
|
4
|
-
private readonly
|
|
3
|
+
private readonly _storeId;
|
|
4
|
+
private readonly _rootHash;
|
|
5
5
|
readonly resourceKey: string | null;
|
|
6
6
|
static readonly nid: string;
|
|
7
7
|
static readonly namespace: string;
|
|
8
|
-
constructor(chainName: string, storeId: string, rootHash?: string | null, resourceKey?: string | null);
|
|
9
|
-
static
|
|
8
|
+
constructor(chainName: string, storeId: string | Buffer, rootHash?: string | Buffer | null, resourceKey?: string | null);
|
|
9
|
+
static convertToBuffer(input: string | Buffer): Buffer;
|
|
10
|
+
static isHex(input: string): boolean;
|
|
11
|
+
static isBase32(input: string): boolean;
|
|
12
|
+
withRootHash(rootHash: string | Buffer | null): Udi;
|
|
13
|
+
withResourceKey(resourceKey: string | null): Udi;
|
|
10
14
|
static fromUrn(urn: string): Udi;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
static toStandardBase64(base64UrlSafe: string): string;
|
|
14
|
-
static addBase64Padding(base64: string): string;
|
|
15
|
-
toUrn(encoding?: "hex" | "base32" | "base64"): string;
|
|
16
|
-
private formatBufferAsEncoding;
|
|
17
|
-
static toBase64UrlSafe(base64Standard: string): string;
|
|
15
|
+
toUrn(encoding?: 'hex' | 'base32' | 'base64'): string;
|
|
16
|
+
bufferToString(buffer: Buffer, encoding: 'hex' | 'base32' | 'base64'): string;
|
|
18
17
|
equals(other: Udi): boolean;
|
|
19
18
|
toString(): string;
|
|
20
19
|
clone(): Udi;
|
|
21
20
|
hashCode(): string;
|
|
22
21
|
get storeId(): string;
|
|
23
22
|
get rootHash(): string | null;
|
|
24
|
-
get storeIdBase32(): string;
|
|
25
|
-
get rootHashBase32(): string | null;
|
|
26
|
-
get storeIdBase64(): string;
|
|
27
|
-
get rootHashBase64(): string | null;
|
|
28
23
|
}
|
|
29
24
|
export { Udi };
|
|
30
25
|
//# sourceMappingURL=Udi.d.ts.map
|
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":"AAUA,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,CAAS;IACpC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAoB;gBAGnD,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,QAAQ,GAAE,MAAM,GAAG,MAAM,GAAG,IAAW,EACvC,WAAW,GAAE,MAAM,GAAG,IAAW;IAWnC,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM;IAgBtD,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIpC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIvC,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,GAAG;IAInD,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,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;IAa7E,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAS3B,QAAQ,IAAI,MAAM;IAIlB,KAAK,IAAI,GAAG;IAIZ,QAAQ,IAAI,MAAM;IAOlB,IAAI,OAAO,IAAI,MAAM,CAEpB;IAGD,IAAI,QAAQ,IAAI,MAAM,GAAG,IAAI,CAE5B;CACF;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
|
package/dist/utils/Udi.js
CHANGED
|
@@ -27,83 +27,73 @@ 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
|
+
//
|
|
30
36
|
class Udi {
|
|
31
37
|
constructor(chainName, storeId, rootHash = null, resourceKey = null) {
|
|
32
38
|
if (!storeId) {
|
|
33
39
|
throw new Error("storeId cannot be empty");
|
|
34
40
|
}
|
|
35
41
|
this.chainName = chainName || "chia";
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
42
|
+
this._storeId = Udi.convertToBuffer(storeId);
|
|
43
|
+
this._rootHash = rootHash ? Udi.convertToBuffer(rootHash) : null;
|
|
38
44
|
this.resourceKey = resourceKey;
|
|
39
45
|
}
|
|
40
|
-
static
|
|
41
|
-
if (
|
|
42
|
-
|
|
46
|
+
static convertToBuffer(input) {
|
|
47
|
+
if (Buffer.isBuffer(input)) {
|
|
48
|
+
return input;
|
|
49
|
+
}
|
|
50
|
+
if (Udi.isHex(input)) {
|
|
51
|
+
return Buffer.from(input, 'hex');
|
|
52
|
+
}
|
|
53
|
+
if (Udi.isBase32(input)) {
|
|
54
|
+
return Buffer.from((0, hi_base32_1.decode)(input.toUpperCase(), false)); // Decode with uppercase
|
|
43
55
|
}
|
|
44
|
-
|
|
56
|
+
throw new Error("Invalid input encoding. Must be 32-byte hex or Base32 string.");
|
|
57
|
+
}
|
|
58
|
+
static isHex(input) {
|
|
59
|
+
return /^[a-fA-F0-9]{64}$/.test(input);
|
|
60
|
+
}
|
|
61
|
+
static isBase32(input) {
|
|
62
|
+
return /^[A-Z2-7]{52}$/.test(input.toUpperCase());
|
|
63
|
+
}
|
|
64
|
+
withRootHash(rootHash) {
|
|
65
|
+
return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey);
|
|
66
|
+
}
|
|
67
|
+
withResourceKey(resourceKey) {
|
|
68
|
+
return new Udi(this.chainName, this._storeId, this._rootHash, resourceKey);
|
|
45
69
|
}
|
|
46
70
|
static fromUrn(urn) {
|
|
47
71
|
const parsedUrn = urns.parseURN(urn);
|
|
48
|
-
if (parsedUrn.nid !== Udi.nid) {
|
|
72
|
+
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
49
73
|
throw new Error(`Invalid nid: ${parsedUrn.nid}`);
|
|
50
74
|
}
|
|
51
|
-
const parts = parsedUrn.nss.split(
|
|
75
|
+
const parts = parsedUrn.nss.split(':');
|
|
52
76
|
if (parts.length < 2) {
|
|
53
77
|
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
54
78
|
}
|
|
55
79
|
const chainName = parts[0];
|
|
56
|
-
const
|
|
57
|
-
let
|
|
80
|
+
const storeId = parts[1].split('/')[0];
|
|
81
|
+
let rootHash = null;
|
|
58
82
|
if (parts.length > 2) {
|
|
59
|
-
|
|
83
|
+
rootHash = parts[2].split('/')[0];
|
|
60
84
|
}
|
|
61
|
-
const pathParts = parsedUrn.nss.split(
|
|
85
|
+
const pathParts = parsedUrn.nss.split('/');
|
|
62
86
|
let resourceKey = null;
|
|
63
87
|
if (pathParts.length > 1) {
|
|
64
|
-
resourceKey = pathParts.slice(1).join(
|
|
88
|
+
resourceKey = pathParts.slice(1).join('/');
|
|
65
89
|
}
|
|
66
|
-
return new Udi(chainName,
|
|
90
|
+
return new Udi(chainName, storeId, rootHash, resourceKey);
|
|
67
91
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (/^[a-fA-F0-9]{64}$/.test(input))
|
|
71
|
-
return input;
|
|
72
|
-
// Convert from Base32
|
|
73
|
-
try {
|
|
74
|
-
const paddedInput = Udi.addBase32Padding(input);
|
|
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
|
-
}
|
|
90
|
-
}
|
|
91
|
-
static addBase32Padding(input) {
|
|
92
|
-
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
93
|
-
return input + "=".repeat(paddingNeeded);
|
|
94
|
-
}
|
|
95
|
-
static toStandardBase64(base64UrlSafe) {
|
|
96
|
-
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
97
|
-
}
|
|
98
|
-
static addBase64Padding(base64) {
|
|
99
|
-
const paddingNeeded = (4 - (base64.length % 4)) % 4;
|
|
100
|
-
return base64 + "=".repeat(paddingNeeded);
|
|
101
|
-
}
|
|
102
|
-
toUrn(encoding = "hex") {
|
|
103
|
-
const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
|
|
92
|
+
toUrn(encoding = 'hex') {
|
|
93
|
+
const storeIdStr = this.bufferToString(this._storeId, encoding);
|
|
104
94
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
105
|
-
if (this.
|
|
106
|
-
const rootHashStr = this.
|
|
95
|
+
if (this._rootHash) {
|
|
96
|
+
const rootHashStr = this.bufferToString(this._rootHash, encoding);
|
|
107
97
|
urn += `:${rootHashStr}`;
|
|
108
98
|
}
|
|
109
99
|
if (this.resourceKey) {
|
|
@@ -111,58 +101,42 @@ class Udi {
|
|
|
111
101
|
}
|
|
112
102
|
return urn;
|
|
113
103
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
104
|
+
bufferToString(buffer, encoding) {
|
|
105
|
+
switch (encoding) {
|
|
106
|
+
case 'hex':
|
|
107
|
+
return buffer.toString('hex');
|
|
108
|
+
case 'base32':
|
|
109
|
+
return (0, hi_base32_1.encode)(buffer).toUpperCase().replace(/=+$/, ''); // Convert to uppercase and remove padding
|
|
110
|
+
case 'base64':
|
|
111
|
+
return buffer.toString('base64').toLowerCase(); // Convert to lowercase
|
|
112
|
+
default:
|
|
113
|
+
throw new Error("Unsupported encoding");
|
|
124
114
|
}
|
|
125
|
-
throw new Error("Unsupported encoding type");
|
|
126
|
-
}
|
|
127
|
-
static toBase64UrlSafe(base64Standard) {
|
|
128
|
-
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
129
115
|
}
|
|
130
116
|
equals(other) {
|
|
131
|
-
return (this.
|
|
117
|
+
return (this._storeId.equals(other._storeId) &&
|
|
132
118
|
this.chainName === other.chainName &&
|
|
133
|
-
(this.
|
|
134
|
-
? this._rootHashHex === other._rootHashHex
|
|
135
|
-
: this._rootHashHex === other._rootHashHex) &&
|
|
119
|
+
(this._rootHash && other._rootHash ? this._rootHash.equals(other._rootHash) : this._rootHash === other._rootHash) &&
|
|
136
120
|
this.resourceKey === other.resourceKey);
|
|
137
121
|
}
|
|
138
122
|
toString() {
|
|
139
123
|
return this.toUrn();
|
|
140
124
|
}
|
|
141
125
|
clone() {
|
|
142
|
-
return new Udi(this.chainName, this.
|
|
126
|
+
return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
|
|
143
127
|
}
|
|
144
128
|
hashCode() {
|
|
145
|
-
const hash = (0, crypto_1.createHash)(
|
|
129
|
+
const hash = (0, crypto_1.createHash)('sha256');
|
|
146
130
|
hash.update(this.toUrn());
|
|
147
|
-
return hash.digest(
|
|
131
|
+
return hash.digest('hex');
|
|
148
132
|
}
|
|
133
|
+
// Getter for storeId as a hex string
|
|
149
134
|
get storeId() {
|
|
150
|
-
return this.
|
|
135
|
+
return this._storeId.toString('hex');
|
|
151
136
|
}
|
|
137
|
+
// Getter for rootHash as a hex string
|
|
152
138
|
get rootHash() {
|
|
153
|
-
return this.
|
|
154
|
-
}
|
|
155
|
-
get storeIdBase32() {
|
|
156
|
-
return this.formatBufferAsEncoding(this._storeIdHex, "base32");
|
|
157
|
-
}
|
|
158
|
-
get rootHashBase32() {
|
|
159
|
-
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
|
|
160
|
-
}
|
|
161
|
-
get storeIdBase64() {
|
|
162
|
-
return this.formatBufferAsEncoding(this._storeIdHex, "base64");
|
|
163
|
-
}
|
|
164
|
-
get rootHashBase64() {
|
|
165
|
-
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
|
|
139
|
+
return this._rootHash ? this._rootHash.toString('hex') : null;
|
|
166
140
|
}
|
|
167
141
|
}
|
|
168
142
|
exports.Udi = Udi;
|