Skip to content

Commit a3d8cce

Browse files
Merge pull request #179 from DIG-Network/release/v0.0.1-alpha.192
Release/v0.0.1 alpha.192
2 parents 5833179 + a6e665b commit a3d8cce

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
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.
44

5+
### [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)
6+
7+
8+
### Features
9+
10+
* add base64 getters to udi class ([1a21acf](https://github.com/DIG-Network/dig-chia-sdk/commit/1a21acf13d7ce39dd37b23a76a0ad4f80f9e5186))
11+
512
### [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)
613

714

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dignetwork/dig-sdk",
3-
"version": "0.0.1-alpha.191",
3+
"version": "0.0.1-alpha.192",
44
"description": "",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/utils/Udi.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ class Udi {
6161
}
6262

6363
static convertToHex(input: string): string {
64-
// Attempt hex conversion first
65-
if (/^[a-fA-F0-9]{64}$/.test(input)) return input;
66-
6764
// Convert from Base32
6865
try {
6966
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
@@ -73,13 +70,11 @@ class Udi {
7370
console.warn("Base32 decoding failed, trying Base64 encoding...");
7471
}
7572

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

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

91-
static toStandardBase64(base64UrlSafe: string): string {
92-
return base64UrlSafe.replace(/-/g, "+").replace(/_/g, "/");
93-
}
94-
95-
static addBase64Padding(base64: string): string {
96-
const paddingNeeded = (4 - (base64.length % 4)) % 4;
97-
return base64 + "=".repeat(paddingNeeded);
98-
}
99-
100-
toUrn(encoding: "hex" | "base32" | "base64" = "hex"): string {
86+
toUrn(encoding: "hex" | "base32" = "hex"): string {
10187
const storeIdStr = this.formatBufferAsEncoding(this._storeIdHex, encoding);
10288
let urn = `${Udi.namespace}:${this.chainName}:${storeIdStr}`;
10389

10490
if (this._rootHashHex) {
105-
const rootHashStr = this.formatBufferAsEncoding(this._rootHashHex, encoding);
91+
const rootHashStr = this.formatBufferAsEncoding(
92+
this._rootHashHex,
93+
encoding
94+
);
10695
urn += `:${rootHashStr}`;
10796
}
10897

@@ -113,22 +102,19 @@ class Udi {
113102
return urn;
114103
}
115104

116-
private formatBufferAsEncoding(hexString: string, encoding: "hex" | "base32" | "base64"): string {
105+
private formatBufferAsEncoding(
106+
hexString: string,
107+
encoding: "hex" | "base32"
108+
): string {
117109
const buffer = Buffer.from(hexString, "hex");
118110
if (encoding === "hex") {
119111
return hexString;
120112
} else if (encoding === "base32") {
121113
return base32Encode(buffer).replace(/=+$/, ""); // Strip padding for Base32
122-
} else if (encoding === "base64") {
123-
return Udi.toBase64UrlSafe(buffer.toString("base64")); // Convert to URL-safe Base64
124114
}
125115
throw new Error("Unsupported encoding type");
126116
}
127117

128-
static toBase64UrlSafe(base64Standard: string): string {
129-
return base64Standard.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
130-
}
131-
132118
equals(other: Udi): boolean {
133119
return (
134120
this._storeIdHex === other._storeIdHex &&
@@ -145,7 +131,12 @@ class Udi {
145131
}
146132

147133
clone(): Udi {
148-
return new Udi(this.chainName, this._storeIdHex, this._rootHashHex, this.resourceKey);
134+
return new Udi(
135+
this.chainName,
136+
this._storeIdHex,
137+
this._rootHashHex,
138+
this.resourceKey
139+
);
149140
}
150141

151142
hashCode(): string {
@@ -167,15 +158,9 @@ class Udi {
167158
}
168159

169160
get rootHashBase32(): string | null {
170-
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base32") : null;
171-
}
172-
173-
get storeIdBase64(): string {
174-
return this.formatBufferAsEncoding(this._storeIdHex, "base64");
175-
}
176-
177-
get rootHashBase64(): string | null {
178-
return this._rootHashHex ? this.formatBufferAsEncoding(this._rootHashHex, "base64") : null;
161+
return this._rootHashHex
162+
? this.formatBufferAsEncoding(this._rootHashHex, "base32")
163+
: null;
179164
}
180165
}
181166

0 commit comments

Comments
 (0)