Skip to content

Commit

Permalink
chore: add metadata to arweave
Browse files Browse the repository at this point in the history
  • Loading branch information
james-a-morris committed Feb 13, 2024
1 parent a9e4f16 commit 578f74c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ export class ArweaveClient {
return data;
}

/**
* Retrieves the metadata of a transaction
* @param transactionID The transaction ID of the record to retrieve
* @returns The metadata of the transaction if it exists, otherwise null
*/
async getMetadata(transactionID: string): Promise<Record<string, string> | null> {
const transaction = await this.client.transactions.get(transactionID);
if (!isDefined(transaction)) {
return null;
}
const tags = Object.fromEntries(
transaction.tags.map((tag) => [
tag.get("name", { decode: true, string: true }),
tag.get("value", { decode: true, string: true }),
])
);
return {
contentType: tags["Content-Type"],
appName: tags["App-Name"],
topic: tags.Topic,
};
}

/**
* Returns the address of the signer of the JWT
* @returns The address of the signer in this client
Expand Down
36 changes: 36 additions & 0 deletions test/arweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import winston from "winston";
import { ArweaveClient } from "../src/caching";
import { parseWinston, toBN } from "../src/utils";
import { object, string } from "superstruct";
import { ARWEAVE_TAG_APP_NAME } from "../src/constants";

const INITIAL_FUNDING_AMNT = "5000000000";
const LOCAL_ARWEAVE_NODE = {
Expand Down Expand Up @@ -118,4 +119,39 @@ describe("ArweaveClient", () => {
const retrievedValue = await client.get(txID!, validatorStruct);
expect(retrievedValue).to.eq(null);
});

it("should retrieve the metadata of a transaction", async () => {
const value = { test: "value" };
const txID = await client.set(value);
expect(txID).to.not.be.undefined;

// Wait for the transaction to be mined
await mineBlock();
await mineBlock();

const metadata = await client.getMetadata(txID!);
expect(metadata).to.deep.equal({
contentType: "application/json",
appName: ARWEAVE_TAG_APP_NAME,
topic: undefined,
});
});

it("should retrieve the metadata of a transaction with a topic tag", async () => {
const value = { test: "value" };
const topicTag = "test-topic";
const txID = await client.set(value, topicTag);
expect(txID).to.not.be.undefined;

// Wait for the transaction to be mined
await mineBlock();
await mineBlock();

const metadata = await client.getMetadata(txID!);
expect(metadata).to.deep.equal({
contentType: "application/json",
appName: ARWEAVE_TAG_APP_NAME,
topic: topicTag,
});
});
});

0 comments on commit 578f74c

Please sign in to comment.