@evvm/testnet-contracts 2.1.3 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/contracts/evvm/Evvm.sol +13 -9
- package/contracts/evvm/lib/EvvmStorage.sol +2 -0
- package/contracts/evvm/lib/SignatureUtils.sol +13 -13
- package/contracts/nameService/NameService.sol +11 -8
- package/contracts/nameService/lib/ErrorsLib.sol +1 -1
- package/contracts/nameService/lib/SignatureUtils.sol +38 -39
- package/contracts/p2pSwap/P2PSwap.sol +20 -388
- package/contracts/p2pSwap/lib/SignatureUtils.sol +15 -16
- package/contracts/staking/Staking.sol +28 -17
- package/contracts/staking/lib/ErrorsLib.sol +0 -1
- package/contracts/staking/lib/SignatureUtils.sol +6 -34
- package/contracts/treasuryTwoChains/lib/SignatureUtils.sol +7 -7
- package/interfaces/IStaking.sol +1 -1
- package/library/Erc191TestBuilder.sol +57 -57
- package/library/EvvmService.sol +104 -0
- package/library/primitives/Math.sol +415 -0
- package/library/primitives/SignatureRecover.sol +42 -0
- package/library/utils/AdvancedStrings.sol +89 -0
- package/library/utils/SignatureUtil.sol +29 -0
- package/library/utils/service/AsyncNonceService.sol +34 -0
- package/library/utils/service/MakeServicePaymentOnEvvm.sol +49 -0
- package/library/utils/service/StakingServiceUtils.sol +37 -0
- package/library/utils/service/SyncNonceService.sol +18 -0
- package/package.json +1 -1
- package/contracts/evvm/EvvmLegacy.sol +0 -1553
- package/library/AdvancedStrings.sol +0 -77
- package/library/SignatureRecover.sol +0 -140
- package/library/StakingServiceHooks.sol +0 -116
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.20;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @dev Standard math utilities missing in the Solidity language.
|
|
8
|
+
*/
|
|
9
|
+
library Math {
|
|
10
|
+
/**
|
|
11
|
+
* @dev Muldiv operation overflow.
|
|
12
|
+
*/
|
|
13
|
+
error MathOverflowedMulDiv();
|
|
14
|
+
|
|
15
|
+
enum Rounding {
|
|
16
|
+
Floor, // Toward negative infinity
|
|
17
|
+
Ceil, // Toward positive infinity
|
|
18
|
+
Trunc, // Toward zero
|
|
19
|
+
Expand // Away from zero
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @dev Returns the addition of two unsigned integers, with an overflow flag.
|
|
24
|
+
*/
|
|
25
|
+
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
26
|
+
unchecked {
|
|
27
|
+
uint256 c = a + b;
|
|
28
|
+
if (c < a) return (false, 0);
|
|
29
|
+
return (true, c);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
|
|
35
|
+
*/
|
|
36
|
+
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
37
|
+
unchecked {
|
|
38
|
+
if (b > a) return (false, 0);
|
|
39
|
+
return (true, a - b);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
|
|
45
|
+
*/
|
|
46
|
+
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
47
|
+
unchecked {
|
|
48
|
+
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
|
|
49
|
+
// benefit is lost if 'b' is also tested.
|
|
50
|
+
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
|
|
51
|
+
if (a == 0) return (true, 0);
|
|
52
|
+
uint256 c = a * b;
|
|
53
|
+
if (c / a != b) return (false, 0);
|
|
54
|
+
return (true, c);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @dev Returns the division of two unsigned integers, with a division by zero flag.
|
|
60
|
+
*/
|
|
61
|
+
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
62
|
+
unchecked {
|
|
63
|
+
if (b == 0) return (false, 0);
|
|
64
|
+
return (true, a / b);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
|
|
70
|
+
*/
|
|
71
|
+
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
72
|
+
unchecked {
|
|
73
|
+
if (b == 0) return (false, 0);
|
|
74
|
+
return (true, a % b);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @dev Returns the largest of two numbers.
|
|
80
|
+
*/
|
|
81
|
+
function max(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
82
|
+
return a > b ? a : b;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @dev Returns the smallest of two numbers.
|
|
87
|
+
*/
|
|
88
|
+
function min(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
89
|
+
return a < b ? a : b;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @dev Returns the average of two numbers. The result is rounded towards
|
|
94
|
+
* zero.
|
|
95
|
+
*/
|
|
96
|
+
function average(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
97
|
+
// (a + b) / 2 can overflow.
|
|
98
|
+
return (a & b) + (a ^ b) / 2;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @dev Returns the ceiling of the division of two numbers.
|
|
103
|
+
*
|
|
104
|
+
* This differs from standard division with `/` in that it rounds towards infinity instead
|
|
105
|
+
* of rounding towards zero.
|
|
106
|
+
*/
|
|
107
|
+
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
108
|
+
if (b == 0) {
|
|
109
|
+
// Guarantee the same behavior as in a regular Solidity division.
|
|
110
|
+
return a / b;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// (a + b - 1) / b can overflow on addition, so we distribute.
|
|
114
|
+
return a == 0 ? 0 : (a - 1) / b + 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or
|
|
119
|
+
* denominator == 0.
|
|
120
|
+
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv) with further edits by
|
|
121
|
+
* Uniswap Labs also under MIT license.
|
|
122
|
+
*/
|
|
123
|
+
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
|
|
124
|
+
unchecked {
|
|
125
|
+
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
|
|
126
|
+
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
|
|
127
|
+
// variables such that product = prod1 * 2^256 + prod0.
|
|
128
|
+
uint256 prod0 = x * y; // Least significant 256 bits of the product
|
|
129
|
+
uint256 prod1; // Most significant 256 bits of the product
|
|
130
|
+
assembly {
|
|
131
|
+
let mm := mulmod(x, y, not(0))
|
|
132
|
+
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Handle non-overflow cases, 256 by 256 division.
|
|
136
|
+
if (prod1 == 0) {
|
|
137
|
+
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
|
|
138
|
+
// The surrounding unchecked block does not change this fact.
|
|
139
|
+
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
|
|
140
|
+
return prod0 / denominator;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Make sure the result is less than 2^256. Also prevents denominator == 0.
|
|
144
|
+
if (denominator <= prod1) {
|
|
145
|
+
revert MathOverflowedMulDiv();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
///////////////////////////////////////////////
|
|
149
|
+
// 512 by 256 division.
|
|
150
|
+
///////////////////////////////////////////////
|
|
151
|
+
|
|
152
|
+
// Make division exact by subtracting the remainder from [prod1 prod0].
|
|
153
|
+
uint256 remainder;
|
|
154
|
+
assembly {
|
|
155
|
+
// Compute remainder using mulmod.
|
|
156
|
+
remainder := mulmod(x, y, denominator)
|
|
157
|
+
|
|
158
|
+
// Subtract 256 bit number from 512 bit number.
|
|
159
|
+
prod1 := sub(prod1, gt(remainder, prod0))
|
|
160
|
+
prod0 := sub(prod0, remainder)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Factor powers of two out of denominator and compute largest power of two divisor of denominator.
|
|
164
|
+
// Always >= 1. See https://cs.stackexchange.com/q/138556/92363.
|
|
165
|
+
|
|
166
|
+
uint256 twos = denominator & (0 - denominator);
|
|
167
|
+
assembly {
|
|
168
|
+
// Divide denominator by twos.
|
|
169
|
+
denominator := div(denominator, twos)
|
|
170
|
+
|
|
171
|
+
// Divide [prod1 prod0] by twos.
|
|
172
|
+
prod0 := div(prod0, twos)
|
|
173
|
+
|
|
174
|
+
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
|
|
175
|
+
twos := add(div(sub(0, twos), twos), 1)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Shift in bits from prod1 into prod0.
|
|
179
|
+
prod0 |= prod1 * twos;
|
|
180
|
+
|
|
181
|
+
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
|
|
182
|
+
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
|
|
183
|
+
// four bits. That is, denominator * inv = 1 mod 2^4.
|
|
184
|
+
uint256 inverse = (3 * denominator) ^ 2;
|
|
185
|
+
|
|
186
|
+
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also
|
|
187
|
+
// works in modular arithmetic, doubling the correct bits in each step.
|
|
188
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^8
|
|
189
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^16
|
|
190
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^32
|
|
191
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^64
|
|
192
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^128
|
|
193
|
+
inverse *= 2 - denominator * inverse; // inverse mod 2^256
|
|
194
|
+
|
|
195
|
+
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
|
|
196
|
+
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
|
|
197
|
+
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
|
|
198
|
+
// is no longer required.
|
|
199
|
+
result = prod0 * inverse;
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
|
|
206
|
+
*/
|
|
207
|
+
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
|
|
208
|
+
uint256 result = mulDiv(x, y, denominator);
|
|
209
|
+
if (unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0) {
|
|
210
|
+
result += 1;
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded
|
|
217
|
+
* towards zero.
|
|
218
|
+
*
|
|
219
|
+
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
|
|
220
|
+
*/
|
|
221
|
+
function sqrt(uint256 a) internal pure returns (uint256) {
|
|
222
|
+
if (a == 0) {
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
|
|
227
|
+
//
|
|
228
|
+
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
|
|
229
|
+
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
|
|
230
|
+
//
|
|
231
|
+
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
|
|
232
|
+
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
|
|
233
|
+
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
|
|
234
|
+
//
|
|
235
|
+
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
|
|
236
|
+
uint256 result = 1 << (log2(a) >> 1);
|
|
237
|
+
|
|
238
|
+
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
|
|
239
|
+
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
|
|
240
|
+
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
|
|
241
|
+
// into the expected uint128 result.
|
|
242
|
+
unchecked {
|
|
243
|
+
result = (result + a / result) >> 1;
|
|
244
|
+
result = (result + a / result) >> 1;
|
|
245
|
+
result = (result + a / result) >> 1;
|
|
246
|
+
result = (result + a / result) >> 1;
|
|
247
|
+
result = (result + a / result) >> 1;
|
|
248
|
+
result = (result + a / result) >> 1;
|
|
249
|
+
result = (result + a / result) >> 1;
|
|
250
|
+
return min(result, a / result);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @notice Calculates sqrt(a), following the selected rounding direction.
|
|
256
|
+
*/
|
|
257
|
+
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
|
|
258
|
+
unchecked {
|
|
259
|
+
uint256 result = sqrt(a);
|
|
260
|
+
return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @dev Return the log in base 2 of a positive value rounded towards zero.
|
|
266
|
+
* Returns 0 if given 0.
|
|
267
|
+
*/
|
|
268
|
+
function log2(uint256 value) internal pure returns (uint256) {
|
|
269
|
+
uint256 result = 0;
|
|
270
|
+
unchecked {
|
|
271
|
+
if (value >> 128 > 0) {
|
|
272
|
+
value >>= 128;
|
|
273
|
+
result += 128;
|
|
274
|
+
}
|
|
275
|
+
if (value >> 64 > 0) {
|
|
276
|
+
value >>= 64;
|
|
277
|
+
result += 64;
|
|
278
|
+
}
|
|
279
|
+
if (value >> 32 > 0) {
|
|
280
|
+
value >>= 32;
|
|
281
|
+
result += 32;
|
|
282
|
+
}
|
|
283
|
+
if (value >> 16 > 0) {
|
|
284
|
+
value >>= 16;
|
|
285
|
+
result += 16;
|
|
286
|
+
}
|
|
287
|
+
if (value >> 8 > 0) {
|
|
288
|
+
value >>= 8;
|
|
289
|
+
result += 8;
|
|
290
|
+
}
|
|
291
|
+
if (value >> 4 > 0) {
|
|
292
|
+
value >>= 4;
|
|
293
|
+
result += 4;
|
|
294
|
+
}
|
|
295
|
+
if (value >> 2 > 0) {
|
|
296
|
+
value >>= 2;
|
|
297
|
+
result += 2;
|
|
298
|
+
}
|
|
299
|
+
if (value >> 1 > 0) {
|
|
300
|
+
result += 1;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
|
|
308
|
+
* Returns 0 if given 0.
|
|
309
|
+
*/
|
|
310
|
+
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
311
|
+
unchecked {
|
|
312
|
+
uint256 result = log2(value);
|
|
313
|
+
return result + (unsignedRoundsUp(rounding) && 1 << result < value ? 1 : 0);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @dev Return the log in base 10 of a positive value rounded towards zero.
|
|
319
|
+
* Returns 0 if given 0.
|
|
320
|
+
*/
|
|
321
|
+
function log10(uint256 value) internal pure returns (uint256) {
|
|
322
|
+
uint256 result = 0;
|
|
323
|
+
unchecked {
|
|
324
|
+
if (value >= 10 ** 64) {
|
|
325
|
+
value /= 10 ** 64;
|
|
326
|
+
result += 64;
|
|
327
|
+
}
|
|
328
|
+
if (value >= 10 ** 32) {
|
|
329
|
+
value /= 10 ** 32;
|
|
330
|
+
result += 32;
|
|
331
|
+
}
|
|
332
|
+
if (value >= 10 ** 16) {
|
|
333
|
+
value /= 10 ** 16;
|
|
334
|
+
result += 16;
|
|
335
|
+
}
|
|
336
|
+
if (value >= 10 ** 8) {
|
|
337
|
+
value /= 10 ** 8;
|
|
338
|
+
result += 8;
|
|
339
|
+
}
|
|
340
|
+
if (value >= 10 ** 4) {
|
|
341
|
+
value /= 10 ** 4;
|
|
342
|
+
result += 4;
|
|
343
|
+
}
|
|
344
|
+
if (value >= 10 ** 2) {
|
|
345
|
+
value /= 10 ** 2;
|
|
346
|
+
result += 2;
|
|
347
|
+
}
|
|
348
|
+
if (value >= 10 ** 1) {
|
|
349
|
+
result += 1;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
|
|
357
|
+
* Returns 0 if given 0.
|
|
358
|
+
*/
|
|
359
|
+
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
360
|
+
unchecked {
|
|
361
|
+
uint256 result = log10(value);
|
|
362
|
+
return result + (unsignedRoundsUp(rounding) && 10 ** result < value ? 1 : 0);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @dev Return the log in base 256 of a positive value rounded towards zero.
|
|
368
|
+
* Returns 0 if given 0.
|
|
369
|
+
*
|
|
370
|
+
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
|
|
371
|
+
*/
|
|
372
|
+
function log256(uint256 value) internal pure returns (uint256) {
|
|
373
|
+
uint256 result = 0;
|
|
374
|
+
unchecked {
|
|
375
|
+
if (value >> 128 > 0) {
|
|
376
|
+
value >>= 128;
|
|
377
|
+
result += 16;
|
|
378
|
+
}
|
|
379
|
+
if (value >> 64 > 0) {
|
|
380
|
+
value >>= 64;
|
|
381
|
+
result += 8;
|
|
382
|
+
}
|
|
383
|
+
if (value >> 32 > 0) {
|
|
384
|
+
value >>= 32;
|
|
385
|
+
result += 4;
|
|
386
|
+
}
|
|
387
|
+
if (value >> 16 > 0) {
|
|
388
|
+
value >>= 16;
|
|
389
|
+
result += 2;
|
|
390
|
+
}
|
|
391
|
+
if (value >> 8 > 0) {
|
|
392
|
+
result += 1;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
|
|
400
|
+
* Returns 0 if given 0.
|
|
401
|
+
*/
|
|
402
|
+
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
403
|
+
unchecked {
|
|
404
|
+
uint256 result = log256(value);
|
|
405
|
+
return result + (unsignedRoundsUp(rounding) && 1 << (result << 3) < value ? 1 : 0);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
|
|
411
|
+
*/
|
|
412
|
+
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
|
|
413
|
+
return uint8(rounding) % 2 == 1;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.0;
|
|
5
|
+
|
|
6
|
+
import {AdvancedStrings} from "@evvm/testnet-contracts/library/utils/AdvancedStrings.sol";
|
|
7
|
+
|
|
8
|
+
library SignatureRecover {
|
|
9
|
+
|
|
10
|
+
function recoverSigner(
|
|
11
|
+
string memory message,
|
|
12
|
+
bytes memory signature
|
|
13
|
+
) internal pure returns (address) {
|
|
14
|
+
bytes32 messageHash = keccak256(
|
|
15
|
+
abi.encodePacked(
|
|
16
|
+
"\x19Ethereum Signed Message:\n",
|
|
17
|
+
AdvancedStrings.uintToString(bytes(message).length),
|
|
18
|
+
message
|
|
19
|
+
)
|
|
20
|
+
);
|
|
21
|
+
(bytes32 r, bytes32 s, uint8 v) = splitSignature(signature);
|
|
22
|
+
return ecrecover(messageHash, v, r, s);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function splitSignature(
|
|
26
|
+
bytes memory signature
|
|
27
|
+
) internal pure returns (bytes32 r, bytes32 s, uint8 v) {
|
|
28
|
+
require(signature.length == 65, "Invalid signature length");
|
|
29
|
+
|
|
30
|
+
assembly {
|
|
31
|
+
r := mload(add(signature, 32))
|
|
32
|
+
s := mload(add(signature, 64))
|
|
33
|
+
v := byte(0, mload(add(signature, 96)))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Ensure signature is valid
|
|
37
|
+
if (v < 27) {
|
|
38
|
+
v += 27;
|
|
39
|
+
}
|
|
40
|
+
require(v == 27 || v == 28, "Invalid signature value");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.0;
|
|
5
|
+
|
|
6
|
+
import {Math} from "@evvm/testnet-contracts/library/primitives/Math.sol";
|
|
7
|
+
|
|
8
|
+
library AdvancedStrings {
|
|
9
|
+
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
|
|
10
|
+
|
|
11
|
+
function uintToString(uint256 value) internal pure returns (string memory) {
|
|
12
|
+
unchecked {
|
|
13
|
+
uint256 length = Math.log10(value) + 1;
|
|
14
|
+
string memory buffer = new string(length);
|
|
15
|
+
uint256 ptr;
|
|
16
|
+
/// @solidity memory-safe-assembly
|
|
17
|
+
assembly {
|
|
18
|
+
ptr := add(buffer, add(32, length))
|
|
19
|
+
}
|
|
20
|
+
while (true) {
|
|
21
|
+
ptr--;
|
|
22
|
+
/// @solidity memory-safe-assembly
|
|
23
|
+
assembly {
|
|
24
|
+
mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
|
|
25
|
+
}
|
|
26
|
+
value /= 10;
|
|
27
|
+
if (value == 0) break;
|
|
28
|
+
}
|
|
29
|
+
return buffer;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function addressToString(
|
|
34
|
+
address _address
|
|
35
|
+
) internal pure returns (string memory) {
|
|
36
|
+
bytes32 _bytes = bytes32(uint256(uint160(_address)));
|
|
37
|
+
bytes memory _string = new bytes(42);
|
|
38
|
+
_string[0] = "0";
|
|
39
|
+
_string[1] = "x";
|
|
40
|
+
for (uint256 i = 0; i < 20; i++) {
|
|
41
|
+
_string[2 + i * 2] = HEX_DIGITS[uint8(_bytes[i + 12] >> 4)];
|
|
42
|
+
_string[3 + i * 2] = HEX_DIGITS[uint8(_bytes[i + 12] & 0x0f)];
|
|
43
|
+
}
|
|
44
|
+
return string(_string);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function equal(
|
|
48
|
+
string memory a,
|
|
49
|
+
string memory b
|
|
50
|
+
) internal pure returns (bool) {
|
|
51
|
+
return
|
|
52
|
+
bytes(a).length == bytes(b).length &&
|
|
53
|
+
keccak256(bytes(a)) == keccak256(bytes(b));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function bytesToString(
|
|
57
|
+
bytes memory data
|
|
58
|
+
) internal pure returns (string memory) {
|
|
59
|
+
if (data.length == 0) {
|
|
60
|
+
return "0x";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
bytes memory result = new bytes(2 + data.length * 2);
|
|
64
|
+
result[0] = "0";
|
|
65
|
+
result[1] = "x";
|
|
66
|
+
|
|
67
|
+
for (uint256 i = 0; i < data.length; i++) {
|
|
68
|
+
result[2 + i * 2] = HEX_DIGITS[uint8(data[i] >> 4)];
|
|
69
|
+
result[3 + i * 2] = HEX_DIGITS[uint8(data[i] & 0x0f)];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return string(result);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function bytes32ToString(
|
|
76
|
+
bytes32 data
|
|
77
|
+
) internal pure returns (string memory) {
|
|
78
|
+
bytes memory result = new bytes(66);
|
|
79
|
+
result[0] = "0";
|
|
80
|
+
result[1] = "x";
|
|
81
|
+
|
|
82
|
+
for (uint256 i = 0; i < 32; i++) {
|
|
83
|
+
result[2 + i * 2] = HEX_DIGITS[uint8(data[i] >> 4)];
|
|
84
|
+
result[3 + i * 2] = HEX_DIGITS[uint8(data[i] & 0x0f)];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return string(result);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.0;
|
|
5
|
+
|
|
6
|
+
import {SignatureRecover} from "@evvm/testnet-contracts/library/primitives/SignatureRecover.sol";
|
|
7
|
+
import {AdvancedStrings} from "@evvm/testnet-contracts/library/utils/AdvancedStrings.sol";
|
|
8
|
+
|
|
9
|
+
library SignatureUtil {
|
|
10
|
+
function verifySignature(
|
|
11
|
+
uint256 evvmID,
|
|
12
|
+
string memory functionName,
|
|
13
|
+
string memory inputs,
|
|
14
|
+
bytes memory signature,
|
|
15
|
+
address expectedSigner
|
|
16
|
+
) internal pure returns (bool) {
|
|
17
|
+
return
|
|
18
|
+
SignatureRecover.recoverSigner(
|
|
19
|
+
string.concat(
|
|
20
|
+
AdvancedStrings.uintToString(evvmID),
|
|
21
|
+
",",
|
|
22
|
+
functionName,
|
|
23
|
+
",",
|
|
24
|
+
inputs
|
|
25
|
+
),
|
|
26
|
+
signature
|
|
27
|
+
) == expectedSigner;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.0;
|
|
5
|
+
|
|
6
|
+
abstract contract AsyncNonceService {
|
|
7
|
+
|
|
8
|
+
error ServiceAsyncNonceAlreadyUsed();
|
|
9
|
+
|
|
10
|
+
mapping(address user => mapping(uint256 nonce => bool availability))
|
|
11
|
+
private asyncServiceNonce;
|
|
12
|
+
|
|
13
|
+
function markAsyncServiceNonceAsUsed(
|
|
14
|
+
address user,
|
|
15
|
+
uint256 nonce
|
|
16
|
+
) internal virtual {
|
|
17
|
+
asyncServiceNonce[user][nonce] = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function verifyAsyncServiceNonce(
|
|
21
|
+
address user,
|
|
22
|
+
uint256 nonce
|
|
23
|
+
) internal view virtual {
|
|
24
|
+
if (asyncServiceNonce[user][nonce])
|
|
25
|
+
revert ServiceAsyncNonceAlreadyUsed();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isAsyncServiceNonceAvailable(
|
|
29
|
+
address user,
|
|
30
|
+
uint256 nonce
|
|
31
|
+
) public view virtual returns (bool) {
|
|
32
|
+
return asyncServiceNonce[user][nonce];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
|
|
4
|
+
pragma solidity ^0.8.0;
|
|
5
|
+
|
|
6
|
+
import {IEvvm} from "@evvm/testnet-contracts/interfaces/IEvvm.sol";
|
|
7
|
+
|
|
8
|
+
abstract contract MakeServicePaymentOnEvvm {
|
|
9
|
+
IEvvm public evvm;
|
|
10
|
+
|
|
11
|
+
constructor(address evvmAddress) {
|
|
12
|
+
evvm = IEvvm(evvmAddress);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function makePay(
|
|
16
|
+
address from,
|
|
17
|
+
address token,
|
|
18
|
+
uint256 amount,
|
|
19
|
+
uint256 priorityFee,
|
|
20
|
+
uint256 nonce,
|
|
21
|
+
bool priorityFlag,
|
|
22
|
+
bytes memory signature
|
|
23
|
+
) internal virtual {
|
|
24
|
+
evvm.pay(
|
|
25
|
+
from,
|
|
26
|
+
address(this),
|
|
27
|
+
"",
|
|
28
|
+
token,
|
|
29
|
+
amount,
|
|
30
|
+
priorityFee,
|
|
31
|
+
nonce,
|
|
32
|
+
priorityFlag,
|
|
33
|
+
address(this),
|
|
34
|
+
signature
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function makeCaPay(
|
|
39
|
+
address to,
|
|
40
|
+
address token,
|
|
41
|
+
uint256 amount
|
|
42
|
+
) internal virtual {
|
|
43
|
+
evvm.caPay(to, token, amount);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _changeEvvmAddress(address newEvvmAddress) internal {
|
|
47
|
+
evvm = IEvvm(newEvvmAddress);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EVVM-NONCOMMERCIAL-1.0
|
|
2
|
+
// Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
|
|
3
|
+
pragma solidity ^0.8.0;
|
|
4
|
+
|
|
5
|
+
import {IEvvm} from "@evvm/testnet-contracts/interfaces/IEvvm.sol";
|
|
6
|
+
import {IStaking} from "@evvm/testnet-contracts/interfaces/IStaking.sol";
|
|
7
|
+
|
|
8
|
+
abstract contract StakingServiceUtils {
|
|
9
|
+
address stakingHookAddress;
|
|
10
|
+
address evvmHookAddress;
|
|
11
|
+
constructor(address _stakingAddress) {
|
|
12
|
+
stakingHookAddress = _stakingAddress;
|
|
13
|
+
evvmHookAddress = IStaking(stakingHookAddress).getEvvmAddress();
|
|
14
|
+
}
|
|
15
|
+
function _makeStakeService(uint256 amountToStake) internal {
|
|
16
|
+
IStaking(stakingHookAddress).prepareServiceStaking(amountToStake);
|
|
17
|
+
IEvvm(evvmHookAddress).caPay(
|
|
18
|
+
address(stakingHookAddress),
|
|
19
|
+
0x0000000000000000000000000000000000000001,
|
|
20
|
+
IStaking(stakingHookAddress).priceOfStaking() * amountToStake
|
|
21
|
+
);
|
|
22
|
+
IStaking(stakingHookAddress).confirmServiceStaking();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function _makeUnstakeService(uint256 amountToUnstake) internal {
|
|
26
|
+
IStaking(stakingHookAddress).serviceUnstaking(amountToUnstake);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _changeStakingAddress(address newStakingAddress) internal {
|
|
30
|
+
stakingHookAddress = newStakingAddress;
|
|
31
|
+
evvmHookAddress = IStaking(stakingHookAddress).getEvvmAddress();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function _changeEvvmHookAddress(address newEvvmAddress) internal {
|
|
35
|
+
evvmHookAddress = newEvvmAddress;
|
|
36
|
+
}
|
|
37
|
+
}
|