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

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,25 @@
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
+ toUrn(encoding?: "hex" | "base32"): string;
14
+ private formatBufferAsEncoding;
17
15
  equals(other: Udi): boolean;
18
16
  toString(): string;
19
17
  clone(): Udi;
20
18
  hashCode(): string;
21
19
  get storeId(): string;
22
20
  get rootHash(): string | null;
21
+ get storeIdBase32(): string;
22
+ get rootHashBase32(): string | null;
23
23
  }
24
24
  export { Udi };
25
25
  //# sourceMappingURL=Udi.d.ts.map
@@ -1 +1 @@
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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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');
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.");
52
43
  }
53
- if (Udi.isBase32(input)) {
54
- return Buffer.from((0, hi_base32_1.decode)(input.toUpperCase(), false)); // Decode with uppercase
55
- }
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("/");
65
+ }
66
+ return new Udi(chainName, storeIdHex, rootHashHex, resourceKey);
67
+ }
68
+ static convertToHex(input) {
69
+ // Convert from Base32
70
+ try {
71
+ const paddedInput = Udi.addBase32Padding(input.toUpperCase());
72
+ const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
73
+ return buffer.toString("hex");
74
+ }
75
+ catch (e) {
76
+ console.warn("Base32 decoding failed, trying Base64 encoding...");
77
+ }
78
+ // Attempt hex conversion first
79
+ if (/^[a-fA-F0-9]{64}$/.test(input)) {
80
+ return input;
81
+ }
82
+ else {
83
+ throw new Error("Input must be a 64-character hex string.");
89
84
  }
90
- return new Udi(chainName, storeId, rootHash, resourceKey);
91
85
  }
92
- toUrn(encoding = 'hex') {
93
- const storeIdStr = this.bufferToString(this._storeId, encoding);
86
+ static addBase32Padding(input) {
87
+ const paddingNeeded = (8 - (input.length % 8)) % 8;
88
+ return input + "=".repeat(paddingNeeded);
89
+ }
90
+ toUrn(encoding = "hex") {
91
+ const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
94
92
  let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
95
- if (this._rootHash) {
96
- const rootHashStr = this.bufferToString(this._rootHash, encoding);
93
+ if (this._rootHashHex) {
94
+ const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
97
95
  urn += `:${rootHashStr}`;
98
96
  }
99
97
  if (this.resourceKey) {
@@ -101,42 +99,48 @@ class Udi {
101
99
  }
102
100
  return urn;
103
101
  }
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");
102
+ formatBufferAsEncoding(hexString, encoding) {
103
+ const buffer = Buffer.from(hexString, "hex");
104
+ if (encoding === "hex") {
105
+ return hexString;
106
+ }
107
+ else if (encoding === "base32") {
108
+ return (0, hi_base32_1.encode)(buffer).replace(/=+$/, ""); // Strip padding for Base32
114
109
  }
110
+ throw new Error("Unsupported encoding type");
115
111
  }
116
112
  equals(other) {
117
- return (this._storeId.equals(other._storeId) &&
113
+ return (this._storeIdHex === other._storeIdHex &&
118
114
  this.chainName === other.chainName &&
119
- (this._rootHash && other._rootHash ? this._rootHash.equals(other._rootHash) : this._rootHash === other._rootHash) &&
115
+ (this._rootHashHex && other._rootHashHex
116
+ ? this._rootHashHex === other._rootHashHex
117
+ : this._rootHashHex === other._rootHashHex) &&
120
118
  this.resourceKey === other.resourceKey);
121
119
  }
122
120
  toString() {
123
121
  return this.toUrn();
124
122
  }
125
123
  clone() {
126
- return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
124
+ return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
127
125
  }
128
126
  hashCode() {
129
- const hash = (0, crypto_1.createHash)('sha256');
127
+ const hash = (0, crypto_1.createHash)("sha256");
130
128
  hash.update(this.toUrn());
131
- return hash.digest('hex');
129
+ return hash.digest("hex");
132
130
  }
133
- // Getter for storeId as a hex string
134
131
  get storeId() {
135
- return this._storeId.toString('hex');
132
+ return this._storeIdHex;
136
133
  }
137
- // Getter for rootHash as a hex string
138
134
  get rootHash() {
139
- return this._rootHash ? this._rootHash.toString('hex') : null;
135
+ return this._rootHashHex;
136
+ }
137
+ get storeIdBase32() {
138
+ return this.formatBufferAsEncoding(this._storeIdHex, "base32");
139
+ }
140
+ get rootHashBase32() {
141
+ return this._rootHashHex
142
+ ? this.formatBufferAsEncoding(this._rootHashHex, "base32")
143
+ : null;
140
144
  }
141
145
  }
142
146
  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.192",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",