@dignetwork/dig-sdk 0.0.1-alpha.190 → 0.0.1-alpha.191

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.
@@ -1,25 +1,30 @@
1
1
  declare class Udi {
2
2
  readonly chainName: string;
3
- private readonly _storeId;
4
- private readonly _rootHash;
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 | 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;
8
+ constructor(chainName: string, storeId: string, rootHash?: string | null, resourceKey?: string | null);
9
+ static verifyAndFormatHex(input: string): string;
14
10
  static fromUrn(urn: string): Udi;
15
- toUrn(encoding?: 'hex' | 'base32' | 'base64'): string;
16
- bufferToString(buffer: Buffer, encoding: 'hex' | 'base32' | 'base64'): string;
11
+ static convertToHex(input: string): string;
12
+ static addBase32Padding(input: string): string;
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;
17
18
  equals(other: Udi): boolean;
18
19
  toString(): string;
19
20
  clone(): Udi;
20
21
  hashCode(): string;
21
22
  get storeId(): string;
22
23
  get rootHash(): string | null;
24
+ get storeIdBase32(): string;
25
+ get rootHashBase32(): string | null;
26
+ get storeIdBase64(): string;
27
+ get rootHashBase64(): string | null;
23
28
  }
24
29
  export { Udi };
25
30
  //# sourceMappingURL=Udi.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Udi.d.ts","sourceRoot":"","sources":["../../src/utils/Udi.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AK,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,73 +27,83 @@ 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) {
39
33
  throw new Error("storeId cannot be empty");
40
34
  }
41
35
  this.chainName = chainName || "chia";
42
- this._storeId = Udi.convertToBuffer(storeId);
43
- this._rootHash = rootHash ? Udi.convertToBuffer(rootHash) : null;
36
+ this._storeIdHex = Udi.verifyAndFormatHex(storeId);
37
+ this._rootHashHex = rootHash ? Udi.verifyAndFormatHex(rootHash) : null;
44
38
  this.resourceKey = resourceKey;
45
39
  }
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
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.");
55
43
  }
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);
44
+ return input;
69
45
  }
70
46
  static fromUrn(urn) {
71
47
  const parsedUrn = urns.parseURN(urn);
72
- if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
48
+ if (parsedUrn.nid !== Udi.nid) {
73
49
  throw new Error(`Invalid nid: ${parsedUrn.nid}`);
74
50
  }
75
- const parts = parsedUrn.nss.split(':');
51
+ const parts = parsedUrn.nss.split(":");
76
52
  if (parts.length < 2) {
77
53
  throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
78
54
  }
79
55
  const chainName = parts[0];
80
- const storeId = parts[1].split('/')[0];
81
- let rootHash = null;
56
+ const storeIdHex = Udi.convertToHex(parts[1].split("/")[0]);
57
+ let rootHashHex = null;
82
58
  if (parts.length > 2) {
83
- rootHash = parts[2].split('/')[0];
59
+ rootHashHex = Udi.convertToHex(parts[2].split("/")[0]);
84
60
  }
85
- const pathParts = parsedUrn.nss.split('/');
61
+ const pathParts = parsedUrn.nss.split("/");
86
62
  let resourceKey = null;
87
63
  if (pathParts.length > 1) {
88
- resourceKey = pathParts.slice(1).join('/');
64
+ resourceKey = pathParts.slice(1).join("/");
89
65
  }
90
- return new Udi(chainName, storeId, rootHash, resourceKey);
66
+ return new Udi(chainName, storeIdHex, rootHashHex, resourceKey);
91
67
  }
92
- toUrn(encoding = 'hex') {
93
- const storeIdStr = this.bufferToString(this._storeId, encoding);
68
+ static convertToHex(input) {
69
+ // Attempt hex conversion first
70
+ if (/^[a-fA-F0-9]{64}$/.test(input))
71
+ return input;
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
+ }
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);
94
104
  let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
95
- if (this._rootHash) {
96
- const rootHashStr = this.bufferToString(this._rootHash, encoding);
105
+ if (this._rootHashHex) {
106
+ const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
97
107
  urn += `:${rootHashStr}`;
98
108
  }
99
109
  if (this.resourceKey) {
@@ -101,42 +111,58 @@ class Udi {
101
111
  }
102
112
  return urn;
103
113
  }
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");
114
+ formatBufferAsEncoding(hexString, encoding) {
115
+ const buffer = Buffer.from(hexString, "hex");
116
+ if (encoding === "hex") {
117
+ return hexString;
118
+ }
119
+ else if (encoding === "base32") {
120
+ return (0, hi_base32_1.encode)(buffer).replace(/=+$/, ""); // Strip padding for Base32
121
+ }
122
+ else if (encoding === "base64") {
123
+ return Udi.toBase64UrlSafe(buffer.toString("base64")); // Convert to URL-safe Base64
114
124
  }
125
+ throw new Error("Unsupported encoding type");
126
+ }
127
+ static toBase64UrlSafe(base64Standard) {
128
+ return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
115
129
  }
116
130
  equals(other) {
117
- return (this._storeId.equals(other._storeId) &&
131
+ return (this._storeIdHex === other._storeIdHex &&
118
132
  this.chainName === other.chainName &&
119
- (this._rootHash && other._rootHash ? this._rootHash.equals(other._rootHash) : this._rootHash === other._rootHash) &&
133
+ (this._rootHashHex && other._rootHashHex
134
+ ? this._rootHashHex === other._rootHashHex
135
+ : this._rootHashHex === other._rootHashHex) &&
120
136
  this.resourceKey === other.resourceKey);
121
137
  }
122
138
  toString() {
123
139
  return this.toUrn();
124
140
  }
125
141
  clone() {
126
- return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
142
+ return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
127
143
  }
128
144
  hashCode() {
129
- const hash = (0, crypto_1.createHash)('sha256');
145
+ const hash = (0, crypto_1.createHash)("sha256");
130
146
  hash.update(this.toUrn());
131
- return hash.digest('hex');
147
+ return hash.digest("hex");
132
148
  }
133
- // Getter for storeId as a hex string
134
149
  get storeId() {
135
- return this._storeId.toString('hex');
150
+ return this._storeIdHex;
136
151
  }
137
- // Getter for rootHash as a hex string
138
152
  get rootHash() {
139
- return this._rootHash ? this._rootHash.toString('hex') : null;
153
+ return this._rootHashHex;
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;
140
166
  }
141
167
  }
142
168
  exports.Udi = Udi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dignetwork/dig-sdk",
3
- "version": "0.0.1-alpha.190",
3
+ "version": "0.0.1-alpha.191",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",