-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.sol
102 lines (85 loc) · 2.97 KB
/
project.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
96
97
98
99
100
101
102
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity 0.8.13;
contract ChatApp {
struct user {
string name;
friend[] friendList;
}
struct friend {
address pubkey;
string name;
}
struct message {
address sender;
uint256 timestamp;
string msg;
}
mapping(address => user) userList;
mapping(bytes32 => message[]) allMessages;
//Check is user Exist
function checkUserExists(address pubkey) public view returns (bool) {
return bytes(userList[pubkey].name).length > 0;
}
//Create Account
function createAccount(string calldata name) external {
require(checkUserExists(msg.sender) == false, "User Already Exist");
require(bytes(name).length > 0, "Username cannot be empty");
userList[msg.sender].name = name;
}
//Get Username
function getUsername(address pubkey) external view returns (string memory) {
require(checkUserExists(pubkey), "User is not registered");
return userList[pubkey].name;
}
//Add Friend
function addFriend(address friend_key, string calldata name) external {
require(checkUserExists(msg.sender), "Please create an account");
require(checkUserExists(friend_key), "User is not registered");
require(msg.sender != friend_key, "user can not themselves as friend");
require(
checkAlredyFriend(msg.sender, friend_key) == false,
"This users are already friends"
);
_addFriend(msg.sender, friend_key, name);
_addFriend(friend_key, msg.sender, userList[msg.sender].name);
}
//Check if we are friend already
function checkAlredyFriend(
address pubkey1,
address pubkey2
) internal view returns (bool) {
if (
userList[pubkey1].friendList.length >
userList[pubkey2].friendList.length
) {
address tmp = pubkey1;
pubkey1 = pubkey2;
pubkey2 = tmp;
}
for (uint256 i = 0; i < userList[pubkey1].friendList.length; i++) {
if (userList[pubkey1].friendList[i].pubkey == pubkey2) return true;
}
return false;
}
function _addFriend(
address me,
address friend_key,
string memory name
) internal {
friend memory newFriend = friend(friend_key, name);
userList[me].friendList.push(newFriend);
}
//Get my Friend
function getMyFriendList() external view returns (friend[] memory) {
return userList[msg.sender].friendList;
}
//Get Chat Code
function _getChatCode(
address pubkey1,
address pubkey2
) internal pure returns (bytes32) {
if (pubkey1 < pubkey2) {
return keccak256(abi.encodePacked(pubkey1, pubkey2));
} else return keccak256(abi.encodePacked(pubkey2, pubkey1));
}
}