Skip to content

Commit 9e650c8

Browse files
committed
preview build
1 parent 0dc84df commit 9e650c8

File tree

15 files changed

+24570
-112
lines changed

15 files changed

+24570
-112
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
node_modules
2+
.env
3+
coverage
4+
coverage.json
5+
typechain
6+
typechain-types
7+
8+
# Hardhat files
9+
cache
10+
artifacts
11+

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"git.enabled": tru
3+
}

ipfs/contracts/IPFS.sol

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
interface IPFS {
5+
function add(bytes calldata data) external returns (bytes32);
6+
function get(bytes32 hash) external view returns (bytes memory);
7+
}
8+
9+
contract IPFSImplementation is IPFS {
10+
mapping(bytes32 => bytes) private ipfsData;
11+
12+
function add(bytes calldata data) external override returns (bytes32) {
13+
bytes32 hash = keccak256(data);
14+
ipfsData[hash] = data;
15+
return hash;
16+
}
17+
18+
function get(bytes32 hash) external view override returns (bytes memory) {
19+
return ipfsData[hash];
20+
}
21+
}

ipfs/contracts/Lock.sol

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.9;
3+
4+
// Uncomment this line to use console.log
5+
// import "hardhat/console.sol";
6+
7+
contract Lock {
8+
uint public unlockTime;
9+
address payable public owner;
10+
11+
event Withdrawal(uint amount, uint when);
12+
13+
constructor(uint _unlockTime) payable {
14+
require(
15+
block.timestamp < _unlockTime,
16+
"Unlock time should be in the future"
17+
);
18+
19+
unlockTime = _unlockTime;
20+
owner = payable(msg.sender);
21+
}
22+
23+
function withdraw() public {
24+
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
25+
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
26+
27+
require(block.timestamp >= unlockTime, "You can't withdraw yet");
28+
require(msg.sender == owner, "You aren't the owner");
29+
30+
emit Withdrawal(address(this).balance, block.timestamp);
31+
32+
owner.transfer(address(this).balance);
33+
}
34+
}

ipfs/contracts/hostify.sol

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
// Import IPFS library
5+
import "./IPFS.sol";
6+
7+
contract CDN {
8+
// Initialize IPFS object
9+
IPFS private ipfs;
10+
11+
// Set the IPFS object in the constructor
12+
constructor(address ipfsAddress) {
13+
ipfs = IPFS(ipfsAddress);
14+
}
15+
16+
// Add file to IPFS and return the file hash
17+
function addFile(bytes memory fileData) public returns (bytes32) {
18+
bytes32 hash = ipfs.add(fileData);
19+
return hash;
20+
}
21+
22+
// Retrieve file from IPFS by its hash
23+
function getFile(bytes32 hash) public view returns (bytes memory) {
24+
bytes memory fileData = ipfs.get(hash);
25+
return fileData;
26+
}
27+
}

ipfs/hardhat.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require("@nomicfoundation/hardhat-toolbox");
2+
3+
/** @type import('hardhat/config').HardhatUserConfig */
4+
module.exports = {
5+
solidity: "0.8.18",
6+
};

ipfs/public/index.html

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,12 @@
1010
content="Web site created using create-react-app"
1111
/>
1212
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13-
<!--
14-
manifest.json provides metadata used when your web app is installed on a
15-
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16-
-->
1713
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18-
<!--
19-
Notice the use of %PUBLIC_URL% in the tags above.
20-
It will be replaced with the URL of the `public` folder during the build.
21-
Only files inside the `public` folder can be referenced from the HTML.
22-
23-
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24-
work correctly both with client-side routing and a non-root public URL.
25-
Learn how to configure a non-root public URL by running `npm run build`.
26-
-->
2714
<title>React App</title>
2815
</head>
2916
<body>
3017
<noscript>You need to enable JavaScript to run this app.</noscript>
3118
<div id="root"></div>
32-
<!--
33-
This HTML file is a template.
34-
If you open it directly in the browser, you will see an empty page.
35-
36-
You can add webfonts, meta tags, or analytics to this file.
37-
The build step will place the bundled scripts into the <body> tag.
3819

39-
To begin the development, run `npm start` or `yarn start`.
40-
To create a production bundle, use `npm run build` or `yarn build`.
41-
-->
4220
</body>
4321
</html>

ipfs/scripts/deploy.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// We require the Hardhat Runtime Environment explicitly here. This is optional
2+
// but useful for running the script in a standalone fashion through `node <script>`.
3+
//
4+
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
5+
// will compile your contracts, add the Hardhat Runtime Environment's members to the
6+
// global scope, and execute the script.
7+
const hre = require("hardhat");
8+
9+
async function main() {
10+
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
11+
const unlockTime = currentTimestampInSeconds + 60;
12+
13+
const lockedAmount = hre.ethers.utils.parseEther("0.001");
14+
15+
const Lock = await hre.ethers.getContractFactory("Lock");
16+
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
17+
18+
await lock.deployed();
19+
20+
console.log(
21+
`Lock with ${ethers.utils.formatEther(
22+
lockedAmount
23+
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.address}`
24+
);
25+
}
26+
27+
// We recommend this pattern to be able to use async/await everywhere
28+
// and properly handle errors.
29+
main().catch((error) => {
30+
console.error(error);
31+
process.exitCode = 1;
32+
});

ipfs/src/App.js

Lines changed: 88 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import web3 from './web3';
33
import ipfs from './ipfs';
44
import storehash from './storehash';
55
import { Button } from 'reactstrap';
6-
import { render } from 'react-dom';
76

87
class App extends Component {
98

@@ -35,91 +34,102 @@ class App extends Component {
3534
this.setState({buffer});
3635
};
3736

38-
async () => {try{ this.setState({blockNumber:"waiting.."}); this.setState({gasUsed:"waiting..."});
37+
// On file submit to IPFS
38+
onSubmit = async (event) => {
39+
event.preventDefault();
40+
//bring in user's metamask account address
41+
const accounts = await web3.eth.getAccounts();
3942

40-
await web3.eth.getTransactionReceipt(this.state.transactionHash, (err, txReceipt) => {
41-
console.log(err,txReceipt);
42-
this.setState({txReceipt});
43-
});
44-
}
43+
console.log('Sending from Metamask account: ' + accounts[0]);
4544

46-
catch(error){ console.log(error); }}
45+
//obtain contract address from storehash.js
46+
const ethAddress= await storehash.options.address;
47+
this.setState({ethAddress});
4748

49+
//save document to IPFS,return its hash#, and set hash# to state
50+
await ipfs.add(this.state.buffer, (err, ipfsHash) => {
51+
console.log(err,ipfsHash);
52+
//setState by setting ipfsHash to ipfsHash[0].hash
53+
this.setState({ ipfsHash:ipfsHash[0].hash });
4854

49-
onSubmit = async (event) => { event.preventDefault();
55+
// call Ethereum contract method "sendHash" and .send IPFS hash to etheruem contract
56+
//return the transaction hash from the ethereum contract
5057

51-
//bring in user's metamask account address
52-
const accounts = await web3.eth.getAccounts();
58+
storehash.methods.sendHash(this.state.ipfsHash).send({
59+
from: accounts[0]
60+
}, (error, transactionHash) => {
61+
console.log(transactionHash);
62+
this.setState({transactionHash});
63+
}); //storehash
64+
}) //await ipfs.add
65+
}; //onSubmit
5366

54-
// render() { return ( <div className="App"> <header className="App-header"> <h1 className="App-title">Welcome to React</h1> </header> <p className="App-intro"> To get started, edit <code>src/App.js</code> and save to reload. </p> <hr /> <h2> Choose file to send to IPFS </h2> <form onSubmit={this.onSubmit}> <input type = "file" onChange = {this.captureFile} /> <input type="submit" /> </form> <hr /> <Button color="primary" onClick={this.onClick}>Get Transaction Receipt</Button> <Button color="primary" onClick={this.onClick}>Get Transaction Receipt Again</Button> <table> <thead> <tr> <th>Tx Receipt Category</th> <th>Values</th> <th> </th> </tr> </thead> <tbody> <tr> <td>IPFS Hash # stored on Eth Contract</td> <td>{this.state.ipfsHash}</td> <td></td> </tr> <tr> <td>Ethereum Contract Address</td> <td>{this.state.ethAddress}</td> <td></td> </tr> <tr> <td>Tx Hash # </td> <td>{this.state.transactionHash}</td> <td></td> </tr> <tr> <td>Block Number # </td> <td>{this.state.blockNumber}</td> <td></td> </tr> <tr> <td>Gas Used</td> <td>{this.state.gasUsed}</td> <td></td> </tr> </tbody> </table> </div> ); }}
67+
// Get transaction receipt from metamask
68+
getTransactionReceipt = async () => {
69+
try {
70+
const txReceipt = await web3.eth.getTransactionReceipt(this.state.transactionHash);
71+
console.log(txReceipt);
72+
this.setState({txReceipt});
73+
} catch(error) {
74+
console.log(error);
75+
}
76+
}
5577

56-
// Get transaction receipt from metamask
57-
getTransactionReceipt = async () => {
58-
try {
59-
const txReceipt = await web3.eth.getTransactionReceipt(this.state.transactionHash);
60-
console.log(txReceipt);
61-
this.setState({txReceipt});
62-
} catch(error) {
63-
console.log(error);
78+
// render the react app
79+
render() {
80+
return (
81+
<div className="App">
82+
<header className="App-header">
83+
<h1 className="App-title">Welcome to React</h1>
84+
</header>
85+
<hr />
86+
<h2> Choose file to send to IPFS </h2>
87+
<form onSubmit={this.onSubmit}>
88+
<input type = "file" onChange = {this.captureFile} />
89+
<input type="submit" />
90+
</form>
91+
<hr />
92+
<Button color="primary" onClick={this.getTransactionReceipt}>Get Transaction Receipt</Button>
93+
<table>
94+
<thead>
95+
<tr>
96+
<th>Tx Receipt Category</th>
97+
<th>Values</th>
98+
<th> </th>
99+
</tr>
100+
</thead>
101+
<tbody>
102+
<tr>
103+
<td>IPFS Hash # stored on Eth Contract</td>
104+
<td>{this.state.ipfsHash}</td>
105+
<td></td>
106+
</tr>
107+
<tr>
108+
<td>Ethereum Contract Address</td>
109+
<td>{this.state.ethAddress}</td>
110+
<td></td>
111+
</tr>
112+
<tr>
113+
<td>Tx Hash # </td>
114+
<td>{this.state.transactionHash}</td>
115+
<td></td>
116+
</tr>
117+
<tr>
118+
<td>Block Number # </td>
119+
<td>{this.state.blockNumber}</td>
120+
<td></td>
121+
</tr>
122+
<tr>
123+
<td>Gas Used</td>
124+
<td>{this.state.gasUsed}</td>
125+
<td></td>
126+
</tr>
127+
</tbody>
128+
</table>
129+
</div>
130+
);
64131
}
65132
}
66133

67-
render () {
68-
return (
69-
<div className="App">
70-
<header className="App-header">
71-
<h1 className="App-title">Welcome to React</h1>
72-
</header>
73-
<p className="App-intro">
74-
To get started, edit <code>src/App.js</code> and save to reload.
75-
</p>
76-
<hr />
77-
<h2> Choose file to send to IPFS </h2>
78-
<form onSubmit={this.onSubmit}>
79-
<input type = "file" onChange = {this.captureFile} />
80-
<input type="submit" />
81-
</form>
82-
<hr />
83-
<Button color="primary" onClick={this.onClick}>Get Transaction Receipt</Button>
84-
<Button color="primary" onClick={this.onClick}>Get Transaction Receipt Again</Button>
85-
<table>
86-
<thead>
87-
<tr>
88-
<th>Tx Receipt Category</th>
89-
<th>Values</th>
90-
<th> </th>
91-
</tr>
92-
</thead>
93-
<tbody>
94-
<tr>
95-
<td>IPFS Hash # stored on Eth Contract</td>
96-
<td>{this.state.ipfsHash}</td>
97-
<td></td>
98-
</tr>
99-
<tr>
100-
<td>Ethereum Contract Address</td>
101-
<td>{this.state.ethAddress}</td>
102-
<td></td>
103-
</tr>
104-
<tr>
105-
<td>Tx Hash # </td>
106-
<td>{this.state.transactionHash}</td>
107-
<td></td>
108-
</tr>
109-
<tr>
110-
<td>Block Number # </td>
111-
<td>{this.state.blockNumber}</td>
112-
<td></td>
113-
</tr>
114-
<tr>
115-
<td>Gas Used</td>
116-
<td>{this.state.gasUsed}</td>
117-
<td></td>
118-
</tr>
119-
</tbody>
120-
</table>
121-
</div>
122-
);
123-
}
124-
}
134+
export default App;
125135

ipfs/src/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
5-
import registerServiceWorker from './registerServiceWorker';
6-
import 'bootstrap/dist/css/bootstrap.min.css';
5+
// import registerServiceWorker from './registerServiceWorker';
6+
// import 'bootstrap/dist/css/bootstrap.min.css';
7+
8+
class registerServiceWorker {
9+
constructor() {
10+
this.registerServiceWorker = registerServiceWorker;
11+
}
12+
registerServiceWorker() {
13+
if ('serviceWorker' in navigator) {
14+
window.addEventListener('load', function() {
15+
navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
16+
// Registration was successful
17+
console.log('ServiceWorker registration successful with scope: ', registration.scope);
18+
}, function(err) {
19+
// registration failed :(
20+
console.log('ServiceWorker registration failed: ', err);
21+
});
22+
});
23+
}
24+
}
25+
}
726

827
ReactDOM.render(<App />, document.getElementById('root'));registerServiceWorker();
9-
// If you want to start measuring performance in your app, pass a function
10-
// to log results (for example: reportWebVitals(console.log))
11-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals

0 commit comments

Comments
 (0)