-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_template.sol
More file actions
430 lines (355 loc) · 11.6 KB
/
_template.sol
File metadata and controls
430 lines (355 loc) · 11.6 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
pragma solidity ^0.5.10;
/*******************************************************************************
*
* Copyright (c) 2024 Ava's DAO.
* Released under the MIT License.
*
* ServiceName - Service description.
*
* Version YY.MM.DD
*
* https://avasdao.org
* support@avasdao.org
*/
/*******************************************************************************
*
* SafeMath
*/
library SafeMath {
function add(uint a, uint b) internal pure returns (uint c) {
c = a + b;
require(c >= a);
}
function sub(uint a, uint b) internal pure returns (uint c) {
require(b <= a);
c = a - b;
}
function mul(uint a, uint b) internal pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function div(uint a, uint b) internal pure returns (uint c) {
require(b > 0);
c = a / b;
}
}
/*******************************************************************************
*
* ECRecovery
*
* Contract function to validate signature of pre-approved token transfers.
* (borrowed from LavaWallet)
*/
contract ECRecovery {
function recover(bytes32 hash, bytes memory sig) public pure returns (address);
}
/*******************************************************************************
*
* ERC Token Standard #20 Interface
* https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
*/
contract ERC20Interface {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
/*******************************************************************************
*
* ApproveAndCallFallBack
*
* Contract function to receive approval and execute function in one call
* (borrowed from MiniMeToken)
*/
contract ApproveAndCallFallBack {
function approveAndCall(address spender, uint tokens, bytes memory data) public;
function receiveApproval(address from, uint256 tokens, address token, bytes memory data) public;
}
/*******************************************************************************
*
* Owned contract
*/
contract Owned {
address public owner;
address public newOwner;
event OwnershipTransferred(address indexed _from, address indexed _to);
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
require(msg.sender == newOwner);
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = address(0);
}
}
/*******************************************************************************
*
* EternalDb Interface
*/
contract IEternalDb {
/* Interface getters. */
function getAddress(bytes32 _key) external view returns (address);
function getBool(bytes32 _key) external view returns (bool);
function getBytes(bytes32 _key) external view returns (bytes memory);
function getInt(bytes32 _key) external view returns (int);
function getString(bytes32 _key) external view returns (string memory);
function getUint(bytes32 _key) external view returns (uint);
/* Interface setters. */
function setAddress(bytes32 _key, address _value) external;
function setBool(bytes32 _key, bool _value) external;
function setBytes(bytes32 _key, bytes calldata _value) external;
function setInt(bytes32 _key, int _value) external;
function setString(bytes32 _key, string calldata _value) external;
function setUint(bytes32 _key, uint _value) external;
/* Interface deletes. */
function deleteAddress(bytes32 _key) external;
function deleteBool(bytes32 _key) external;
function deleteBytes(bytes32 _key) external;
function deleteInt(bytes32 _key) external;
function deleteString(bytes32 _key) external;
function deleteUint(bytes32 _key) external;
}
/*******************************************************************************
*
* @notice Service Name
*
* @dev Developer details.
*/
contract ServiceName is Owned {
using SafeMath for uint;
/* Initialize predecessor contract. */
address private _predecessor;
/* Initialize successor contract. */
address private _successor;
/* Initialize revision number. */
uint private _revision;
/* Initialize EternalDb contract. */
IEternalDb private _eternalDb;
/**
* Set Namespace
*
* Provides a "unique" name for generating "unique" data identifiers,
* most commonly used as database "key-value" keys.
*
* NOTE: Use of `namespace` is REQUIRED when generating ANY & ALL
* EternalDb keys; in order to prevent ANY accidental or
* malicious SQL-injection vulnerabilities / attacks.
*/
string private _namespace = 'SERVICE_NAME_HERE';
event Broadcast(
address indexed primary,
address secondary,
bytes data
);
/***************************************************************************
*
* Constructor
*/
constructor() public {
/* Initialize EternalDb (eternal) storage database contract. */
// NOTE We hard-code the address here, since it should never change.
_eternalDb = IEternalDb(0x0000000000000000000000000000000000000000);
/* Initialize (aname) hash. */
bytes32 hash = keccak256(abi.encodePacked('aname.', _namespace));
/* Set predecessor address. */
_predecessor = _eternalDb.getAddress(hash);
/* Verify predecessor address. */
if (_predecessor != address(0x0)) {
/* Make address payable. */
address payable predecessor = address(uint160(_predecessor));
/* Retrieve the last revision number (if available). */
uint lastRevision = ServiceName(predecessor).getRevision();
/* Set (current) revision number. */
_revision = lastRevision + 1;
}
}
/**
* @dev Only allow access to an authorized Modenero administrator.
*/
modifier onlyAuthByModenero() {
/* Verify write access is only permitted to authorized accounts. */
require(_eternalDb.getBool(keccak256(
abi.encodePacked(msg.sender, '.has.auth.for.', _namespace))) == true);
_; // function code is inserted here
}
/**
* THIS CONTRACT DOES NOT ACCEPT DIRECT ETHER
*/
function () external payable {
/* Cancel this transaction. */
revert('Oops! Direct payments are NOT permitted here.');
}
/***************************************************************************
*
* ACTIONS
*
*/
function firstAction() public pure {
}
/***************************************************************************
*
* GETTERS
*
*/
/**
* Get Revision (Number)
*/
function getRevision() public view returns (uint) {
return _revision;
}
/**
* Get Predecessor (Address)
*/
function getPredecessor() public view returns (address) {
return _predecessor;
}
/**
* Get Successor (Address)
*/
function getSuccessor() public view returns (address) {
return _successor;
}
/***************************************************************************
*
* SETTERS
*
*/
/**
* Set Successor
*
* This is the contract address that replaced this current instnace.
*/
function setSuccessor(
address _newSuccessor
) onlyAuthByModenero external returns (bool success) {
/* Set successor contract. */
_successor = _newSuccessor;
/* Return success. */
return true;
}
/***************************************************************************
*
* INTERFACES
*
*/
/**
* Supports Interface (EIP-165)
*
* (see: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md)
*
* NOTE: Must support the following conditions:
* 1. (true) when interfaceID is 0x01ffc9a7 (EIP165 interface)
* 2. (false) when interfaceID is 0xffffffff
* 3. (true) for any other interfaceID this contract implements
* 4. (false) for any other interfaceID
*/
function supportsInterface(
bytes4 _interfaceID
) external pure returns (bool) {
/* Initialize constants. */
bytes4 InvalidId = 0xffffffff;
bytes4 ERC165Id = 0x01ffc9a7;
/* Validate condition #2. */
if (_interfaceID == InvalidId) {
return false;
}
/* Validate condition #1. */
if (_interfaceID == ERC165Id) {
return true;
}
// TODO Add additional interfaces here.
/* Return false (for condition #4). */
return false;
}
/**
* ECRecovery Interface
*/
function _ecRecovery() private view returns (
ECRecovery ecrecovery
) {
/* Initialize hash. */
bytes32 hash = keccak256('aname.ecrecovery');
/* Retrieve value from EternalDb. */
address aname = _eternalDb.getAddress(hash);
/* Initialize interface. */
ecrecovery = ECRecovery(aname);
}
/***************************************************************************
*
* UTILITIES
*
*/
/**
* Is (Owner) Contract
*
* Tests if a specified account / address is a contract.
*/
function _ownerIsContract(
address _owner
) private view returns (bool isContract) {
/* Initialize code length. */
uint codeLength;
/* Run assembly. */
assembly {
/* Retrieve the size of the code on target address. */
codeLength := extcodesize(_owner)
}
/* Set test result. */
isContract = (codeLength > 0);
}
/**
* Bytes-to-Address
*
* Converts bytes into type address.
*/
function _bytesToAddress(
bytes memory _address
) private pure returns (address) {
uint160 m = 0;
uint160 b = 0;
for (uint8 i = 0; i < 20; i++) {
m *= 256;
// b = uint160(_address[i]); // NOTE: This line broke in v0.5.0
b = uint160(uint8(_address[i])); // NOTE: This HAS NOT been tested yet.
m += (b);
}
return address(m);
}
/**
* Convert Bytes to Bytes32
*/
function _bytesToBytes32(
bytes memory _data,
uint _offset
) private pure returns (bytes32 result) {
/* Loop through each byte. */
for (uint i = 0; i < 32; i++) {
/* Shift bytes onto result. */
result |= bytes32(_data[i + _offset] & 0xFF) >> (i * 8);
}
}
/**
* Convert Bytes32 to Bytes
*
* NOTE: Since solidity v0.4.22, you can use `abi.encodePacked()` for this,
* which returns bytes. (https://ethereum.stackexchange.com/a/55963)
*/
function _bytes32ToBytes(
bytes32 _data
) private pure returns (bytes memory result) {
/* Pack the data. */
return abi.encodePacked(_data);
}
}