This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Go-like error catching implementation. | |
* | |
* The 'attempt' function receives a callback function as a parameter | |
* and calls it inside a try catch statement. | |
* | |
* Returns a tuple of [data, error] where 'data' (if exists) is the | |
* result of the operation and 'error' (if exists) | |
* is the error from the catch statement. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ethers from 'ethers'; | |
import { | |
ExternalProvider, | |
JsonRpcSigner, | |
Network, | |
Web3Provider | |
} from '@ethersproject/providers'; | |
import { useState } from 'react'; | |
declare global { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const untar = await require("js-untar"); | |
async download(file: PersssistFile) { | |
const iterable = this.ipfs.get(file.filePath); | |
var chunks: Uint8Array[] = []; | |
// we need to use a for await for downloading | |
// the buffer in chunks. | |
for await (const b of iterable) { | |
chunks.push(b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async getFilesMetadata(): Promise<PersssistFile[]> { | |
const methods = this.contract.methods; | |
const filesCount = await methods.fileCount().call(); | |
const filesMetadata: PersssistFile[] = []; | |
for (var i = filesCount; i >= 1; i--) { | |
const file = await methods.files(i).call() | |
filesMetadata.push({ | |
fileId: file.id, | |
fileName: file.fileName, | |
filePath: file.filePath, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async uploadFileMetadata( | |
path: string, | |
size: number, | |
type: string, | |
name: string, | |
account: string, | |
) { | |
return this.contract.methods | |
.uploadFile(path, size, type, name) | |
.send({ from: account }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { create } from "ipfs-http-client"; | |
async upload(file: File) { | |
this.ipfs = create({ | |
host: 'ipfs.infura.io', | |
port: 5001, | |
protocol: 'https' | |
}); | |
const blob = new Blob([file.buffer], { type: file.type }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Web3 from "web3"; | |
// we import the abi file created after | |
// the migration using truffle. | |
import Persssist from '../../public/abis/Persssist.json'; | |
async initializeContractLocal() { | |
if (window.ethereum) this.web3 = new Web3(window.ethereum) | |
else if (window.web3) this.web3 = new Web3(window.web3.currentProvider); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async requestAccounts() { | |
if(typeof window === "undefined") return; | |
return window | |
.ethereum?.request({ method: "eth_requestAccounts" }) | |
.catch((err: any) => console.log(err)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async fetchAccounts() { | |
if(typeof window === "undefined") return; | |
return window | |
.ethereum?.request({ method: "eth_accounts" }) | |
.catch((err: any) => console.log(err)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("uploads a valid file", async () => { | |
// checks filecount variable | |
// before uploading the file | |
const countBeforeUpload = await this.contract.fileCount(); | |
await this.contract.uploadFile('path', 1, 'type', 'name'); | |
// checks filecount increased | |
// by one after upload | |
const countAfterUpload = await this.contract.fileCount(); |
NewerOlder