-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.sol
49 lines (38 loc) · 1.48 KB
/
Auth.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Auth {
// Written by Alex Beregszaszi (@axic), use it under the terms of the MIT license.
// slightly modified
function verifySig(address account, bytes32 hash, bytes memory sig) public pure returns (bool) {
bytes32 r;
bytes32 s;
uint8 v;
if (sig.length != 65){
return false;
//throw;
}
// The signature format is a compact form of:
// {bytes32 r}{bytes32 s}{uint8 v}
// Compact means, uint8 is not padded to 32 bytes.
assembly {
r := mload(add(sig, 32))
s := mload(add(sig, 64))
// Here we are loading the last 32 bytes, including 31 bytes
// of 's'. There is no 'mload8' to do this.
//
// 'byte' is not working due to the Solidity parser, so lets
// use the second best option, 'and'
v := and(mload(add(sig, 65)), 255)
}
// old geth sends a `v` value of [0,1], while the new, in line with the YP sends [27,28]
if (v < 27)
v += 27;
return verify(account, hash, v, r, s);
}
function getAddress(bytes32 hash, uint8 v, bytes32 r, bytes32 s) public pure returns(address) {
return ecrecover(hash, v, r, s);
}
function verify(address account, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public pure returns(bool) {
return account == getAddress(hash, v, r, s);
}
}