Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #79 from Irys-xyz/fix/anchor-encoding
Browse files Browse the repository at this point in the history
Fix/anchor encoding
  • Loading branch information
JesseTheRobot authored Jan 16, 2024
2 parents 1424542 + 5c82191 commit 5db54ee
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 23 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
}
},
"files": [
"./build/**/*"
"./build/**/*",
"./src/**/*"
],
"scripts": {
"build": "rm -rf ./build && concurrently \"tsc -p tsconfig.json && resolve-tspaths -p tsconfig.json && sh ./fix-pkg.sh node/esm module && yarn tsc-esm-fix --tsconfig tsconfig.json \" \"tsc -p cjs.tsconfig.json && resolve-tspaths -p cjs.tsconfig.json && sh ./fix-pkg.sh node/cjs commonjs \" \"tsc -p web.tsconfig.json && resolve-tspaths -p web.tsconfig.json && sh ./fix-pkg.sh web/esm module && yarn tsc-esm-fix --tsconfig web.tsconfig.json \" \"tsc -p web-cjs.tsconfig.json && resolve-tspaths -p web-cjs.tsconfig.json && sh ./fix-pkg.sh web/cjs commonjs\" && webpack",
Expand Down Expand Up @@ -151,4 +152,4 @@
"multistream": "^4.1.0",
"tmp-promise": "^3.0.2"
}
}
}
19 changes: 10 additions & 9 deletions src/DataItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SIG_CONFIG, SignatureConfig } from "./constants";
import { getCryptoDriver } from "$/utils";
import { deserializeTags } from "./tags";
import { createHash } from "crypto";
import type { Base64URLString } from "./types";

export const MIN_BINARY_SIZE = 80;
export const MAX_TAG_BYTES = 4096;
Expand Down Expand Up @@ -38,7 +39,7 @@ export class DataItem implements BundleItem {
return DataItem.verify(this.binary);
}

get id(): string {
get id(): Base64URLString {
return base64url.encode(this.rawId);
}

Expand All @@ -58,7 +59,7 @@ export class DataItem implements BundleItem {
return this.binary.subarray(2, 2 + this.signatureLength);
}

get signature(): string {
get signature(): Base64URLString {
return base64url.encode(this.rawSignature);
}

Expand All @@ -76,7 +77,7 @@ export class DataItem implements BundleItem {
return SIG_CONFIG[this.signatureType].sigLength;
}

get owner(): string {
get owner(): Base64URLString {
return base64url.encode(this.rawOwner);
}

Expand All @@ -90,7 +91,7 @@ export class DataItem implements BundleItem {
return isPresent ? this.binary.subarray(targetStart + 1, targetStart + 33) : Buffer.alloc(0);
}

get target(): string {
get target(): Base64URLString {
return base64url.encode(this.rawTarget);
}

Expand All @@ -101,8 +102,8 @@ export class DataItem implements BundleItem {
return isPresent ? this.binary.subarray(anchorStart + 1, anchorStart + 33) : Buffer.alloc(0);
}

get anchor(): string {
return this.rawAnchor.toString();
get anchor(): Base64URLString {
return base64url.encode(this.rawAnchor); /* .toString(); */
}

get rawTags(): Buffer {
Expand All @@ -123,7 +124,7 @@ export class DataItem implements BundleItem {
return deserializeTags(Buffer.from(this.binary.subarray(tagsStart + 16, tagsStart + 16 + tagsSize)));
}

get tagsB64Url(): { name: string; value: string }[] {
get tagsB64Url(): { name: Base64URLString; value: Base64URLString }[] {
const _tags = this.tags;
return _tags.map((t) => ({
name: base64url.encode(t.name),
Expand All @@ -149,7 +150,7 @@ export class DataItem implements BundleItem {
return this.binary.subarray(dataStart, this.binary.length);
}

get data(): string {
get data(): Base64URLString {
return base64url.encode(this.rawData);
}

Expand Down Expand Up @@ -184,7 +185,7 @@ export class DataItem implements BundleItem {
data: string;
signature: string;
target: string;
tags: { name: string; value: string }[];
tags: { name: Base64URLString; value: Base64URLString }[];
} {
return {
signature: this.signature,
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/dataItem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("DataItem", () => {
});
describe("given we want to get the anchor", () => {
it("should return the anchor", async () => {
expect(dataItem.anchor).toEqual(anchor ?? "");
expect(dataItem.anchor).toEqual(base64url.encode(anchor ?? ""));
});
});
describe("given we want to get the target", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/filePolygon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PolygonSigner, createData } from "../../index";
import type { DataItemCreateOptions } from "../ar-data-base";
import { createData as createFileData } from "../../src/file/index";
import base64url from "base64url";

describe("Polygon signing tests", function () {
it("should sign and verify using non file", async function () {
Expand Down Expand Up @@ -48,7 +49,7 @@ describe("Polygon signing tests", function () {
expect((await d.rawOwner()).toString("hex")).toEqual(signer.pk);
expect(await d.signatureType()).toEqual(3);
expect(await d.target()).toEqual("OXcT1sVRSA5eGwt2k6Yuz8-3e3g9WJi5uSE99CWqsBs");
expect(await d.anchor()).toEqual("Math.apt'#]gng(36).substring(30)");
expect(await d.anchor()).toEqual(base64url.encode("Math.apt'#]gng(36).substring(30)"));
expect(await d.tags()).toEqual([{ name: "Content-Type", value: "image/png" }]);
expect(Buffer.compare(Buffer.from("hello"), await d.rawData())).toEqual(0);
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/fileTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("DataItem", () => {
});
describe("given we want to get the anchor", () => {
it("should return the anchor", async () => {
expect(await dataItem.anchor()).toEqual(anchor ?? "");
expect(await dataItem.anchor()).toEqual(base64url.encode(anchor ?? ""));
});
});
describe("given we want to get the target", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/polygon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64url from "base64url";
import { createData, PolygonSigner } from "../../index";
import type { DataItemCreateOptions } from "../ar-data-base";

Expand All @@ -18,7 +19,7 @@ describe("Polygon signing tests", function () {
expect(d.rawOwner.toString("hex")).toEqual(signer.pk);
expect(d.signatureType).toEqual(3);
expect(d.target).toEqual("OXcT1sVRSA5eGwt2k6Yuz8-3e3g9WJi5uSE99CWqsBs");
expect(d.anchor).toEqual("Math.apt'#]gng(36).substring(30)");
expect(d.anchor).toEqual(base64url.encode("Math.apt'#]gng(36).substring(30)"));
expect(d.tags).toEqual([{ name: "Content-Type", value: "image/png" }]);
expect(d.rawData.toString()).toEqual("hello");
});
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/signer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import base58 from "bs58";
import arweaveTestKey from "./test_key0.json";
import { createData as createFileData } from "../file";
import { Wallet } from "@ethersproject/wallet";
import base64url from "base64url";

const multiAptoskeyPairs = [
{
Expand Down Expand Up @@ -260,7 +261,7 @@ describe("Signers()", function () {
expect(encodedOwner).toEqual(publicKey);
expect(dataItem.signatureType).toEqual(signerTestVariation.signatureType);
expect(dataItem.target).toEqual(targetTestVariation.target ?? "");
expect(dataItem.anchor).toEqual(anchorTestVariation.anchor ?? "");
expect(dataItem.anchor).toEqual(base64url.encode(anchorTestVariation.anchor ?? ""));
expect(dataItem.tags).toEqual(tags ?? []);
});

Expand Down Expand Up @@ -346,7 +347,7 @@ describe("Signers()", function () {
expect(encodedOwner).toEqual(publicKey);
expect(await dataItem.signatureType()).toEqual(signerTestVariation.signatureType);
expect(await dataItem.target()).toEqual(targetTestVariation.target ?? "");
expect(await dataItem.anchor()).toEqual(anchorTestVariation.anchor ?? "");
expect(await dataItem.anchor()).toEqual(base64url.encode(anchorTestVariation.anchor ?? ""));
expect(await dataItem.tags()).toEqual(tags ?? []);
});

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/solana.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64url from "base64url";
import { SolanaSigner, createData } from "../../index";
import type { DataItemCreateOptions } from "../ar-data-base";
import base58 from "bs58";
Expand All @@ -18,7 +19,7 @@ describe("Solana signing tests", function () {
expect(base58.encode(d.rawOwner)).toEqual(signer.pk);
expect(d.signatureType).toEqual(2);
expect(d.target).toEqual("OXcT1sVRSA5eGwt2k6Yuz8-3e3g9WJi5uSE99CWqsBs");
expect(d.anchor).toEqual("Math.apt'#]gng(36).substring(30)");
expect(d.anchor).toEqual(base64url.encode("Math.apt'#]gng(36).substring(30)"));
expect(d.tags).toEqual([{ name: "Content-Type", value: "image/png" }]);
expect(d.rawData.toString()).toEqual("hello");
});
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Buffer } from "buffer";
import * as fs from "fs";
import type { DataItemCreateOptions } from "../../index";
import { SolanaSigner, bundleAndSignData, DataItem, createData, ArweaveSigner } from "../../index";
import base64url from "base64url";
const wallet0 = JSON.parse(readFileSync(path.join(__dirname, "test_key0.json")).toString());

describe("Creating and indexing a data item", function () {
Expand All @@ -28,7 +29,7 @@ describe("Creating and indexing a data item", function () {
expect(d.rawData.toString()).toEqual("hello");
expect(d.owner).toBe(wallet0.n);
expect(d.target).toBe("OXcT1sVRSA5eGwt2k6Yuz8-3e3g9WJi5uSE99CWqsBs");
expect(d.anchor).toEqual("Math.apt'#]gng(36).substring(30)");
expect(d.anchor).toEqual(base64url.encode("Math.apt'#]gng(36).substring(30)"));
expect(d.tags).toEqual([{ name: "Content-Type", value: "image/png" }]);
expect(await DataItem.verify(d.getRaw())).toEqual(true);
}, 5000000);
Expand All @@ -51,7 +52,7 @@ describe("Creating and indexing a data item", function () {
expect(Buffer.from(d.rawData).toString()).toBe("tasty");
expect(d.owner).toBe(wallet0.n);
expect(d.target).toBe("");
expect(d.anchor).toEqual("Math.apt'#]gng(36).substring(30)");
expect(d.anchor).toEqual(base64url.encode("Math.apt'#]gng(36).substring(30)"));
expect(d.tags).toEqual([
{
name: "testname",
Expand Down Expand Up @@ -140,7 +141,7 @@ describe("Creating and indexing a data item", function () {
expect(Buffer.from(dataItems[0].rawData).toString()).toBe("tasty");
expect(dataItems[0].owner).toBe(wallet0.n);
expect(Buffer.from(dataItems[0].target).toString()).toBe("pFwvlpz1x_nebBPxkK35NZm522XPnvUSveGf4Pz8y4A");
expect(dataItems[0].anchor).toEqual("Math.randomgng(36).substring(30)");
expect(dataItems[0].anchor).toEqual(base64url.encode("Math.randomgng(36).substring(30)"));
expect(dataItems[0].tags).toEqual([{ name: "x", value: "y" }]);
expect(await DataItem.verify(dataItems[0].getRaw())).toEqual(true);
});
Expand Down
5 changes: 3 additions & 2 deletions src/file/FileDataItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import axios from "axios";
import { SIG_CONFIG } from "../constants";
import { promisify } from "util";
import { deserializeTags } from "../tags";
import type { Base64URLString } from "../types";

const read = promisify(FSRead);
const write = promisify(FSWrite);
Expand Down Expand Up @@ -185,8 +186,8 @@ export class FileDataItem implements BundleItem {
return Buffer.allocUnsafe(0);
}

async anchor(): Promise<string> {
return (await this.rawAnchor()).toString();
async anchor(): Promise<Base64URLString> {
return base64url.encode(await this.rawAnchor());
}

async rawTags(): Promise<Buffer> {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Base64URLString = string;

0 comments on commit 5db54ee

Please sign in to comment.