Skip to content

Commit

Permalink
Merge pull request #169 from DIG-Network/release/v0.0.1-alpha.181
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.181
  • Loading branch information
MichaelTaylor3D authored Oct 31, 2024
2 parents 929d71b + 6f5e03e commit 4f8a712
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 43 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.181](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.180...v0.0.1-alpha.181) (2024-10-31)


### Features

* add base32 getters to udi class ([8b5d18f](https://github.com/DIG-Network/dig-chia-sdk/commit/8b5d18fcbb8ca07100978f19ae476c41bbd419a6))

### [0.0.1-alpha.180](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.179...v0.0.1-alpha.180) (2024-10-31)


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.180",
"version": "0.0.1-alpha.181",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
88 changes: 48 additions & 40 deletions src/utils/Udi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import * as urns from 'urns';
import { createHash } from 'crypto';
import { encode as base32Encode, decode as base32Decode } from 'hi-base32';

//
// This class encapsulates the concept of a Universal Data Identifier (UDI), which is a
// standardized way to identify resources across the distributed DIG mesh network.
// The UDI format: urn:dig:chainName:storeId:rootHash/resourceKey
// This allows unique resource identification across the DIG network.
//
import * as urns from "urns";
import { createHash } from "crypto";
import { encode as base32Encode, decode as base32Decode } from "hi-base32";

class Udi {
readonly chainName: string;
private readonly _storeId: Buffer;
Expand Down Expand Up @@ -37,14 +31,17 @@ class Udi {
}

if (Udi.isHex(input)) {
return Buffer.from(input, 'hex');
return Buffer.from(input, "hex");
}

if (Udi.isBase32(input)) {
return Buffer.from(base32Decode(input, false)); // Decode as UTF-8
const paddedInput = Udi.addBase32Padding(input.toUpperCase());
return Buffer.from(base32Decode(paddedInput, false)); // Decode as UTF-8
}

throw new Error("Invalid input encoding. Must be 32-byte hex or Base32 string.");
throw new Error(
"Invalid input encoding. Must be 32-byte hex or Base32 string."
);
}

static isHex(input: string): boolean {
Expand All @@ -55,6 +52,12 @@ class Udi {
return /^[a-z2-7]{52}$/.test(input.toLowerCase());
}

static addBase32Padding(input: string): string {
// Calculate required padding
const paddingNeeded = (8 - (input.length % 8)) % 8;
return input + "=".repeat(paddingNeeded);
}

withRootHash(rootHash: string | Buffer | null): Udi {
return new Udi(this.chainName, this._storeId, rootHash, this.resourceKey);
}
Expand All @@ -69,29 +72,29 @@ class Udi {
throw new Error(`Invalid nid: ${parsedUrn.nid}`);
}

const parts = parsedUrn.nss.split(':');
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];
const storeId = parts[1].split("/")[0];

let rootHash: string | null = null;
if (parts.length > 2) {
rootHash = parts[2].split('/')[0];
rootHash = parts[2].split("/")[0];
}

const pathParts = parsedUrn.nss.split('/');
const pathParts = parsedUrn.nss.split("/");
let resourceKey: string | null = null;
if (pathParts.length > 1) {
resourceKey = pathParts.slice(1).join('/');
resourceKey = pathParts.slice(1).join("/");
}

return new Udi(chainName, storeId, rootHash, resourceKey);
}

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

Expand All @@ -107,17 +110,19 @@ class Udi {
return urn;
}

bufferToString(buffer: Buffer, encoding: 'hex' | 'base32'): string {
return encoding === 'hex'
? buffer.toString('hex')
: base32Encode(buffer).toLowerCase().replace(/=+$/, '');
bufferToString(buffer: Buffer, encoding: "hex" | "base32"): string {
return encoding === "hex"
? buffer.toString("hex")
: base32Encode(buffer).toLowerCase().replace(/=+$/, "");
}

equals(other: Udi): boolean {
return (
this._storeId.equals(other._storeId) &&
this.chainName === other.chainName &&
(this._rootHash && other._rootHash ? this._rootHash.equals(other._rootHash) : this._rootHash === other._rootHash) &&
(this._rootHash && other._rootHash
? this._rootHash.equals(other._rootHash)
: this._rootHash === other._rootHash) &&
this.resourceKey === other.resourceKey
);
}
Expand All @@ -127,34 +132,37 @@ class Udi {
}

clone(): Udi {
return new Udi(this.chainName, this._storeId, this._rootHash, this.resourceKey);
return new Udi(
this.chainName,
this._storeId,
this._rootHash,
this.resourceKey
);
}

hashCode(): string {
const hash = createHash('sha256');
const hash = createHash("sha256");
hash.update(this.toUrn());
return hash.digest('hex');
return hash.digest("hex");
}

// Getter for storeId as a hex string
get storeId(): string {
return this._storeId.toString('hex');
return this._storeId.toString("hex");
}

// Getter for rootHash as a hex string
get rootHash(): string | null {
return this._rootHash ? this._rootHash.toString('hex') : null;
return this._rootHash ? this._rootHash.toString("hex") : null;
}

// Getter for storeId as a hex string
get storeIdBase32(): string {
return this.bufferToString(this._storeId, 'base32') ;
}

// Getter for rootHash as a hex string
get rootHashBase32(): string | null {
return this._rootHash ? this.bufferToString(this._rootHash, 'base32') : null;
}
get storeIdBase32(): string {
return this.bufferToString(this._storeId, "base32");
}

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

export { Udi };

0 comments on commit 4f8a712

Please sign in to comment.