diff --git a/README.md b/README.md index 13a2df9..b3d37e9 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,26 @@ -# ANS-104 Bundles - -**If you want to use Bundlr Network head over to [this repo](https://github.com/Bundlr-Network/js-client)** +# Irys bundles +**If you want to use the Irys Network, head over to [our SDK](https://github.com/Irys-xyz/js-sdk)** A low level library for creating, editing, reading and verifying bundles. -See [ANS-104](https://github.com/ArweaveTeam/arweave-standards/blob/master/ans/ANS-104.md) for more details. - ## Installing the library Using npm: -`npm install arbundles` +`npm install @irys/bundles` Using yarn: -`yarn add arbundles` +`yarn add @irys/bundles` ## Creating bundles ```ts -import { bundleAndSignData, createData } from "arbundles"; +import { bundleAndSignData, createData, EthereumSigner } from "@irys/bundles"; const dataItems = [createData("some data"), createData("some other data")]; -const signer = new ArweaveSigner(jwk); +const signer = new EthereumSigner("privateKey") const bundle = await bundleAndSignData(dataItems, signer); ``` diff --git a/package.json b/package.json index b400acd..6af975a 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.1", + "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", 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/__tests__/filePolygon.spec.ts b/src/__tests__/filePolygon.spec.ts index ec4a932..9a41235 100644 --- a/src/__tests__/filePolygon.spec.ts +++ b/src/__tests__/filePolygon.spec.ts @@ -44,8 +44,6 @@ describe("Polygon signing tests", function () { await d.sign(signer); expect(await d.isValid()).toBe(true); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore expect((await d.rawOwner()).toString("hex")).toEqual(signer.pk); expect(await d.signatureType()).toEqual(3); expect(await d.target()).toEqual("OXcT1sVRSA5eGwt2k6Yuz8-3e3g9WJi5uSE99CWqsBs"); 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