@dignetwork/dig-sdk 0.0.1-alpha.182 → 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.
- package/dist/utils/Udi.d.ts +2 -8
- package/dist/utils/Udi.d.ts.map +1 -1
- package/dist/utils/Udi.js +36 -74
- package/package.json +1 -1
package/dist/utils/Udi.d.ts
CHANGED
|
@@ -7,18 +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
|
-
static isBase64Safe(input: string): boolean;
|
|
13
10
|
static addBase32Padding(input: string): string;
|
|
14
|
-
static toStandardBase64(
|
|
15
|
-
static toBase64Safe(base64Standard: string): string;
|
|
11
|
+
static toStandardBase64(base64UrlSafe: string): string;
|
|
16
12
|
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
13
|
toUrn(encoding?: "hex" | "base32" | "base64"): string;
|
|
21
14
|
bufferToString(buffer: Buffer, encoding: "hex" | "base32" | "base64"): string;
|
|
15
|
+
static toBase64UrlSafe(base64Standard: string): string;
|
|
22
16
|
equals(other: Udi): boolean;
|
|
23
17
|
toString(): string;
|
|
24
18
|
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,CAAC,QAAQ,CAA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C,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
|
@@ -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,72 +15,55 @@ 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
|
-
|
|
45
|
-
|
|
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
|
-
|
|
34
|
+
// Attempt Base32 decoding
|
|
35
|
+
try {
|
|
48
36
|
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
|
|
49
|
-
|
|
37
|
+
const buffer = Buffer.from((0, hi_base32_1.decode)(paddedInput, false));
|
|
38
|
+
if (buffer.length === 32)
|
|
39
|
+
return buffer;
|
|
50
40
|
}
|
|
51
|
-
|
|
41
|
+
catch (e) {
|
|
42
|
+
console.warn("Base32 decoding failed, trying Base64 encoding...");
|
|
43
|
+
}
|
|
44
|
+
// Attempt Base64 (URL-safe) decoding
|
|
45
|
+
try {
|
|
52
46
|
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
|
|
53
|
-
|
|
47
|
+
const buffer = Buffer.from(standardBase64, "base64");
|
|
48
|
+
if (buffer.length === 32)
|
|
49
|
+
return buffer;
|
|
54
50
|
}
|
|
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);
|
|
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.");
|
|
65
55
|
}
|
|
66
56
|
static addBase32Padding(input) {
|
|
67
57
|
const paddingNeeded = (8 - (input.length % 8)) % 8;
|
|
68
58
|
return input + "=".repeat(paddingNeeded);
|
|
69
59
|
}
|
|
70
|
-
static toStandardBase64(
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
static toBase64Safe(base64Standard) {
|
|
74
|
-
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
60
|
+
static toStandardBase64(base64UrlSafe) {
|
|
61
|
+
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
75
62
|
}
|
|
76
63
|
static addBase64Padding(base64) {
|
|
77
64
|
const paddingNeeded = (4 - (base64.length % 4)) % 4;
|
|
78
65
|
return base64 + "=".repeat(paddingNeeded);
|
|
79
66
|
}
|
|
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);
|
|
85
|
-
}
|
|
86
|
-
static fromUrn(urn) {
|
|
87
|
-
const parsedUrn = urns.parseURN(urn);
|
|
88
|
-
if (parsedUrn.nid.toLowerCase() !== Udi.nid) {
|
|
89
|
-
throw new Error(`Invalid UDI: ${parsedUrn.nid}`);
|
|
90
|
-
}
|
|
91
|
-
const parts = parsedUrn.nss.split(":");
|
|
92
|
-
if (parts.length < 2) {
|
|
93
|
-
throw new Error(`Invalid UDI format: ${parsedUrn.nss}`);
|
|
94
|
-
}
|
|
95
|
-
const chainName = parts[0];
|
|
96
|
-
const storeId = parts[1].split("/")[0];
|
|
97
|
-
let rootHash = null;
|
|
98
|
-
if (parts.length > 2) {
|
|
99
|
-
rootHash = parts[2].split("/")[0];
|
|
100
|
-
}
|
|
101
|
-
const pathParts = parsedUrn.nss.split("/");
|
|
102
|
-
let resourceKey = null;
|
|
103
|
-
if (pathParts.length > 1) {
|
|
104
|
-
resourceKey = pathParts.slice(1).join("/");
|
|
105
|
-
}
|
|
106
|
-
return new Udi(chainName, storeId, rootHash, resourceKey);
|
|
107
|
-
}
|
|
108
67
|
toUrn(encoding = "hex") {
|
|
109
68
|
const storeIdStr = this.bufferToString(this._storeId, encoding);
|
|
110
69
|
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
|
|
@@ -125,10 +84,13 @@ class Udi {
|
|
|
125
84
|
return (0, hi_base32_1.encode)(buffer).toLowerCase().replace(/=+$/, "");
|
|
126
85
|
}
|
|
127
86
|
else if (encoding === "base64") {
|
|
128
|
-
return Udi.
|
|
87
|
+
return Udi.toBase64UrlSafe(buffer.toString("base64"));
|
|
129
88
|
}
|
|
130
89
|
throw new Error("Unsupported encoding type");
|
|
131
90
|
}
|
|
91
|
+
static toBase64UrlSafe(base64Standard) {
|
|
92
|
+
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
93
|
+
}
|
|
132
94
|
equals(other) {
|
|
133
95
|
return (this._storeId.equals(other._storeId) &&
|
|
134
96
|
this.chainName === other.chainName &&
|