From 1ce70494598562d6da2cc1c5aa654e3d2705ea86 Mon Sep 17 00:00:00 2001 From: JesseTheRobot Date: Tue, 3 Sep 2024 23:17:10 +0000 Subject: [PATCH] feat: change id encoding from base64url to base58, update package.json --- package.json | 12 ++++++------ src/Bundle.ts | 7 ++++--- src/DataItem.ts | 7 ++++--- src/__tests__/dataItem.spec.ts | 3 ++- src/ar-data-bundle.ts | 4 ++-- src/file/FileBundle.ts | 10 +++++----- src/file/FileDataItem.ts | 7 ++++--- src/file/file.ts | 3 ++- src/stream/index.ts | 5 +++-- src/types.ts | 1 + 10 files changed, 33 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index b400acd..8db413b 100755 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "arbundles", - "version": "0.11.1", - "description": "Arweave bundling library", - "author": "Josh Benaron ", + "name": "@irys/bundles", + "version": "0.0.0", + "description": "Transaction bundling library", + "author": "Irys maintainers ", "license": "Apache-2.0", - "repository": "https://github.com/Bundlr-Network/arbundles", + "repository": "https://github.com/Irys-xyz/bundles", "main": "build/node/cjs/index.js", "module": "build/node/esm/index.js", "browser": "build/web/esm/webIndex.js", @@ -152,4 +152,4 @@ "multistream": "^4.1.0", "tmp-promise": "^3.0.2" } -} +} \ No newline at end of file diff --git a/src/Bundle.ts b/src/Bundle.ts index d675da1..30a0559 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -6,6 +6,7 @@ import type { BundleInterface } from "./BundleInterface"; import type { JWKInterface } from "./interface-jwk"; import { createHash } from "crypto"; import type { CreateTransactionInterface, Transaction } from "$/utils"; +import base58 from "bs58"; const HEADER_START = 32; @@ -56,7 +57,7 @@ export class Bundle implements BundleInterface { if (bundleId.length === 0) { throw new Error("Invalid bundle, id specified in headers doesn't exist"); } - ids.push(base64url.encode(bundleId)); + ids.push(base58.encode(bundleId)); } return ids; @@ -68,7 +69,7 @@ export class Bundle implements BundleInterface { } const start = 64 + 64 * index; - return base64url.encode(this.binary.subarray(start, start + 32)); + return base58.encode(this.binary.subarray(start, start + 32)); } public async toTransaction( @@ -85,7 +86,7 @@ export class Bundle implements BundleInterface { public async verify(): Promise { for (const item of this.items) { const valid = await item.isValid(); - const expected = base64url(createHash("sha256").update(item.rawSignature).digest()); + const expected = base58.encode(createHash("sha256").update(item.rawSignature).digest()); if (!(valid && item.id === expected)) { return false; } diff --git a/src/DataItem.ts b/src/DataItem.ts index 4c4ceb4..72e98a0 100644 --- a/src/DataItem.ts +++ b/src/DataItem.ts @@ -10,7 +10,8 @@ import { SIG_CONFIG, SignatureConfig } from "./constants"; import { getCryptoDriver } from "$/utils"; import { deserializeTags } from "./tags"; import { createHash } from "crypto"; -import type { Base64URLString } from "./types"; +import type { Base58String, Base64URLString } from "./types"; +import base58 from "bs58"; export const MIN_BINARY_SIZE = 80; export const MAX_TAG_BYTES = 4096; @@ -39,8 +40,8 @@ export class DataItem implements BundleItem { return DataItem.verify(this.binary); } - get id(): Base64URLString { - return base64url.encode(this.rawId); + get id(): Base58String { + return base58.encode(this.rawId); } set id(id: string) { diff --git a/src/__tests__/dataItem.spec.ts b/src/__tests__/dataItem.spec.ts index 53ae71a..c85c128 100644 --- a/src/__tests__/dataItem.spec.ts +++ b/src/__tests__/dataItem.spec.ts @@ -90,7 +90,8 @@ describe("DataItem", () => { }); describe("given we want to get the id", () => { it("should return the id", async () => { - expect(dataItem.id).toBe("mM5C3u9R1AJp1UL1MUvvLHRo1AGtXYUWi_q0wBCPdfc"); + // expect(dataItem.id).toBe("mM5C3u9R1AJp1UL1MUvvLHRo1AGtXYUWi_q0wBCPdfc"); + expect(dataItem.id).toBe("BHVPXzXznQ8hqa76NcarF9B8LPgSyvzSebTfBBardGcW"); }); }); describe("given we want to get the owner", () => { diff --git a/src/ar-data-bundle.ts b/src/ar-data-bundle.ts index 34292db..50328a1 100644 --- a/src/ar-data-bundle.ts +++ b/src/ar-data-bundle.ts @@ -35,13 +35,13 @@ export async function bundleAndSignData(dataItems: DataItem[], signer: Signer): const binaries = await Promise.all( dataItems.map(async (d, index) => { // Sign DataItem - const id = d.isSigned() ? d.rawId : await sign(d, signer); + const rawId = d.isSigned() ? d.rawId : await sign(d, signer); // Create header array const header = new Uint8Array(64); // Set offset header.set(longTo32ByteArray(d.getRaw().byteLength), 0); // Set id - header.set(id, 32); + header.set(rawId, 32); // Add header to array of headers headers.set(header, 64 * index); // Convert to array for flattening diff --git a/src/file/FileBundle.ts b/src/file/FileBundle.ts index 6ac1284..8bb8caf 100644 --- a/src/file/FileBundle.ts +++ b/src/file/FileBundle.ts @@ -10,11 +10,11 @@ import MultiStream from "multistream"; // import { createTransactionAsync } from 'arweave-stream'; import type { JWKInterface } from "../interface-jwk"; import { promisify } from "util"; -import base64url from "base64url"; import { pipeline } from "stream/promises"; import type { CreateTransactionInterface, Transaction } from "$/utils"; import { resolve } from "path"; +import base58 from "bs58"; // import { Readable } from 'stream'; // import { createTransactionAsync } from 'arweave-stream'; // import { pipeline } from 'stream/promises'; @@ -131,7 +131,7 @@ export class FileBundle implements BundleInterface { for (let i = 32; i < 32 + 64 * (await this.length()); i += 64) { yield { offset: byteArrayToLong(await read(handle.fd, Buffer.allocUnsafe(32), 0, 32, i).then((r) => r.buffer)), - id: await read(handle.fd, Buffer.allocUnsafe(32), 0, 32, i + 32).then((r) => base64url.encode(r.buffer)), + id: await read(handle.fd, Buffer.allocUnsafe(32), 0, 32, i + 32).then((r) => base58.encode(r.buffer)), }; } await handle.close(); @@ -140,7 +140,7 @@ export class FileBundle implements BundleInterface { private async *itemsGenerator(): AsyncGenerator { let counter = 0; for await (const { id } of this.getHeaders()) { - yield new FileDataItem(this.txs[counter], base64url.toBuffer(id)); + yield new FileDataItem(this.txs[counter], base58.decode(id)); counter++; } } @@ -148,7 +148,7 @@ export class FileBundle implements BundleInterface { private async getById(txId: string): Promise { let counter = 0; for await (const { id } of this.getHeaders()) { - if (id === txId) return new FileDataItem(this.txs[counter], base64url.toBuffer(id)); + if (id === txId) return new FileDataItem(this.txs[counter], base58.decode(id)); counter++; } throw new Error("Can't find by id"); @@ -159,7 +159,7 @@ export class FileBundle implements BundleInterface { for await (const { id } of this.getHeaders()) { if (count === index) { - return new FileDataItem(this.txs[count], base64url.toBuffer(id)); + return new FileDataItem(this.txs[count], base58.decode(id)); } count++; } diff --git a/src/file/FileDataItem.ts b/src/file/FileDataItem.ts index fdf1f9f..0aa9c27 100644 --- a/src/file/FileDataItem.ts +++ b/src/file/FileDataItem.ts @@ -12,7 +12,8 @@ import axios from "axios"; import { SIG_CONFIG } from "../constants"; import { promisify } from "util"; import { deserializeTags } from "../tags"; -import type { Base64URLString } from "../types"; +import type { Base58String, Base64URLString } from "../types"; +import base58 from "bs58"; const read = promisify(FSRead); const write = promisify(FSWrite); @@ -39,9 +40,9 @@ export class FileDataItem implements BundleItem { private _id?: Buffer; - get id(): string { + get id(): Base58String { if (!this._id) throw new Error("FileDataItem - ID is undefined"); - return base64url.encode(this._id); + return base58.encode(this._id); } get rawId(): Buffer { diff --git a/src/file/file.ts b/src/file/file.ts index 9e51681..d813762 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -9,6 +9,7 @@ import { streamSigner } from "../stream/index"; import type { Readable } from "stream"; import type { Tag } from "../tags"; import { deserializeTags } from "../tags"; +import base58 from "bs58"; type File = string | FileHandle; const read = promisify(FSRead); @@ -87,7 +88,7 @@ export async function getHeaderAt(file: File, index: number): Promise[]> { const reader = getReader(stream); @@ -22,7 +23,7 @@ export async function processStream(stream: Readable): Promise