From 2a907484b07a3cd42e7ffb63e92b5945fb1fe90e Mon Sep 17 00:00:00 2001 From: Stefan Adolf Date: Mon, 25 May 2020 14:17:45 +0200 Subject: [PATCH] read document history from chain --- src/IpfsPage.tsx | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/IpfsPage.tsx b/src/IpfsPage.tsx index 79389a8..2add116 100644 --- a/src/IpfsPage.tsx +++ b/src/IpfsPage.tsx @@ -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); @@ -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 { const addResult = ipfsNode!.add(content); const results = []; @@ -101,17 +121,21 @@ const IpfsPage: React.FC = () => {

History:

+

My Document: