美文网首页
ConfusionCoin

ConfusionCoin

作者: 小吖么小一郎 | 来源:发表于2020-11-23 16:06 被阅读0次
pragma solidity 0.6.2;

/*
    混币
*/
contract ConfusionCoin{
    
    string private salt = "403b36d4083daa52879d457f4726af2986026584ec207421d20b3";
    
    mapping(bytes32 => uint256) voucherMapping;

    
    function getMapping(bytes32 b) public view returns(uint256){
        return voucherMapping[b];
    }
    // 传入任意字符串作为凭证,生成凭证验证码
    function getVoucher(string memory random) public view returns(bytes32){
        require(bytes(random).length == 64,"random length is error");
        string memory k = strAddStr(random,salt);
        return sha256(bytes(k));
    }
    // 将验证码传入合约,并记录
    function setVoucher(bytes32 voucher) public payable{
        require(msg.value == 1 ether || msg.value == 10 ether,"payable eth value is error");
        voucherMapping[voucher] = msg.value;
    }
    // 传入凭证,用来触发指定业务
    function verificationVoucher(string memory random) public view returns(uint256){
        require(bytes(random).length == 64,"random length is error");
        bytes32 b = getVoucher(random);
        require(voucherMapping[b] > 0,"verification voucher is error");
        if(voucherMapping[b] > 0){
            // 业务
            
            return 1;
        }
        return 0;
    }
    
    function strAddStr(string memory a,string memory b) public pure returns(string memory){
        bytes memory _a = bytes(a);
        bytes memory _b = bytes(b);
        string memory ab = new string(_a.length + _b.length);
        bytes memory _ab = bytes(ab);
        uint k = 0;
        for(uint i=0; i<_a.length; i++){
            _ab[k++] = _a[i];    
        }
        for(uint j=0; j<_b.length; j++){
            _ab[k++] = _b[j];
        }
        return string(_ab);
    }
}

相关文章

网友评论

      本文标题:ConfusionCoin

      本文链接:https://www.haomeiwen.com/subject/cgmkiktx.html