-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAP.sol
More file actions
189 lines (151 loc) · 6.24 KB
/
TAP.sol
File metadata and controls
189 lines (151 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
pragma solidity ^0.4.4;
contract TAP {
struct Attestation {
uint id;
address contractAddress;
bytes4 methodId;
address attestorAddress;
string attestationIPFSHash;
bool exists;
// Votes
uint voteCount;
mapping (address => bool) addressVoted;
address[] usersWhoVoted;
}
struct Contract {
address contractAddress;
string verificationIPFSHash;
string name;
bool exists;
}
// All known verified contracts to be attested to
mapping (address => Contract) public contractVerifications;
// All known attestations
Attestation[] public attestations;
// Attestations ids for a given contract. Helpful for looping through attestations
// from outside of this contract.
mapping (address => uint[]) public idsForContract;
// All known attestation ids for a given user
mapping (address => uint[]) public attestationsForUser;
address owner;
modifier onlyOwner() {
if (msg.sender != owner) {
throw;
}
_;
}
function TAP() {
owner = msg.sender;
}
function addContractVerification(address _addr, string _ipfsHash, string _name) returns (bool) {
// Check to see if there already is a verification for this contract
if(contractVerifications[_addr].exists == true) {
return false;
}
Contract c = contractVerifications[_addr];
c.contractAddress = _addr;
c.verificationIPFSHash = _ipfsHash;
c.name = _name;
c.exists = true;
contractVerifications[_addr] = c;
return true;
}
function addAttestation(address _contractAddress, bytes4 _methodId, string _ipfsHash) returns (bool) {
Attestation a = attestations[attestations.length++];
a.id = attestations.length - 1;
a.contractAddress = _contractAddress;
a.methodId = _methodId;
a.attestorAddress = msg.sender;
a.attestationIPFSHash = _ipfsHash;
a.exists = true;
// Vote count starts at one because you vote for yourself
a.voteCount = 1;
a.addressVoted[msg.sender] = true;
a.usersWhoVoted.push(msg.sender);
// Update the indexes to allow quick lookup
idsForContract[_contractAddress].push(a.id);
attestationsForUser[msg.sender].push(a.id);
return true;
}
function vote(uint _attestationId) returns (bool) {
Attestation a = attestations[_attestationId];
// If user already voted return false
if (a.addressVoted[msg.sender] == true) {
return false;
}
a.voteCount++;
a.addressVoted[msg.sender] = true;
a.usersWhoVoted.push(msg.sender);
return true;
}
// Warning...this is unsafe if there are a LOT of votes.
// The function may run out of gas before resetting the state.
// Unfortunately we have to loop through the usersWhoVoted array, which
// can run out of gas in a transaction.
function unvote(uint _attestationId) returns (bool) {
Attestation a = attestations[_attestationId];
// If a user hasn't voted return false
if (a.addressVoted[msg.sender] == false) {
return false;
}
a.voteCount--;
a.addressVoted[msg.sender] = false;
int j = -1;
for (uint i = 0; i < a.usersWhoVoted.length; i++) {
if (j > -1 ) {
a.usersWhoVoted[i - 1] = a.usersWhoVoted[i];
}
if (a.usersWhoVoted[i] == msg.sender) {
j = int(i);
}
}
delete a.usersWhoVoted[a.usersWhoVoted.length - 1];
a.usersWhoVoted.length--;
return true;
}
// This return signature is giving major problems. It works in this order, but
// bytes4 seems to be screwing something up, as anything that comes after it doesn't
// get returned correctly. It seems to work if that's the last return val.
function getAttestation(uint id) constant returns (uint, address, address, string, uint, bytes4) {
if (id >= attestations.length) {
return;
} else {
Attestation a = attestations[id];
return (a.id,
a.contractAddress,
a.attestorAddress,
a.attestationIPFSHash,
a.voteCount,
a.methodId);
}
}
function getIdsForContract(address _contractAddress) constant returns (uint[]) {
return idsForContract[_contractAddress];
}
function getAttestationCountForUser(address _user) constant returns (uint) {
return attestationsForUser[_user].length;
}
// Right now reputation is just the raw data of how many attestations this user has written and
// how many collective votes their attestations have received
function getUserReputation(address _user) constant returns (uint numberOfAttestations, uint numberOfVotes) {
uint[] userAttestations = attestationsForUser[_user];
numberOfAttestations = userAttestations.length;
for (uint i = 0; i < numberOfAttestations; i++) {
numberOfVotes += attestations[userAttestations[i]].voteCount;
}
return (numberOfAttestations, numberOfVotes);
}
// Do not send ether to this contract blindly
function () payable {
throw;
}
}
// In trying to identify the function and match up attestations with verified code,
// this reference for the ABIs is helpful: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
// First four bytes of the SHA3 hash of the function signature are the first four bytes of the
// signed transaction data. Parameter types are split by a single comma with no spaces.
// So to compute this for function bidOnRecord(string recordId) returns (bool success),
// you would use bytes4(sha3("bidOnRecord(string)"))
// Also, should think about signing the attestation using the ethereum address private key
// which can then be verified to establish a link between the author and the attestation.
// http://ethereum.stackexchange.com/questions/1777/workflow-on-signing-a-string-with-private-key-followed-by-signature-verificatio/1794#1794