Skip to content

Commit

Permalink
read document history from chain
Browse files Browse the repository at this point in the history
  • Loading branch information
elmariachi111 committed May 25, 2020
1 parent be675f7 commit 87a129d
Showing 1 changed file with 34 additions and 10 deletions.
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

0 comments on commit 87a129d

Please sign in to comment.