Skip to content

Commit

Permalink
Merge pull request #70 from AbdeltwabMF/development
Browse files Browse the repository at this point in the history
Upgrade smart contract
  • Loading branch information
AbdeltwabMF authored Jul 8, 2022
2 parents 804c14d + ca8b7fc commit eaecba2
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 41 deletions.
55 changes: 36 additions & 19 deletions __tests__/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,51 @@
const { ethers } = require('hardhat')
const { expect } = require('chai')

it('should return 1 -- the count of the files', async function () {
const Storage = await ethers.getContractFactory('Storage')
describe('Storage', () => {
const deploy = async () => {
const Storage = await ethers.getContractFactory('Storage')

const storage = await Storage.deploy()
await storage.deployed()
const storage = await Storage.deploy()
await storage.deployed()
return storage
}

const storeFileTx = await storage.storeFile('amd.pdf', 134289, 'application/pdf', 'C81E24EB7A70438929DA795574EE7E7935B5FCFBEA33D54640B243A52DD34288')
it('should return the count of files equal to 1', async function () {
const storage = await deploy()
const storeFileTx = await storage.storeFile('amf.pdf', 134289, 'application/pdf', 'C81E24EB7A70438929DA795574EE7E7935B5FCFBEA33D54640B243A52DD34288')

// wait until the transaction is mined
await storeFileTx.wait()
// wait until the transaction is mined
await storeFileTx.wait()

const filesCount = await storage.getFilesCount()
const filesCount = await storage.getFilesCount()

expect(parseInt(filesCount, 10)).to.equal(1)
})
expect(parseInt(filesCount, 10)).to.equal(1)
})

it('should revert the transaction and return 0 as files count', async function () {
const storage = await deploy()
const storeFileTx = await storage.storeFile('amd.pdf', '1073741824', 'application/pdf', 'QmbopufRN1kR1FENy3YnsjuZikbbBEmzvTMcxPpc9Ztfm1')

// wait until the transaction is mined
await storeFileTx.wait()

it('should return 0 -- the count of the files', async function () {
const Storage = await ethers.getContractFactory('Storage')
const filesCount = await storage.getFilesCount()

const storage = await Storage.deploy()
await storage.deployed()
expect(parseInt(filesCount, 10)).to.equal(1)
})

const storeFileTx = await storage.storeFile('amd.pdf', '1073741824', 'application/pdf', 'QmbopufRN1kR1FENy3YnsjuZikbbBEmzvTMcxPpc9Ztfm1')
it('should return null for file with index 0', async function () {
const storage = await deploy()
const storeFileTx = await storage.storeFile('hola.png', 14289, 'image/png', 'C81E24EB7A70438929DA795574EE7E7935B5FCFBEA33D54640B243A52DD34288')
await storeFileTx.wait()

// wait until the transaction is mined
await storeFileTx.wait()
const files = await storage.getAllFiles()
console.log(parseInt(files[0].index, 10))

const filesCount = await storage.getFilesCount()
const removeFileTx = await storage.removeFile(1)
await removeFileTx.wait()

expect(parseInt(filesCount, 10)).to.equal(1)
const filesCount = await storage.getFilesCount()
expect(filesCount).to.equal(0)
})
})
2 changes: 1 addition & 1 deletion artifacts/contracts/storage.sol/Storage.dbg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/3cf9a72e7ddf28a09fcd35e365dbd97b.json"
"buildInfo": "../../build-info/36b742c7a7077c488496f38b514e2955.json"
}
23 changes: 21 additions & 2 deletions artifacts/contracts/storage.sol/Storage.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions components/FilesList/FilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function FilesList (props) {
}

const removeFileHandler = () => {
setIsRemoving(prevState => UNSET)
console.log(selectedFileIndex)
removeFile(selectedFileIndex)
setIsRemoving(prevState => TRUE)
Expand Down Expand Up @@ -146,6 +147,8 @@ export default function FilesList (props) {
<button
className={styles.actionDropdownItem + ' dropdown-item'}
onClick={() => {
console.log(file.index)
console.log(files.length)
setSelectedFileIndex(prevState => file.index)
setAreYouSureRemovingFile(prevState => TRUE)
}}
Expand Down
7 changes: 5 additions & 2 deletions components/Pagination/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ export default function Pagination () {
icon={faChevronLeft}
className={styles.icon}
/>
<span className={styles.prev}>prev</span>
<span className={styles.prev}>Prev</span>
</button>
<span className={styles.ratio}>
{currentPage} of {totalPages}
</span>
<button className={styles.button} onClick={nextPageHandler}>
<span className={styles.next}>next</span>
<span className={styles.next}>Next</span>
<FontAwesomeIcon
icon={faChevronRight}
className={styles.icon}
Expand Down
87 changes: 73 additions & 14 deletions contracts/storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pragma solidity ^0.8.4;
*/

contract Storage {
/**
* @dev The file's metadata.
*/
mapping(address => File[]) private _ownerToFiles;
mapping(address => mapping(string => uint256)) private _hashToIndex;

Expand Down Expand Up @@ -48,6 +51,13 @@ contract Storage {
uint256 uploadTime
);

/**
* @dev Creates a new file.
* @param _name Name of the file.
* @param _size Size of the file.
* @param _mimeType Mime type of the file.
* @param _hash Hash of the file.
*/
function storeFile(
string memory _name,
uint256 _size,
Expand All @@ -72,7 +82,7 @@ contract Storage {
require(bytes(_hash).length > 0, "Hash cannot be empty");

File memory newFile = File(
_ownerToFiles[msg.sender].length,
_ownerToFiles[msg.sender].length + 1,
_name,
_size,
_mimeType,
Expand All @@ -96,18 +106,32 @@ contract Storage {
}
}

/**
* @dev Count the number of files owned by the caller.
*/
function getFilesCount() public view returns (uint256) {
return _ownerToFiles[msg.sender].length;
}

/**
* @dev Returns the file at the given index.
* @param _index The index of the file to return. -- 1-based indexing
*/
function getSingleFile(uint256 _index) public view returns (File memory) {
require(
_index < _ownerToFiles[msg.sender].length,
_index <= _ownerToFiles[msg.sender].length,
"Index out of bounds"
);
return _ownerToFiles[msg.sender][_index];
require(
_index > 0,
"Index must be greater than 0"
);
return _ownerToFiles[msg.sender][_index - 1];
}

/**
* @dev Returns an array of files owned by the caller.
*/
function getAllFiles() public view returns (File[] memory) {
require(_ownerToFiles[msg.sender].length > 0, "No files found!");
return _ownerToFiles[msg.sender];
Expand All @@ -121,41 +145,59 @@ contract Storage {
}
}

/**
* @dev Share a file with another user.
* @param _startIndex the start index of the range of files to return. -- 1-based indexing
* @param _endIndex the end index of the range of files to return. -- 1-based indexing
*/
function getRangeOfFiles(uint256 _startIndex, uint256 _endIndex)
public
view
returns (File[] memory)
{
require(_ownerToFiles[msg.sender].length > 0, "No files found!");
require(
_startIndex < _ownerToFiles[msg.sender].length,
_startIndex <= _ownerToFiles[msg.sender].length,
"Index out of bounds"
);
require(_startIndex > 0, "Index out of bounds");
require(
_endIndex > _startIndex,
"start index must be strictly smaller than the end index"
);

_endIndex = min(_endIndex, _ownerToFiles[msg.sender].length);
_endIndex = min(_endIndex, _ownerToFiles[msg.sender].length + 1);
uint256 range = _endIndex - _startIndex;
_startIndex = _startIndex - 1;
_endIndex = _endIndex - 1;
File[] memory result = new File[](range);
for (uint256 i = _startIndex; i < _endIndex; ++i) {
result[i - _startIndex] = _ownerToFiles[msg.sender][i];
}
return result;
}

/**
* @dev Share a file with another user.
* @param _to The address of the user to share the file with.
* @param _index The index of the file to be shared. -- 1-based indexing
*/
function shareFile(address _to, uint256 _index) public {
require(
_index < _ownerToFiles[msg.sender].length,
_index <= _ownerToFiles[msg.sender].length,
"Index out of bounds!"
);
require(
_index > 0,
"Index must be greater than 0!"
);

require(_to != msg.sender, "To yourself!");
require(_to != address(0), "Null address!");

File memory file = _ownerToFiles[msg.sender][_index];
File memory file = _ownerToFiles[msg.sender][_index - 1];

// check if the file's hash is already present in the hashes array of the receiver
// check if the file's hash is already present in the hashes array
if (_hashToIndex[_to][file.hash] == 0) {
_ownerToFiles[_to].push(file);
_hashToIndex[_to][file.hash] = _ownerToFiles[_to].length;
Expand All @@ -172,25 +214,34 @@ contract Storage {
}
}

/**
* @dev Removes a file from the storage.
* @param _index The index of the file to be removed. -- 1-based indexing
*/
function removeFile(uint256 _index) public {
require(
_index < _ownerToFiles[msg.sender].length,
_index <= _ownerToFiles[msg.sender].length,
"Index out of bounds!"
);
require(
_index > 0,
"Index must be greater than 0!"
);

File memory file = _ownerToFiles[msg.sender][_index];
File memory file = _ownerToFiles[msg.sender][_index - 1];

_ownerToFiles[msg.sender][_index] = _ownerToFiles[msg.sender][
_ownerToFiles[msg.sender][_index - 1] = _ownerToFiles[msg.sender][
_ownerToFiles[msg.sender].length - 1
];
_ownerToFiles[msg.sender][_index].index = _index;
_ownerToFiles[msg.sender].pop();
_ownerToFiles[msg.sender][_index - 1].index = _index;

_hashToIndex[msg.sender][
_ownerToFiles[msg.sender][_index].hash
_ownerToFiles[msg.sender][_index - 1].hash
] = _hashToIndex[msg.sender][file.hash];
_hashToIndex[msg.sender][file.hash] = 0;

_ownerToFiles[msg.sender].pop();

emit FileRemoved(
file.index,
file.name,
Expand All @@ -200,4 +251,12 @@ contract Storage {
file.uploadTime
);
}

/**
* @dev Is the file with the given hash stored in the storage?
* @param _hash The hash of the file to check.
*/
function isFilePresent(string memory _hash) public view returns (bool) {
return _hashToIndex[msg.sender][_hash] != 0;
}
}
2 changes: 1 addition & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function App ({ Component, pageProps }) {
const [blockNumber, setBlockNumber] = useState(UNSET)
const [balance, setBalance] = useState(UNSET)
// 3 = '0x449fE6C97F4AD1d4769971F9fb1C33a64856AB73'
const contractAddress = '0x801e446103561Ac3d0eb8fC416a44917FC83a4D5'
const contractAddress = '0x37655359fD67c7A91C7D7501EEee7b087A486780'

useEffect(() => {
import('bootstrap/dist/js/bootstrap.min.js')
Expand Down
14 changes: 12 additions & 2 deletions pages/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default function Vault () {
const [isRemoving, setIsRemoving] = useState(UNSET)

const [pageSize, setPageSize] = useState(10)
const [firstIndex, setFirstIndex] = useState(0)
const [lastIndex, setLastIndex] = useState(pageSize)
const [firstIndex, setFirstIndex] = useState(1)
const [lastIndex, setLastIndex] = useState(1 + pageSize)
const [currentPage, setCurrentPage] = useState(1)
const [totalFiles, setTotalFiles] = useState(0)
const [totalPages, setTotalPages] = useState(1)
Expand Down Expand Up @@ -269,12 +269,22 @@ export default function Vault () {
console.log('Encrypting & Uploading file to IPFS...')
setIsUploading(prevState => TRUE)

const _options = { from: account, gasLimit: 3000000 }
const ipfs = getIpfs()
// fileBuffer = ArrayBuffer
const encryptedFileBufferWordArray = encryptAES256(fileBuffer, passphrase)

const response = await ipfs.add(Buffer.from(encryptedFileBufferWordArray))

console.log('response:', response.path)
const isFilePresent = await contract.isFilePresent(response.path, _options)
console.log(isFilePresent)
if (isFilePresent) {
console.log('File already exists')
setIsUploading(prevState => FALSE)
return
}

setSize(prevState => response.size)
setHash(prevState => response.path)

Expand Down

1 comment on commit eaecba2

@vercel
Copy link

@vercel vercel bot commented on eaecba2 Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

devault – ./

devault-git-main-abdeltwabmf.vercel.app
devault-abdeltwabmf.vercel.app
devault.vercel.app

Please sign in to comment.