Skip to content

Commit 3ac1b78

Browse files
authored
Merge pull request #30 from WeBankFinTech/feature/evi-linked-event
Evidence in linked event
2 parents 8af3ec9 + b0a1c7e commit 3ac1b78

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

contracts/EvidenceContract.sol

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
pragma solidity ^0.4.4;
2+
/*
3+
* Copyright© (2018-2020) WeBank Co., Ltd.
4+
*
5+
* This file is part of weidentity-contract.
6+
*
7+
* weidentity-contract is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* weidentity-contract is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with weidentity-contract. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
contract EvidenceContract {
22+
23+
// block number map, hash as key
24+
mapping(string => uint256) changed;
25+
26+
// Attribute keys
27+
string constant private ATTRIB_KEY_SIGNINFO = "info";
28+
string constant private ATTRIB_KEY_EXTRA = "extra";
29+
30+
// Error codes
31+
uint256 constant private RETURN_CODE_SUCCESS = 0;
32+
uint256 constant private RETURN_CODE_FAILURE_NOT_EXIST = 500600;
33+
34+
// Both hash and signer are used as identification key
35+
event EvidenceAttributeChanged(
36+
string hash,
37+
address signer,
38+
string key,
39+
string value,
40+
uint256 updated,
41+
uint256 previousBlock
42+
);
43+
44+
function getLatestRelatedBlock(
45+
string hash
46+
)
47+
public
48+
constant
49+
returns (uint256)
50+
{
51+
return changed[hash];
52+
}
53+
54+
/**
55+
* Create evidence. Here, hash value is the key; signInfo is the base64 signature;
56+
* and extra is the compact json of blob: {"credentialId":"aacc1122-324b.."}
57+
* This allows append operation from other signer onto a same hash, so no permission check.
58+
*/
59+
function createEvidence(
60+
string hash,
61+
string sig,
62+
string extra,
63+
uint256 updated
64+
)
65+
public
66+
{
67+
EvidenceAttributeChanged(hash, msg.sender, ATTRIB_KEY_SIGNINFO, sig, updated, changed[hash]);
68+
EvidenceAttributeChanged(hash, msg.sender, ATTRIB_KEY_EXTRA, extra, updated, changed[hash]);
69+
changed[hash] = block.number;
70+
}
71+
72+
/**
73+
* Aribitrarily append attributes to an existing hash evidence, e.g. revoke status.
74+
*/
75+
function setAttribute(
76+
string hash,
77+
string key,
78+
string value,
79+
uint256 updated
80+
)
81+
public
82+
{
83+
if (!isHashExist(hash)) {
84+
return;
85+
}
86+
if (isEqualString(key, ATTRIB_KEY_SIGNINFO)) {
87+
return;
88+
}
89+
EvidenceAttributeChanged(hash, msg.sender, key, value, updated, changed[hash]);
90+
changed[hash] = block.number;
91+
}
92+
93+
function isHashExist(string hash) public constant returns (bool) {
94+
if (changed[hash] != 0) {
95+
return true;
96+
}
97+
return false;
98+
}
99+
100+
function isEqualString(string a, string b) private constant returns (bool) {
101+
if (bytes(a).length != bytes(b).length) {
102+
return false;
103+
} else {
104+
return keccak256(a) == keccak256(b);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)