Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 reads document history from chain #6

Open
wants to merge 1 commit into
base: ipfs-to-contract
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/IpfsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const IpfsPage: React.FC = () => {
console.log("cid stored on chain", resultBytes);

const bytes = Web3.utils.hexToBytes(resultBytes);
console.log(bytes);
const cid = new CID(Buffer.from(bytes));

console.log("CID recovered from contract", cid.toString());

const contentChunks = await ipfsNode!.cat(cid);
Expand All @@ -56,6 +58,24 @@ const IpfsPage: React.FC = () => {
return result;
}

async function readAllPreviousVersions() {
const oldEvents = await contract.getPastEvents("DocumentAdded", {
fromBlock: "earliest",
toBlock: "latest",
});
console.log(oldEvents);
const cids = oldEvents.map((evt) => {
try {
const bytes = Web3.utils.hexToBytes(evt.returnValues.cid);
const cid = new CID(Buffer.from(bytes));
return { cid };
} catch (e) {
return { cid: null };
}
});
setFiles(cids);
}

async function addToIpfs(content: string | any[]): Promise<any[]> {
const addResult = ipfsNode!.add(content);
const results = [];
Expand Down Expand Up @@ -101,17 +121,21 @@ const IpfsPage: React.FC = () => {
</button>
</form>
<h2>History:</h2>
<button onClick={readAllPreviousVersions}>get from chain...</button>
<ul>
{files.map((f) => (
<li>
<a
href={`https://ipfs.io/ipfs/${f.cid.toString()}`}
target="_blank"
>
{f.cid.toString()}
</a>
</li>
))}
{files.map(
(f) =>
f.cid && (
<li key={f.cid}>
<a
href={`https://ipfs.io/ipfs/${f.cid.toString()}`}
target="_blank"
>
{f.cid.toString()}
</a>
</li>
)
)}
</ul>
<h2>My Document:</h2>
<button onClick={getMyDocumentFromChainAndResolve}>
Expand Down