搭建第一个Dapp应用(3)——Solidity防伪溯源存证签证合约——2021.5.3
Key不回私聊,不懂评论留言,必回一丶配置:1.运行环境:FISCO BCOS联盟链中间件——WeBASE-Front2.合约语言:solidity 0.4.25版本二丶合约代码Evidence.solEvidenceFactory三丶效果展示1.部署合约,初始化传入2个用户地址2.查看合法签名者地址3.查看是否为合法地址4.创建签证合约(只有合法签名者才能成功创建)5.查看签证信息6.合法签名者
·
搭建第一个Dapp应用(3)——Solidity防伪溯源存证签证合约
一丶配置:
1.运行环境:FISCO BCOS联盟链中间件——WeBASE-Front
2.合约语言:solidity 0.4.25版本
二丶合约代码
Evidence.sol
pragma solidity ^0.4.25;
contract EvidenceSignersDataABI
{
//验证是否是合法地址
function verify(address addr)public constant returns(bool){}
//根据索引值返回签名者地址
function getSigner(uint index)public constant returns(address){}
//返回签名人数
function getSignersSize() public constant returns(uint){}
}
contract Evidence{
string evidence; //存证信息
address[] signers;//储存合法签名者地址
address public factoryAddr;//工厂合约地址
//返回事件信息,查看log->判断正确或错误的信息
event addSignaturesEvent(string evi);
event newSignaturesEvent(string evi, address addr);
event errorNewSignaturesEvent(string evi, address addr);
event errorAddSignaturesEvent(string evi, address addr);
event addRepeatSignaturesEvent(string evi);
event errorRepeatSignaturesEvent(string evi, address addr);
//查看此地址是否为合法签名者地址
function CallVerify(address addr) public constant returns(bool) {
return EvidenceSignersDataABI(factoryAddr).verify(addr);
}
//初始化,创建存证合约
constructor(string evi, address addr) {
factoryAddr = addr;
//tx.origin =>启动交易的原始地址(其实就是部署者的地址)
//如果是外部调用,在此可以理解为函数调用者地址
if(CallVerify(tx.origin))
{
evidence = evi;
signers.push(tx.origin);
newSignaturesEvent(evi,addr);
}
else
{
errorNewSignaturesEvent(evi,addr);
}
}
//返回签名信息,合约签名者地址,当前签名者地址
function getEvidence() public constant returns(string,address[],address[]){
uint length = EvidenceSignersDataABI(factoryAddr).getSignersSize();
address[] memory signerList = new address[](length);
for(uint i= 0 ;i<length ;i++)
{
signerList[i] = (EvidenceSignersDataABI(factoryAddr).getSigner(i));
}
return(evidence,signerList,signers);
}
//添加签名者地址(此地址必须为合约签名者地址)
function addSignatures() public returns(bool) {
for(uint i= 0 ;i<signers.length ;i++)
{
//此时的tx.orgin为当前调用此方法的调用者地址
if(tx.origin == signers[i])
{
addRepeatSignaturesEvent(evidence);
return true;
}
}
if(CallVerify(tx.origin))
{
signers.push(tx.origin);
addSignaturesEvent(evidence);
return true;
}
else
{
errorAddSignaturesEvent(evidence,tx.origin);
return false;
}
}
//返回所有的合约签名者地址
function getSigners()public constant returns(address[])
{
uint length = EvidenceSignersDataABI(factoryAddr).getSignersSize();
address[] memory signerList = new address[](length);
for(uint i= 0 ;i<length ;i++)
{
signerList[i] = (EvidenceSignersDataABI(factoryAddr).getSigner(i));
}
return signerList;
}
}
EvidenceFactory
pragma solidity ^0.4.25;
import "Evidence.sol";
contract EvidenceFactory{
address[] signers; //存储签名者地址
event newEvidenceEvent(address addr); //新签证事件,返回地址
//传入签名内容 string类型,创建合约Evidence并初始化
function newEvidence(string evi)public returns(address)
{
//this:代表当前工厂合约的地址
Evidence evidence = new Evidence(evi, this);
newEvidenceEvent(evidence);
return evidence;
}
//获得签证信息
function getEvidence(address addr) public constant returns(string,address[],address[]){
return Evidence(addr).getEvidence();
}
function addSignatures(address addr) public returns(bool) {
return Evidence(addr).addSignatures();
}
//初始化合约,导入签名者们的地址(数组传参)为合法签名者地址
//只有合法签名者才有资格进行签证
constructor(address[] evidenceSigners){
for(uint i=0; i<evidenceSigners.length; ++i) {
signers.push(evidenceSigners[i]);
}
}
// 验证身份是否为合法签名者地址
function verify(address addr)public constant returns(bool){
for(uint i=0; i<signers.length; ++i) {
if (addr == signers[i])
{
return true;
}
}
return false;
}
//根据索引值返回合约签名者地址
function getSigner(uint index)public constant returns(address){
uint listSize = signers.length;
//判断索引值是否越界
if(index < listSize)
{
return signers[index];
}
else
{
return 0;
}
}
//获取当前合约签名者人数
function getSignersSize() public constant returns(uint){
return signers.length;
}
//返回所有合约签名者的地址
function getSigners() public constant returns(address[]){
return signers;
}
}
三丶效果展示
1.部署合约,初始化传入2个用户地址
- 0x76c4e6e1d093092135d79677b724ae3470cdd6e3
- 0xad6110c4f698ec996667fd5e2da0cdf5f1ee78e8
2.查看合法签名者地址
3.查看是否为合法地址
4.创建签证合约(只有合法签名者才能成功创建)
5.查看签证信息
6.合法签名者签名签证合约
7.再次查看签证信息
四丶上一篇文章:搭建第一个Dapp应用(2)——搭建WeBase-Front中间件
搭建第一个Dapp应用(2)——搭建WeBase-Front中间件
五丶下一篇文章:搭建第一个Dapp应用(4)——搭建SmartDev-Scaffold
更多推荐
已为社区贡献2条内容
所有评论(0)