@dignetwork/dig-sdk 0.0.1-alpha.186 → 0.0.1-alpha.189

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,18 +1,19 @@
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;
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
- bufferToString(buffer: Buffer, encoding: "hex" | "base32" | "base64"): string;
16
+ private formatBufferAsEncoding;
16
17
  static toBase64UrlSafe(base64Standard: string): string;
17
18
  equals(other: Udi): boolean;
18
19
  toString(): string;
@@ -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,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,M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,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,54 +33,19 @@ class Udi {
33
33
  throw new Error("storeId cannot be empty");
34
34
  }
35
35
  this.chainName = chainName || "chia";
36
- this._storeId = Udi.convertToBuffer(storeId);
37
- this._rootHash = rootHash ? Udi.convertToBuffer(rootHash) : null;
36
+ this._storeIdHex = Udi.verifyAndFormatHex(storeId);
37
+ this._rootHashHex = rootHash ? Udi.verifyAndFormatHex(rootHash) : null;
38
38
  this.resourceKey = resourceKey;
39
39
  }
40
- static convertToBuffer(input) {
41
- if (Buffer.isBuffer(input)) {
42
- if (input.length !== 32) {
43
- throw new Error("Buffer must be exactly 32 bytes.");
44
- }
45
- return input;
46
- }
47
- // Attempt hex decoding
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;
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.");
64
43
  }
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;
79
45
  }
80
46
  static fromUrn(urn) {
81
- console.log(`Parsing URN: ${urn}`);
82
47
  const parsedUrn = urns.parseURN(urn);
83
- if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
48
+ if (parsedUrn.nid !== Udi.nid) {
84
49
  throw new Error(`Invalid nid: ${parsedUrn.nid}`);
85
50
  }
86
51
  const parts = parsedUrn.nss.split(":");
@@ -88,24 +53,46 @@ class Udi {
88
53
  throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
89
54
  }
90
55
  const chainName = parts[0];
91
- const storeId = Udi.convertToBuffer(parts[1].split("/")[0]);
92
- let rootHash = null;
56
+ const storeIdHex = Udi.convertToHex(parts[1].split("/")[0]);
57
+ let rootHashHex = null;
93
58
  if (parts.length > 2) {
94
- rootHash = Udi.convertToBuffer(parts[2].split("/")[0]);
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, storeId, rootHash, resourceKey);
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;
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
+ }
102
90
  }
103
91
  static addBase32Padding(input) {
104
92
  const paddingNeeded = (8 - (input.length % 8)) % 8;
105
93
  return input + "=".repeat(paddingNeeded);
106
94
  }
107
95
  static toStandardBase64(base64UrlSafe) {
108
- // Convert URL-safe Base64 to standard Base64
109
96
  return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
110
97
  }
111
98
  static addBase64Padding(base64) {
@@ -113,10 +100,10 @@ class Udi {
113
100
  return base64 + "=".repeat(paddingNeeded);
114
101
  }
115
102
  toUrn(encoding = "hex") {
116
- const storeIdStr = this.bufferToString(this._storeId, encoding);
103
+ const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
117
104
  let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
118
- if (this._rootHash) {
119
- const rootHashStr = this.bufferToString(this._rootHash, encoding);
105
+ if (this._rootHashHex) {
106
+ const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
120
107
  urn += `:${rootHashStr}`;
121
108
  }
122
109
  if (this.resourceKey) {
@@ -124,12 +111,13 @@ class Udi {
124
111
  }
125
112
  return urn;
126
113
  }
127
- bufferToString(buffer, encoding) {
114
+ formatBufferAsEncoding(hexString, encoding) {
115
+ const buffer = Buffer.from(hexString, "hex");
128
116
  if (encoding === "hex") {
129
- return buffer.toString("hex");
117
+ return hexString;
130
118
  }
131
119
  else if (encoding === "base32") {
132
- return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
120
+ return (0, hi_base32_1.encode)(buffer).replace(/=+$/, "");
133
121
  }
134
122
  else if (encoding === "base64") {
135
123
  return Udi.toBase64UrlSafe(buffer.toString("base64"));
@@ -140,18 +128,18 @@ class Udi {
140
128
  return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
141
129
  }
142
130
  equals(other) {
143
- return (this._storeId.equals(other._storeId) &&
131
+ return (this._storeIdHex === other._storeIdHex &&
144
132
  this.chainName === other.chainName &&
145
- (this._rootHash && other._rootHash
146
- ? this._rootHash.equals(other._rootHash)
147
- : this._rootHash === other._rootHash) &&
133
+ (this._rootHashHex && other._rootHashHex
134
+ ? this._rootHashHex === other._rootHashHex
135
+ : this._rootHashHex === other._rootHashHex) &&
148
136
  this.resourceKey === other.resourceKey);
149
137
  }
150
138
  toString() {
151
139
  return this.toUrn();
152
140
  }
153
141
  clone() {
154
- return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
142
+ return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
155
143
  }
156
144
  hashCode() {
157
145
  const hash = (0, crypto_1.createHash)("sha256");
@@ -159,22 +147,22 @@ class Udi {
159
147
  return hash.digest("hex");
160
148
  }
161
149
  get storeId() {
162
- return this._storeId.toString("hex");
150
+ return this._storeIdHex;
163
151
  }
164
152
  get rootHash() {
165
- return this._rootHash ? this._rootHash.toString("hex") : null;
153
+ return this._rootHashHex;
166
154
  }
167
155
  get storeIdBase32() {
168
- return this.bufferToString(this._storeId, "base32");
156
+ return this.formatBufferAsEncoding(this._storeIdHex, "base32");
169
157
  }
170
158
  get rootHashBase32() {
171
- return this._rootHash ? this.bufferToString(this._rootHash, "base32") : null;
159
+ return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
172
160
  }
173
161
  get storeIdBase64() {
174
- return this.bufferToString(this._storeId, "base64");
162
+ return this.formatBufferAsEncoding(this._storeIdHex, "base64");
175
163
  }
176
164
  get rootHashBase64() {
177
- return this._rootHash ? this.bufferToString(this._rootHash, "base64") : null;
165
+ return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
178
166
  }
179
167
  }
180
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.186",
3
+ "version": "0.0.1-alpha.189",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",