-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserHandle.sol
95 lines (81 loc) · 2.63 KB
/
UserHandle.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract appUserHandles {
struct OwnerHandle {
address owner;
string userHandle;
}
OwnerHandle[] private handles;
OwnerHandle handlelocal;
function addHandle(string memory _handle) public returns (string memory) {
if (checkAddress(msg.sender) || checkHandle(_handle)) {
return "Handle or address already exists.";
} else {
handlelocal.owner = msg.sender;
handlelocal.userHandle = _handle;
handles.push(handlelocal);
return "Done";
}
}
function fetchHandle() public view returns (string memory) {
bool addExists = checkAddress(msg.sender);
address tempAdd;
string memory foundHandle;
if (addExists == true) {
for (uint256 i = 0; i < handles.length; i++) {
tempAdd = handles[i].owner;
if (tempAdd == msg.sender) {
foundHandle = handles[i].userHandle;
}
}
} else {
foundHandle = "#DNE";
}
return foundHandle;
}
function fetchAddress(string memory _handle) public view returns (address) {
bool handleExists = checkHandle(_handle);
address foundAdd;
string memory tempHandle;
if (handleExists == true) {
for (uint256 i = 0; i < handles.length; i++) {
tempHandle = handles[i].userHandle;
if (keccak256(abi.encodePacked(tempHandle)) == keccak256(abi.encodePacked(_handle))) {
foundAdd = handles[i].owner;
}
}
} else {
foundAdd;
}
return foundAdd;
}
function checkAddress(address _checkAddress) private view returns (bool) {
bool addressFound = false;
address tempAdd;
for (uint256 i = 0; i < handles.length && !addressFound; i++) {
tempAdd = handles[i].owner;
if (tempAdd == _checkAddress) {
addressFound = true;
}
}
return addressFound;
}
function checkHandle(string memory _checkHandle)
public
view
returns (bool)
{
bool handleFound = false;
string memory tempHandle;
for (uint256 i = 0; i < handles.length && !handleFound; i++) {
tempHandle = handles[i].userHandle;
if (
keccak256(abi.encodePacked(tempHandle)) ==
keccak256(abi.encodePacked(_checkHandle))
) {
handleFound = true;
}
}
return handleFound;
}
}