Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Arweave from "arweave";
type Post = {
authorship: {
contributor: string;
};
content: {
body: string;
timestamp: string;
title: string;
};
digest: string;
originalDigest: string;
};
const arweave = Arweave.init({
host: "arweave.net",
port: 443,
protocol: "https",
});
async function getPostsForContributor(address: string): Promise<Post[]> {
const arweaveTransactionIds = await arweave.transactions.search(
"Contributor",
address
);
const arweaveTransactions = await Promise.all(
arweaveTransactionIds.map((txId) => arweave.transactions.get(txId))
);
const postsByOriginalDigest: Record<string, Post> = {};
for (const transaction of arweaveTransactions) {
const tags: Record<string, string> = {};
for (const tag of transaction.tags) {
const name = tag.get("name", { decode: true, string: true });
const value = tag.get("value", { decode: true, string: true });
tags[name] = value;
}
const appName = tags["App-Name"];
if (appName !== "MirrorXYZ") {
continue;
}
const originalDigest = tags["Original-Content-Digest"];
if (postsByOriginalDigest[originalDigest]) {
continue;
}
const rawData = transaction.get("data", { decode: true, string: true });
postsByOriginalDigest[originalDigest] = {
...JSON.parse(rawData),
originalDigest,
};
}
return Object.values(postsByOriginalDigest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment