Skip to content

Commit

Permalink
feat: change id encoding from base64url to base58, update package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTheRobot committed Sep 3, 2024
1 parent b35f492 commit 1ce7049
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 26 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "arbundles",
"version": "0.11.1",
"description": "Arweave bundling library",
"author": "Josh Benaron <joshbenaron@gmail.com>",
"name": "@irys/bundles",
"version": "0.0.0",
"description": "Transaction bundling library",
"author": "Irys maintainers <hello@irys.xyz>",
"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",
Expand Down Expand Up @@ -152,4 +152,4 @@
"multistream": "^4.1.0",
"tmp-promise": "^3.0.2"
}
}
}
7 changes: 4 additions & 3 deletions src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -85,7 +86,7 @@ export class Bundle implements BundleInterface {
public async verify(): Promise<boolean> {
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;
}
Expand Down
7 changes: 4 additions & 3 deletions src/DataItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/dataItem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/ar-data-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/file/FileBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -140,15 +140,15 @@ export class FileBundle implements BundleInterface {
private async *itemsGenerator(): AsyncGenerator<FileDataItem> {
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++;
}
}

private async getById(txId: string): Promise<FileDataItem> {
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");
Expand All @@ -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++;
}
Expand Down
7 changes: 4 additions & 3 deletions src/file/FileDataItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function getHeaderAt(file: File, index: number): Promise<DataItemHe
await fd.close();
return {
offset: byteArrayToLong(headerBuffer.subarray(0, 32)),
id: base64url.encode(headerBuffer.subarray(32, 64)),
id: base58.encode(headerBuffer.subarray(32, 64)),
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { deepHash } from "../deepHash";
import type { Signer } from "../signing/index";
import { deserializeTags } from "../tags";
import { createHash } from "crypto";
import base58 from "bs58";

export async function processStream(stream: Readable): Promise<Record<string, any>[]> {
const reader = getReader(stream);
Expand All @@ -22,7 +23,7 @@ export async function processStream(stream: Readable): Promise<Record<string, an
bytes = await readBytes(reader, bytes, headersLength);
const headers: [number, string][] = new Array(itemCount);
for (let i = 0; i < headersLength; i += 64) {
headers[i / 64] = [byteArrayToLong(bytes.subarray(i, i + 32)), base64url(Buffer.from(bytes.subarray(i + 32, i + 64)))];
headers[i / 64] = [byteArrayToLong(bytes.subarray(i, i + 32)), base58.encode(Buffer.from(bytes.subarray(i + 32, i + 64)))];
}

bytes = bytes.subarray(headersLength);
Expand Down Expand Up @@ -125,7 +126,7 @@ export async function processStream(stream: Readable): Promise<Record<string, an
transform.end();

// Check id
if (id !== base64url(createHash("sha256").update(signature).digest())) throw new Error("ID doesn't match signature");
if (id !== base58.encode(createHash("sha256").update(signature).digest())) throw new Error("ID doesn't match signature");

const Signer = indexToType[signatureType];

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type Base64URLString = string;
export type Base58String = string;

Check failure on line 2 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Insert `⏎`

Check failure on line 2 in src/types.ts

View workflow job for this annotation

GitHub Actions / test

Insert `⏎`

0 comments on commit 1ce7049

Please sign in to comment.