合约的编写
首先在remix上编写Proof.sol智能合约代码
pragma solidity ^0.4.0;
contract Proof{
struct FileDetails{
uint timestamp;
string owner;
}
mapping(string => FileDetails) files;
event logFileAddedStatus(bool status,uint timestamp,string owner,string fileHash);
function set(string owner,string fileHash)public{
if(files[fileHash].timestamp == 0){
files[fileHash] = FileDetails(block.timestamp,owner);
emit logFileAddedStatus(true,block.timestamp,owner,fileHash);
}else{
emit logFileAddedStatus(false,block.timestamp,owner,fileHash);
}
}
function get(string fileHash) view public returns(uint timestamp,string owner){
return (files[fileHash].timestamp,files[fileHash].owner);
}
}
点击Compile Proof.sol按钮编译合约,然后点击Compilation Details查看合约编译后的相关信息,找到WEB3DEPLOY并复制下来。
使用合约对象的new方法部署
//以ABI为参数创建一个合约对象
var proofContract = new web3.eth.Contract([{
"constant":true,"inputs":[{
"name":"fileHash","type":"string"}],"name":"get","outputs":[{
"name":"timestamp","type":"uint256"},{
"name":"owner","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{
"constant":false,"inputs":[{
"name":"owner","type":"string"},{
"name":"fileHash","type":"string"}],"name":"set","outputs":[],