@dignetwork/dig-sdk 0.0.1-alpha.181 → 0.0.1-alpha.183

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.
@@ -7,14 +7,12 @@ declare class Udi {
7
7
  static readonly namespace: string;
8
8
  constructor(chainName: string, storeId: string | Buffer, rootHash?: string | Buffer | null, resourceKey?: string | null);
9
9
  static convertToBuffer(input: string | Buffer): Buffer;
10
- static isHex(input: string): boolean;
11
- static isBase32(input: string): boolean;
12
10
  static addBase32Padding(input: string): string;
13
- withRootHash(rootHash: string | Buffer | null): Udi;
14
- withResourceKey(resourceKey: string | null): Udi;
15
- static fromUrn(urn: string): Udi;
16
- toUrn(encoding?: "hex" | "base32"): string;
17
- bufferToString(buffer: Buffer, encoding: "hex" | "base32"): string;
11
+ static toStandardBase64(base64UrlSafe: string): string;
12
+ static addBase64Padding(base64: string): string;
13
+ toUrn(encoding?: "hex" | "base32" | "base64"): string;
14
+ bufferToString(buffer: Buffer, encoding: "hex" | "base32" | "base64"): string;
15
+ static toBase64UrlSafe(base64Standard: string): string;
18
16
  equals(other: Udi): boolean;
19
17
  toString(): string;
20
18
  clone(): Udi;
@@ -23,6 +21,8 @@ declare class Udi {
23
21
  get rootHash(): string | null;
24
22
  get storeIdBase32(): string;
25
23
  get rootHashBase32(): string | null;
24
+ get storeIdBase64(): string;
25
+ get rootHashBase64(): string | null;
26
26
  }
27
27
  export { Udi };
28
28
  //# sourceMappingURL=Udi.d.ts.map
@@ -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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E5B;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
@@ -1,30 +1,6 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.Udi = void 0;
27
- const urns = __importStar(require("urns"));
28
4
  const crypto_1 = require("crypto");
29
5
  const hi_base32_1 = require("hi-base32");
30
6
  class Udi {
@@ -39,55 +15,54 @@ class Udi {
39
15
  }
40
16
  static convertToBuffer(input) {
41
17
  if (Buffer.isBuffer(input)) {
18
+ if (input.length !== 32) {
19
+ throw new Error("Buffer must be exactly 32 bytes.");
20
+ }
42
21
  return input;
43
22
  }
44
- if (Udi.isHex(input)) {
45
- return Buffer.from(input, "hex");
23
+ // Attempt hex decoding
24
+ if (/^[a-fA-F0-9]+$/.test(input) && input.length === 64) {
25
+ try {
26
+ const buffer = Buffer.from(input, "hex");
27
+ if (buffer.length === 32)
28
+ return buffer;
29
+ }
30
+ catch (e) {
31
+ console.warn("Hex decoding failed, trying next encoding...");
32
+ }
46
33
  }
47
- if (Udi.isBase32(input)) {
34
+ // Attempt Base32 decoding
35
+ try {
48
36
  const paddedInput = Udi.addBase32Padding(input.toUpperCase());
49
- return Buffer.from((0, hi_base32_1.decode)(paddedInput, false)); // Decode as UTF-8
37
+ const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
38
+ if (buffer.length === 32)
39
+ return buffer;
50
40
  }
51
- throw new Error("Invalid input encoding. Must be 32-byte hex or Base32 string.");
52
- }
53
- static isHex(input) {
54
- return /^[a-fA-F0-9]{64}$/.test(input);
55
- }
56
- static isBase32(input) {
57
- return /^[a-z2-7]{52}$/.test(input.toLowerCase());
41
+ catch (e) {
42
+ console.warn("Base32 decoding failed, trying Base64 encoding...");
43
+ }
44
+ // Attempt Base64 (URL-safe) decoding
45
+ try {
46
+ const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
47
+ const buffer = Buffer.from(standardBase64, "base64");
48
+ if (buffer.length === 32)
49
+ return buffer;
50
+ }
51
+ catch (e) {
52
+ throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string.");
53
+ }
54
+ throw new Error("Failed to decode input as a 32-byte buffer.");
58
55
  }
59
56
  static addBase32Padding(input) {
60
- // Calculate required padding
61
57
  const paddingNeeded = (8 - (input.length % 8)) % 8;
62
58
  return input + "=".repeat(paddingNeeded);
63
59
  }
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);
60
+ static toStandardBase64(base64UrlSafe) {
61
+ return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
69
62
  }
70
- static fromUrn(urn) {
71
- const parsedUrn = urns.parseURN(urn);
72
- if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
73
- throw new Error(`Invalid nid: ${parsedUrn.nid}`);
74
- }
75
- const parts = parsedUrn.nss.split(":");
76
- if (parts.length < 2) {
77
- throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
78
- }
79
- const chainName = parts[0];
80
- const storeId = parts[1].split("/")[0];
81
- let rootHash = null;
82
- if (parts.length > 2) {
83
- rootHash = parts[2].split("/")[0];
84
- }
85
- const pathParts = parsedUrn.nss.split("/");
86
- let resourceKey = null;
87
- if (pathParts.length > 1) {
88
- resourceKey = pathParts.slice(1).join("/");
89
- }
90
- return new Udi(chainName, storeId, rootHash, resourceKey);
63
+ static addBase64Padding(base64) {
64
+ const paddingNeeded = (4 - (base64.length % 4)) % 4;
65
+ return base64 + "=".repeat(paddingNeeded);
91
66
  }
92
67
  toUrn(encoding = "hex") {
93
68
  const storeIdStr = this.bufferToString(this._storeId, encoding);
@@ -102,9 +77,19 @@ class Udi {
102
77
  return urn;
103
78
  }
104
79
  bufferToString(buffer, encoding) {
105
- return encoding === "hex"
106
- ? buffer.toString("hex")
107
- : (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
80
+ if (encoding === "hex") {
81
+ return buffer.toString("hex");
82
+ }
83
+ else if (encoding === "base32") {
84
+ return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
85
+ }
86
+ else if (encoding === "base64") {
87
+ return Udi.toBase64UrlSafe(buffer.toString("base64"));
88
+ }
89
+ throw new Error("Unsupported encoding type");
90
+ }
91
+ static toBase64UrlSafe(base64Standard) {
92
+ return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
108
93
  }
109
94
  equals(other) {
110
95
  return (this._storeId.equals(other._storeId) &&
@@ -135,9 +120,13 @@ class Udi {
135
120
  return this.bufferToString(this._storeId, "base32");
136
121
  }
137
122
  get rootHashBase32() {
138
- return this._rootHash
139
- ? this.bufferToString(this._rootHash, "base32")
140
- : null;
123
+ return this._rootHash ? this.bufferToString(this._rootHash, "base32") : null;
124
+ }
125
+ get storeIdBase64() {
126
+ return this.bufferToString(this._storeId, "base64");
127
+ }
128
+ get rootHashBase64() {
129
+ return this._rootHash ? this.bufferToString(this._rootHash, "base64") : null;
141
130
  }
142
131
  }
143
132
  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.181",
3
+ "version": "0.0.1-alpha.183",
4
4
  "description": "",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",