@dignetwork/dig-sdk 0.0.1-alpha.182 → 0.0.1-alpha.184
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 +3 -8
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +48 -40
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -7,18 +7,13 @@ 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
|
|
11
|
-
static isBase32(input: string): boolean;
|
|
12
|
-
static isBase64Safe(input: string): boolean;
|
|
10
|
+
static fromUrn(urn: string): Udi;
|
|
13
11
|
static addBase32Padding(input: string): string;
|
|
14
|
-
static toStandardBase64(
|
|
15
|
-
static toBase64Safe(base64Standard: string): string;
|
|
12
|
+
static toStandardBase64(base64UrlSafe: string): string;
|
|
16
13
|
static addBase64Padding(base64: string): string;
|
|
17
|
-
withRootHash(rootHash: string | Buffer | null): Udi;
|
|
18
|
-
withResourceKey(resourceKey: string | null): Udi;
|
|
19
|
-
static fromUrn(urn: string): Udi;
|
|
20
14
|
toUrn(encoding?: "hex" | "base32" | "base64"): string;
|
|
21
15
|
bufferToString(buffer: Buffer, encoding: "hex" | "base32" | "base64"): string;
|
|
16
|
+
static toBase64UrlSafe(base64Standard: string): string;
|
|
22
17
|
equals(other: Udi): boolean;
|
|
23
18
|
toString(): string;
|
|
24
19
|
clone(): Udi;
|
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":"AAIA,cAAM,GAAG;IACP,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,OAAO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CAAC,gBAAgB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAItD,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAK/C,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;IAW7E,MAAM,CAAC,eAAe,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM;IAItD,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAW3B,QAAQ,IAAI,MAAM;IAIlB,KAAK,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
|
@@ -39,64 +39,58 @@ class Udi {
|
|
|
39
39
|
}
|
|
40
40
|
static convertToBuffer(input) {
|
|
41
41
|
if (Buffer.isBuffer(input)) {
|
|
42
|
+
if (input.length !== 32) {
|
|
43
|
+
throw new Error("Buffer must be exactly 32 bytes.");
|
|
44
|
+
}
|
|
42
45
|
return input;
|
|
43
46
|
}
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
}
|
|
46
57
|
}
|
|
47
|
-
|
|
58
|
+
// Attempt Base32 decoding
|
|
59
|
+
try {
|
|
48
60
|
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
|
|
49
|
-
|
|
61
|
+
const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
|
|
62
|
+
if (buffer.length === 32)
|
|
63
|
+
return buffer;
|
|
50
64
|
}
|
|
51
|
-
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.warn("Base32 decoding failed, trying Base64 encoding...");
|
|
67
|
+
}
|
|
68
|
+
// Attempt Base64 (URL-safe) decoding
|
|
69
|
+
try {
|
|
52
70
|
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
|
|
53
|
-
|
|
71
|
+
const buffer = Buffer.from(standardBase64, "base64");
|
|
72
|
+
if (buffer.length === 32)
|
|
73
|
+
return buffer;
|
|
54
74
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
static isBase32(input) {
|
|
61
|
-
return /^[a-z2-7]{52}$/.test(input.toLowerCase());
|
|
62
|
-
}
|
|
63
|
-
static isBase64Safe(input) {
|
|
64
|
-
return /^[A-Za-z0-9\-_]+$/.test(input);
|
|
65
|
-
}
|
|
66
|
-
static addBase32Padding(input) {
|
|
67
|
-
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
68
|
-
return input + "=".repeat(paddingNeeded);
|
|
69
|
-
}
|
|
70
|
-
static toStandardBase64(base64Safe) {
|
|
71
|
-
return base64Safe.replace(/-/g, "+").replace(/_/g, "/");
|
|
72
|
-
}
|
|
73
|
-
static toBase64Safe(base64Standard) {
|
|
74
|
-
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
75
|
-
}
|
|
76
|
-
static addBase64Padding(base64) {
|
|
77
|
-
const paddingNeeded = (4 - (base64.length % 4)) % 4;
|
|
78
|
-
return base64 + "=".repeat(paddingNeeded);
|
|
79
|
-
}
|
|
80
|
-
withRootHash(rootHash) {
|
|
81
|
-
return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey);
|
|
82
|
-
}
|
|
83
|
-
withResourceKey(resourceKey) {
|
|
84
|
-
return new Udi(this.chainName, this._storeId, this._rootHash, resourceKey);
|
|
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.");
|
|
85
79
|
}
|
|
86
80
|
static fromUrn(urn) {
|
|
87
81
|
const parsedUrn = urns.parseURN(urn);
|
|
88
82
|
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
89
|
-
throw new Error(`Invalid
|
|
83
|
+
throw new Error(`Invalid nid: ${parsedUrn.nid}`);
|
|
90
84
|
}
|
|
91
85
|
const parts = parsedUrn.nss.split(":");
|
|
92
86
|
if (parts.length < 2) {
|
|
93
87
|
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
94
88
|
}
|
|
95
89
|
const chainName = parts[0];
|
|
96
|
-
const storeId = parts[1].split("/")[0];
|
|
90
|
+
const storeId = Udi.convertToBuffer(parts[1].split("/")[0]);
|
|
97
91
|
let rootHash = null;
|
|
98
92
|
if (parts.length > 2) {
|
|
99
|
-
rootHash = parts[2].split("/")[0];
|
|
93
|
+
rootHash = Udi.convertToBuffer(parts[2].split("/")[0]);
|
|
100
94
|
}
|
|
101
95
|
const pathParts = parsedUrn.nss.split("/");
|
|
102
96
|
let resourceKey = null;
|
|
@@ -105,6 +99,17 @@ class Udi {
|
|
|
105
99
|
}
|
|
106
100
|
return new Udi(chainName, storeId, rootHash, resourceKey);
|
|
107
101
|
}
|
|
102
|
+
static addBase32Padding(input) {
|
|
103
|
+
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
104
|
+
return input + "=".repeat(paddingNeeded);
|
|
105
|
+
}
|
|
106
|
+
static toStandardBase64(base64UrlSafe) {
|
|
107
|
+
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
108
|
+
}
|
|
109
|
+
static addBase64Padding(base64) {
|
|
110
|
+
const paddingNeeded = (4 - (base64.length % 4)) % 4;
|
|
111
|
+
return base64 + "=".repeat(paddingNeeded);
|
|
112
|
+
}
|
|
108
113
|
toUrn(encoding = "hex") {
|
|
109
114
|
const storeIdStr = this.bufferToString(this._storeId, encoding);
|
|
110
115
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
@@ -125,10 +130,13 @@ class Udi {
|
|
|
125
130
|
return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
126
131
|
}
|
|
127
132
|
else if (encoding === "base64") {
|
|
128
|
-
return Udi.
|
|
133
|
+
return Udi.toBase64UrlSafe(buffer.toString("base64"));
|
|
129
134
|
}
|
|
130
135
|
throw new Error("Unsupported encoding type");
|
|
131
136
|
}
|
|
137
|
+
static toBase64UrlSafe(base64Standard) {
|
|
138
|
+
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
139
|
+
}
|
|
132
140
|
equals(other) {
|
|
133
141
|
return (this._storeId.equals(other._storeId) &&
|
|
134
142
|
this.chainName === other.chainName &&
|