This repository was archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathProofOfPhysicalAddress.sol
347 lines (298 loc) · 10.9 KB
/
ProofOfPhysicalAddress.sol
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
pragma solidity 0.4.19;
import "./EthereumClaimsRegistryInterface.sol";
import "./PhysicalAddressClaim.sol";
// Checks -> Effects -> Interactions
contract ProofOfPhysicalAddress {
address public owner;
address public signer;
EthereumClaimsRegistryInterface public registry;
// Main structures:
struct PhysicalAddress {
string name;
string country;
string state;
string city;
string location;
string zip;
uint256 creationBlock;
bytes32 keccakIdentifier;
bytes32 confirmationCodeSha3;
}
function ProofOfPhysicalAddress(address _registry) public
{
owner = msg.sender;
signer = owner;
registry = EthereumClaimsRegistryInterface(_registry);
}
struct User {
uint256 creationBlock;
PhysicalAddress[] physicalAddresses;
}
mapping (address => User) public users;
// Stats:
uint64 public totalUsers;
uint64 public totalAddresses;
uint64 public totalConfirmed;
// Helpers:
function signerIsValid(bytes32 data, uint8 v, bytes32 r, bytes32 s)
public constant returns (bool)
{
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixed = keccak256(prefix, data);
return (ecrecover(prefixed, v, r, s) == signer);
}
// Methods:
// set address that is used on server-side to calculate signatures
// and on contract-side to verify them
function setSigner(address newSigner) public {
require(msg.sender == owner);
signer = newSigner;
}
function setRegistry(address newRegistry) public {
require(msg.sender == owner);
registry = EthereumClaimsRegistryInterface(newRegistry);
}
// withdraw specified amount of eth in wei
function withdrawSome(uint256 amountWei)
public
{
require(msg.sender == owner);
require(address(this).balance >= amountWei);
owner.transfer(amountWei);
}
// withdraw all available eth
function withdrawAll()
public
{
require(msg.sender == owner);
require(address(this).balance > 0);
owner.transfer(address(this).balance);
}
function userExists(address wallet)
public constant returns (bool)
{
return (users[wallet].creationBlock > 0);
}
function userAddressConfirmed(address wallet, uint256 addressIndex)
public constant returns (bool)
{
require(userExists(wallet));
bytes32 keccakIdentifier = users[wallet].physicalAddresses[addressIndex].keccakIdentifier;
if (keccakIdentifier == 0x0) {
return false;
}
return PhysicalAddressClaim.decodeConfirmation(registry.getClaim(address(this), wallet, keccakIdentifier)) > 0;
}
// returns (found/not found, index if found/0 if not found, confirmed/not confirmed)
function userAddressByCreationBlock(address wallet, uint256 creationBlock)
public constant returns (bool, uint256, bool)
{
require(userExists(wallet));
for (uint256 ai = 0; ai < users[wallet].physicalAddresses.length; ai++) {
if (users[wallet].physicalAddresses[ai].creationBlock == creationBlock) {
return (true, ai, userAddressConfirmed(wallet, ai));
}
}
return (false, 0, false);
}
// returns (found/not found, index if found/0 if not found, confirmed/not confirmed)
function userAddressByConfirmationCode(
address wallet,
bytes32 confirmationCodeSha3
)
public
constant
returns(bool, uint256, bool, bytes32)
{
require(userExists(wallet));
for (uint256 ai = 0; ai < users[wallet].physicalAddresses.length; ai++) {
if (users[wallet].physicalAddresses[ai].confirmationCodeSha3 == confirmationCodeSha3) {
return (
true,
ai,
userAddressConfirmed(wallet, ai),
users[wallet].physicalAddresses[ai].keccakIdentifier
);
}
}
return (false, 0, false, 0x0);
}
// returns (found/not found, index if found/0 if not found, confirmed/not confirmed)
function userAddressByAddress(address wallet, string country, string state, string city, string location, string zip)
public constant returns(bool, uint256, bool)
{
require(userExists(wallet));
bytes32 keccakIdentifier = keccak256(country, state, city, location, zip);
for (uint256 ai = 0; ai < users[wallet].physicalAddresses.length; ai++) {
if (users[wallet].physicalAddresses[ai].keccakIdentifier == keccakIdentifier) {
return (true, ai, userAddressConfirmed(wallet, ai));
}
}
return (false, 0, false);
}
// returns last name submitted to PoPA contract
function userLastSubmittedName(address wallet)
public constant returns (string)
{
require(userExists(wallet));
return users[wallet].physicalAddresses[users[wallet].physicalAddresses.length-1].name;
}
// returns name from the last confirmed address. If no addresses were confirmed returns ''
function userLastConfirmedName(address wallet)
public constant returns (string)
{
require(userExists(wallet));
for (uint256 ai = users[wallet].physicalAddresses.length; ai > 0;) {
ai--;
if (userAddressConfirmed(wallet, ai)) {
return users[wallet].physicalAddresses[ai].name;
}
}
return "";
}
// returns how many addresses there are in PoPA contract. If user does not exist, returns 0
function userSubmittedAddressesCount(address wallet)
public constant returns (uint256)
{
return users[wallet].physicalAddresses.length;
}
// returns how many addresses from PoPA contract are confirmed. If user does not exist, returns 0
function userConfirmedAddressesCount(address wallet)
public constant returns (uint256)
{
uint256 c = 0;
for (uint256 ai = 0; ai < users[wallet].physicalAddresses.length; ai++) {
if (userAddressConfirmed(wallet, ai)) {
c += 1;
}
}
return c;
}
function userAddress(address wallet, uint256 addressIndex)
public constant returns (
string country, string state, string city, string location, string zip)
{
require(userExists(wallet));
return (
users[wallet].physicalAddresses[addressIndex].country,
users[wallet].physicalAddresses[addressIndex].state,
users[wallet].physicalAddresses[addressIndex].city,
users[wallet].physicalAddresses[addressIndex].location,
users[wallet].physicalAddresses[addressIndex].zip
);
}
function userAddressInfo(address wallet, uint256 addressIndex)
public constant returns (
string name,
uint256 creationBlock,
uint256 confirmationBlock,
bytes32 keccakIdentifier
) {
require(userExists(wallet));
return (
users[wallet].physicalAddresses[addressIndex].name,
users[wallet].physicalAddresses[addressIndex].creationBlock,
PhysicalAddressClaim.decodeConfirmation(registry.getClaim(
address(this),
wallet,
users[wallet].physicalAddresses[addressIndex].keccakIdentifier)
),
users[wallet].physicalAddresses[addressIndex].keccakIdentifier
);
}
// Main methods:
function registerAddress(
string name,
string country, string state, string city, string location, string zip,
uint256 priceWei,
bytes32 confirmationCodeSha3, uint8 sigV, bytes32 sigR, bytes32 sigS)
public payable
{
require(bytes(name).length > 0);
require(bytes(country).length > 0);
require(bytes(state).length > 0);
require(bytes(city).length > 0);
require(bytes(location).length > 0);
require(bytes(zip).length > 0);
require(msg.value >= priceWei);
bytes32 data = keccak256(
msg.sender,
name,
country,
state,
city,
location,
zip,
priceWei,
confirmationCodeSha3
);
require(signerIsValid(data, sigV, sigR, sigS));
if (userExists(msg.sender)) {
// check if this address is already registered
bool found;
(found, , ) = userAddressByAddress(msg.sender, country, state, city, location, zip);
require(!found);
} else {
// new user
users[msg.sender].creationBlock = block.number;
totalUsers += 1;
}
PhysicalAddress memory pa;
pa.name = name;
pa.country = country;
pa.state = state;
pa.city = city;
pa.location = location;
pa.zip = zip;
pa.creationBlock = block.number;
pa.confirmationCodeSha3 = confirmationCodeSha3;
pa.keccakIdentifier = keccak256(country, state, city, location, zip);
users[msg.sender].physicalAddresses.push(pa);
totalAddresses += 1;
}
function unregisterAddress(string country, string state, string city, string location, string zip) public {
require(userExists(msg.sender));
bool found;
uint256 index;
(found, index, ) = userAddressByAddress(msg.sender, country, state, city, location, zip);
require(found);
registry.removeClaim(
address(this),
msg.sender,
users[msg.sender].physicalAddresses[index].keccakIdentifier
);
// Remove physical address from list
uint256 length = users[msg.sender].physicalAddresses.length;
for (uint256 i = index; i < length - 1; i++) {
users[msg.sender].physicalAddresses[i] = users[msg.sender].physicalAddresses[i+1];
}
delete users[msg.sender].physicalAddresses[length - 1];
users[msg.sender].physicalAddresses.length--;
if (users[msg.sender].physicalAddresses.length == 0) {
delete users[msg.sender];
}
}
function confirmAddress(string confirmationCodePlain, uint8 sigV, bytes32 sigR, bytes32 sigS)
public
{
require(bytes(confirmationCodePlain).length > 0);
require(userExists(msg.sender));
bytes32 data = keccak256(
msg.sender,
confirmationCodePlain
);
require(signerIsValid(data, sigV, sigR, sigS));
bool found;
uint ai;
bool confirmed;
bytes32 keccakIdentifier;
(found, ai, confirmed, keccakIdentifier) = userAddressByConfirmationCode(
msg.sender,
keccak256(confirmationCodePlain)
);
require(found);
require(!confirmed);
registry.setClaim(msg.sender, keccakIdentifier, PhysicalAddressClaim.encode(block.number));
totalConfirmed += 1;
}
}