From 9b55e84a446894c11f2489043dd51d1e4ff73a9c Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 31 Oct 2024 18:29:35 -0400 Subject: [PATCH 1/2] feat: add base64 getters to udi class --- src/utils/Udi.ts | 93 ++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/src/utils/Udi.ts b/src/utils/Udi.ts index 00abae6..d36e3cf 100644 --- a/src/utils/Udi.ts +++ b/src/utils/Udi.ts @@ -27,36 +27,41 @@ class Udi { static convertToBuffer(input: string | Buffer): Buffer { if (Buffer.isBuffer(input)) { + if (input.length !== 32) { + throw new Error("Buffer must be exactly 32 bytes."); + } return input; } - if (Udi.isHex(input)) { - return Buffer.from(input, "hex"); + // Attempt hex decoding + if (/^[a-fA-F0-9]+$/.test(input) && input.length === 64) { + try { + const buffer = Buffer.from(input, "hex"); + if (buffer.length === 32) return buffer; + } catch (e) { + console.warn("Hex decoding failed, trying next encoding..."); + } } - if (Udi.isBase32(input)) { + // Attempt Base32 decoding + try { const paddedInput = Udi.addBase32Padding(input.toUpperCase()); - return Buffer.from(base32Decode(paddedInput, false)); + const buffer = Buffer.from(base32Decode(paddedInput, false)); + if (buffer.length === 32) return buffer; + } catch (e) { + console.warn("Base32 decoding failed, trying Base64 encoding..."); } - if (Udi.isBase64Safe(input)) { + // Attempt Base64 (URL-safe) decoding + try { const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input)); - return Buffer.from(standardBase64, "base64"); + const buffer = Buffer.from(standardBase64, "base64"); + if (buffer.length === 32) return buffer; + } catch (e) { + throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string."); } - throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 URL-safe string."); - } - - static isHex(input: string): boolean { - return /^[a-fA-F0-9]{64}$/.test(input); - } - - static isBase32(input: string): boolean { - return /^[a-z2-7]{52}$/.test(input.toLowerCase()); - } - - static isBase64Safe(input: string): boolean { - return /^[A-Za-z0-9\-_]+$/.test(input); + throw new Error("Failed to decode input as a 32-byte buffer."); } static addBase32Padding(input: string): string { @@ -64,12 +69,8 @@ class Udi { return input + "=".repeat(paddingNeeded); } - static toStandardBase64(base64Safe: string): string { - return base64Safe.replace(/-/g, "+").replace(/_/g, "/"); - } - - static toBase64Safe(base64Standard: string): string { - return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); + static toStandardBase64(base64UrlSafe: string): string { + return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/"); } static addBase64Padding(base64: string): string { @@ -77,42 +78,6 @@ class Udi { return base64 + "=".repeat(paddingNeeded); } - withRootHash(rootHash: string | Buffer | null): Udi { - return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey); - } - - withResourceKey(resourceKey: string | null): Udi { - return new Udi(this.chainName, this._storeId, this._rootHash, resourceKey); - } - - static fromUrn(urn: string): Udi { - const parsedUrn = urns.parseURN(urn); - if (parsedUrn.nid.toLowerCase() !== Udi.nid) { - throw new Error(`Invalid UDI: ${parsedUrn.nid}`); - } - - const parts = parsedUrn.nss.split(":"); - if (parts.length < 2) { - throw new Error(`Invalid UDI format: ${parsedUrn.nss}`); - } - - const chainName = parts[0]; - const storeId = parts[1].split("/")[0]; - - let rootHash: string | null = null; - if (parts.length > 2) { - rootHash = parts[2].split("/")[0]; - } - - const pathParts = parsedUrn.nss.split("/"); - let resourceKey: string | null = null; - if (pathParts.length > 1) { - resourceKey = pathParts.slice(1).join("/"); - } - - return new Udi(chainName, storeId, rootHash, resourceKey); - } - toUrn(encoding: "hex" | "base32" | "base64" = "hex"): string { const storeIdStr = this.bufferToString(this._storeId, encoding); let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`; @@ -135,11 +100,15 @@ class Udi { } else if (encoding === "base32") { return base32Encode(buffer).toLowerCase().replace(/=+$/, ""); } else if (encoding === "base64") { - return Udi.toBase64Safe(buffer.toString("base64")); + return Udi.toBase64UrlSafe(buffer.toString("base64")); } throw new Error("Unsupported encoding type"); } + static toBase64UrlSafe(base64Standard: string): string { + return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); + } + equals(other: Udi): boolean { return ( this._storeId.equals(other._storeId) && From a69372784a7533ae010313052610df66832cf3d5 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 31 Oct 2024 18:30:08 -0400 Subject: [PATCH 2/2] chore(release): 0.0.1-alpha.183 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d3727e..8a68fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [0.0.1-alpha.183](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.182...v0.0.1-alpha.183) (2024-10-31) + + +### Features + +* add base64 getters to udi class ([9b55e84](https://github.com/DIG-Network/dig-chia-sdk/commit/9b55e84a446894c11f2489043dd51d1e4ff73a9c)) + ### [0.0.1-alpha.182](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.181...v0.0.1-alpha.182) (2024-10-31) diff --git a/package-lock.json b/package-lock.json index 26cde2a..44d6a35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.182", + "version": "0.0.1-alpha.183", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.182", + "version": "0.0.1-alpha.183", "license": "ISC", "dependencies": { "@dignetwork/datalayer-driver": "^0.1.29", diff --git a/package.json b/package.json index 28cb44c..b553ff8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dignetwork/dig-sdk", - "version": "0.0.1-alpha.182", + "version": "0.0.1-alpha.183", "description": "", "type": "commonjs", "main": "./dist/index.js",