Skip to content

Commit

Permalink
Merge pull request #179 from DIG-Network/release/v0.0.1-alpha.192
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.192
  • Loading branch information
MichaelTaylor3D authored Nov 1, 2024
2 parents 5833179 + a6e665b commit a3d8cce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.192](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.191...v0.0.1-alpha.192) (2024-11-01)


### Features

* add base64 getters to udi class ([1a21acf](https://github.com/DIG-Network/dig-chia-sdk/commit/1a21acf13d7ce39dd37b23a76a0ad4f80f9e5186))

### [0.0.1-alpha.191](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.190...v0.0.1-alpha.191) (2024-11-01)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.191",
"version": "0.0.1-alpha.192",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
61 changes: 23 additions & 38 deletions src/utils/Udi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ class Udi {
}

static convertToHex(input: string): string {
// Attempt hex conversion first
if (/^[a-fA-F0-9]{64}$/.test(input)) return input;

// Convert from Base32
try {
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
Expand All @@ -73,13 +70,11 @@ class Udi {
console.warn("Base32 decoding failed, trying Base64 encoding...");
}

// Convert from Base64
try {
const standardBase64 = Udi.addBase64Padding(Udi.toStandardBase64(input));
const buffer = Buffer.from(standardBase64, "base64");
return buffer.toString("hex");
} catch (e) {
throw new Error("Invalid input encoding. Must be 32-byte hex, Base32, or Base64 string.");
// Attempt hex conversion first
if (/^[a-fA-F0-9]{64}$/.test(input)) {
return input;
} else {
throw new Error("Input must be a 64-character hex string.");
}
}

Expand All @@ -88,21 +83,15 @@ class Udi {
return input + "=".repeat(paddingNeeded);
}

static toStandardBase64(base64UrlSafe: string): string {
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
}

static addBase64Padding(base64: string): string {
const paddingNeeded = (4 - (base64.length % 4)) % 4;
return base64 + "=".repeat(paddingNeeded);
}

toUrn(encoding: "hex" | "base32" | "base64" = "hex"): string {
toUrn(encoding: "hex" | "base32" = "hex"): string {
const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;

if (this._rootHashHex) {
const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
const rootHashStr = this.formatBufferAsEncoding(
this._rootHashHex,
encoding
);
urn += `:${rootHashStr}`;
}

Expand All @@ -113,22 +102,19 @@ class Udi {
return urn;
}

private formatBufferAsEncoding(hexString: string, encoding: "hex" | "base32" | "base64"): string {
private formatBufferAsEncoding(
hexString: string,
encoding: "hex" | "base32"
): string {
const buffer = Buffer.from(hexString, "hex");
if (encoding === "hex") {
return hexString;
} else if (encoding === "base32") {
return base32Encode(buffer).replace(/=+$/, ""); // Strip padding for Base32
} else if (encoding === "base64") {
return Udi.toBase64UrlSafe(buffer.toString("base64")); // Convert to URL-safe 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._storeIdHex === other._storeIdHex &&
Expand All @@ -145,7 +131,12 @@ class Udi {
}

clone(): Udi {
return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
return new Udi(
this.chainName,
this._storeIdHex,
this._rootHashHex,
this.resourceKey
);
}

hashCode(): string {
Expand All @@ -167,15 +158,9 @@ class Udi {
}

get rootHashBase32(): string | null {
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
}

get storeIdBase64(): string {
return this.formatBufferAsEncoding(this._storeIdHex, "base64");
}

get rootHashBase64(): string | null {
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
return this._rootHashHex
? this.formatBufferAsEncoding(this._rootHashHex, "base32")
: null;
}
}

Expand Down

0 comments on commit a3d8cce

Please sign in to comment.