-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddress2PublicKey.sol
More file actions
71 lines (60 loc) · 2.33 KB
/
Address2PublicKey.sol
File metadata and controls
71 lines (60 loc) · 2.33 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "./utils/Address.sol";
import "./access/Ownable.sol";
contract PubllicKey is Ownable{
using Address for address;
mapping(address => string) pubKeys;
/*bytes encryptData;
constructor(){
encryptData = new bytes(72);
bytes28 prefix = 0x19457468657265756d205369676e6564204d6573736167653a0a3434;
for(uint i=0;i<prefix.length;i++){
encryptData[i] = prefix[i];
}
}
//This way cost more gas.
function storePubKey(string memory _pubKey,bytes calldata signature,address _keyOwner) public returns(bool){
bytes memory pubKey = bytes(_pubKey);
bytes memory _encryptData = encryptData;
for(uint i=0;i<pubKey.length;i++){
_encryptData[i+28] = pubKey[i];
}
bytes32 hash = keccak256(_encryptData);
bytes32 r = bytesToBytes32(signature[0:32]);
bytes32 s = bytesToBytes32(signature[32:64]);
bytes1 bv = signature[64:65][0];
uint8 v = uint8(bv);
address addr=ecrecover(hash, v, r, s);
assert(_keyOwner == addr);
pubKeys[addr] = _pubKey;
return true;
}*/
function storePubKey(bytes calldata _pubKey,bytes calldata signature,address _keyOwner) public returns(bool){
bytes32 hash = keccak256(_pubKey);
bytes32 r = bytesToBytes32(signature[0:32]);
bytes32 s = bytesToBytes32(signature[32:64]);
bytes1 bv = signature[64:65][0];
uint8 v = uint8(bv);
address addr=ecrecover(hash, v, r, s);
assert(_keyOwner == addr);
bytes memory pubKey = _pubKey[28:_pubKey.length];
pubKeys[addr] = string(pubKey);
return true;
}
function getPublicKey(address addr) view public returns(string memory){
if(addr.isContract()) {
}
return pubKeys[addr];
}
function destruct(bytes32 hash,bytes calldata signature) public {
bytes32 r = bytesToBytes32(signature[0:32]);
bytes32 s = bytesToBytes32(signature[32:64]);
bytes1 bv = signature[64:65][0];
uint8 v = uint8(bv);
address addr=ecrecover(hash, v, r, s);
require(addr == owner());
address payable executor = payable(addr);
selfdestruct(executor);
}
}