-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.sol
More file actions
84 lines (67 loc) · 3.04 KB
/
Copy pathDatabase.sol
File metadata and controls
84 lines (67 loc) · 3.04 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
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Database {
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;
function checkUserExists(address pubkey) public view returns(bool) {
return bytes(userList[pubkey].name).length > 0;
}
function createAccount(string calldata name) external {
require(!checkUserExists(msg.sender), "User already exists!");
require(bytes(name).length > 0, "Username cannot be empty!");
userList[msg.sender].name = name;
}
function getUsername(address pubkey) external view returns(string memory) {
require(checkUserExists(pubkey), "User is not registered!");
return userList[pubkey].name;
}
function addFriend(address friend_key, string calldata name) external {
require(checkUserExists(msg.sender), "Create an account first!");
require(checkUserExists(friend_key), "User is not registered!");
require(msg.sender != friend_key, "Users cannot add themselves as friends!");
require(!checkAlreadyFriends(msg.sender, friend_key), "These users are already friends!");
_addFriend(msg.sender, friend_key, name);
_addFriend(friend_key, msg.sender, userList[msg.sender].name);
}
function checkAlreadyFriends(address pubkey1, address pubkey2) internal view returns(bool) {
for (uint 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);
}
function getMyFriendList() external view returns(friend[] memory) {
return userList[msg.sender].friendList;
}
function _getChatCode(address pubkey1, address pubkey2) internal pure returns(bytes32) {
return pubkey1 < pubkey2 ? keccak256(abi.encodePacked(pubkey1, pubkey2)) : keccak256(abi.encodePacked(pubkey2, pubkey1));
}
function sendMessage(address friend_key, string calldata _msg) external {
require(checkUserExists(msg.sender), "Create an account first!");
require(checkUserExists(friend_key), "User is not registered!");
require(checkAlreadyFriends(msg.sender, friend_key), "You are not friends with the given user");
bytes32 chatCode = _getChatCode(msg.sender, friend_key);
message memory newMsg = message(msg.sender, block.timestamp, _msg);
allMessages[chatCode].push(newMsg);
}
function readMessage(address friend_key) external view returns(message[] memory) {
bytes32 chatCode = _getChatCode(msg.sender, friend_key);
return allMessages[chatCode];
}
}