-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxtrs.sol
321 lines (250 loc) · 10.8 KB
/
txtrs.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
*Submitted for verification at Etherscan.io on 2019-12-22
*/
pragma solidity ^0.4.26;
pragma experimental ABIEncoderV2;
contract PublicMessage{
string message;
address sender;
uint timestamp;
function PublicMessage(string _message, address _sender){
message = _message;
sender=_sender;
timestamp = block.timestamp;
}
function get_message() returns (string){
return message;
}
function get_message_sender() returns (address){
return sender;
}
function get_message_timestamp() returns (uint){
return timestamp;
}
}
contract PrivateMessage{
string public encrypted_message;
address public alice;
address public bob;
string public bob_public;
uint256 public stage;
bool public bob_finalized;
bool public bob_error;
event NewPrivateMessage(address PrivateMessage, address alice, address bob);
function PrivateMessage(address _alice, address _bob){ // initiated by alice
//privte messages are initialized by alice along side bob's address and her one time address she created
require(stage==0);
require(msg.sender != _bob);
stage=1;
bob=_bob;
alice = _alice;
emit NewPrivateMessage(address(this), alice, bob);
}
event NewBobReply(address PrivateMessage,address alice, address bob, string bob_public);
function bob_reply(string _bob_public) public{
require(stage==1);
stage=2;
require(tx.origin == bob);
bob_public=_bob_public;
emit NewBobReply(address(this),alice, bob, bob_public);
}
event NewAliceSendEncryptedMessage(address PrivateMessage, address alice, address bob, string bob_public, string encrypted_message);
function alice_send_encrypted_message (string _encrypted_message) public{
require(stage==2);
stage=3;
require(tx.origin == alice);
encrypted_message=_encrypted_message;
emit NewAliceSendEncryptedMessage(address(this), alice, bob, bob_public,encrypted_message);
}
event NewBobFinal(address PrivateMessage, address alice, address bob, bool bob_error);
function bob_final(bool _error) public{
require(stage==3);
stage=4;
require(tx.origin == bob);
bob_finalized=true; // don't need this because stage=4 is the same
bob_error=_error;
emit NewBobFinal(address(this), alice, bob, bob_error);
}
}
contract Message{
string message;
address sender;
address recipient;
function Message(string _message, address _sender, address _recipient ){
message = _message;
sender=_sender;
recipient=_recipient;
}
function get_message() returns (string){
return message;
}
function get_message_sender() returns (address){
return sender;
}
function get_message_recipient() returns (address){
return recipient;
}
}
contract Txtrs {
struct Txtr {
bytes32 name; // short name (up to 32 bytes)
address owner;
address[] all_sent_messages;
address[] all_received_messages;
address[] all_public_messages;
address[] private_messages;
bool exists;
}
mapping(address => Txtr) public txtrs;
address[] txtrs_list;
address[] public_messages;
uint256 public_messages_length;
//todo function for changing ownership of a username
//todo have Txtr be erc721 token
event NewPublicMessage(address message_addr, string message, address indexed _from);
event NewTxtr(address indexed _from);
//private message events:
event NewPrivateMessage(address PrivateMessage, address alice, address bob);
event NewBobReply2(address PrivateMessage,address alice, address bob, string bob_public);
event NewAliceSendEncryptedMessage(address PrivateMessage, address alice, address bob, string bob_public, string encrypted_message);
event NewBobFinal(address PrivateMessage, address alice, address bob, bool bob_error);
function pm_init(address bob ) returns (address){
address alice = msg.sender;
address message = address( new PrivateMessage ({_alice:alice,_bob:bob}));
if(!txtrs[alice].exists){
create_txtr_on_receive(alice);
}
if(!txtrs[bob].exists){
create_txtr_on_receive(bob);
}
txtrs[alice].all_sent_messages.push(message);
txtrs[bob].all_received_messages.push(message);
//add to private messages array TODO is remove rec/sent arrays
txtrs[alice].private_messages.push(message);
if(alice!=bob){
txtrs[bob].private_messages.push(message);
}
emit NewPrivateMessage(message, alice, bob);
return message;
}
function pm_bob_reply(address pm,string bob_public){
PrivateMessage my_pm = PrivateMessage(pm);
my_pm.bob_reply(bob_public);
emit NewBobReply2(pm,my_pm.alice(), my_pm.bob(),bob_public);
}
function pm_alice_send_encrypted_message(address pm, string _encrypted_message){
PrivateMessage my_pm = PrivateMessage(pm);
my_pm.alice_send_encrypted_message(_encrypted_message);
emit NewAliceSendEncryptedMessage(pm, my_pm.alice(), my_pm.bob(),my_pm.bob_public(), _encrypted_message);
}
function pm_bob_final(address pm, bool _error){
PrivateMessage my_pm = PrivateMessage(pm);
my_pm.bob_final(_error);
emit NewBobFinal(pm, my_pm.alice(), my_pm.bob(), _error);
}
function send_public_message(string message_text) returns (address){
address message = address(new PublicMessage({_message:message_text, _sender:msg.sender }));
if(!txtrs[msg.sender].exists){
txtrs[msg.sender]=create_txtr_on_receive(msg.sender);
emit NewTxtr(msg.sender);
}
txtrs[msg.sender].all_public_messages.push(message);
public_messages.push(message);
public_messages_length+=1;
emit NewPublicMessage(message, message_text,msg.sender);
return message;
}
function get_public_message_count() returns (uint256){
return public_messages.length;
}
function get_public_message_address(uint256 index) returns (address){
return public_messages[index];
}
function get_public_message_message(uint256 index) returns (string){
address addr = public_messages[index];
PublicMessage message = PublicMessage(addr);
return message.get_message();
}
function get_public_message_sender(uint256 index) returns (address){
address addr = public_messages[index];
PublicMessage message = PublicMessage(addr);
return message.get_message_sender();
}
function create_txtr_on_public_message(address addr) private returns (Txtr){
address[] storage all_sent_messages;
address[] storage all_received_messages;
address[] storage all_public_messages;
address[] storage private_messages;
txtrs_list.push(addr);
return Txtr({name:'', owner:addr, all_sent_messages: all_sent_messages,all_received_messages:all_received_messages,all_public_messages:all_public_messages,private_messages: private_messages,exists:true });
}
function create_txtr_on_receive(address addr) private returns (Txtr){
address[] storage all_sent_messages;
address[] storage all_received_messages;
address[] storage all_public_messages;
address[] storage private_messages;
txtrs_list.push(addr);
return Txtr({name:'', owner:addr, all_sent_messages: all_sent_messages,all_received_messages:all_received_messages,all_public_messages:all_public_messages,private_messages:private_messages,exists:true });
}
function create_txtr(bytes32 username) {
address[] storage all_sent_messages;
address[] storage all_received_messages;
address[] storage all_public_messages;
address[] storage private_messages;
txtrs[msg.sender] = Txtr({name:username, owner:msg.sender, all_sent_messages: all_sent_messages,all_received_messages:all_received_messages,all_public_messages:all_public_messages, private_messages:private_messages,exists:true });
txtrs_list.push(msg.sender);
}
function create_message(string message_text, address recipient) returns (address){
address message = address(new Message({_message:message_text, _sender:msg.sender, _recipient:recipient }));
if(!txtrs[msg.sender].exists){
txtrs[msg.sender]=create_txtr_on_receive(msg.sender);
}
txtrs[msg.sender].all_sent_messages.push(message);
if(!txtrs[recipient].exists){
txtrs[recipient]=create_txtr_on_receive(recipient);
}
txtrs[recipient].all_received_messages.push(message);
return message;
}
function get_received_messages_total(address addr) public returns (uint256) {
return txtrs[addr].all_received_messages.length;
}
function get_received_message(address addr, uint256 index) public returns (address) {
return address(txtrs[addr].all_received_messages[index]);
}
function get_sent_messages_total(address addr) public returns (uint256) {
return txtrs[addr].all_sent_messages.length;
}
function get_sent_message(address addr, uint256 index) public returns (address) {
return address(txtrs[addr].all_sent_messages[index]);
}
function get_private_messages_total(address addr) public returns (uint256) {
return txtrs[addr].private_messages.length;
}
function get_private_message(address addr, uint256 index) public returns (address) {
return address(txtrs[addr].private_messages[index]);
}
function get_one_message(uint256 index, address addr) public returns (string) {
address _msg = txtrs[addr].all_received_messages[index];
Message message = Message(_msg);
return message.get_message();
}
function get_one_message_sender(uint256 index, address addr) public returns (address) {
address _msg = txtrs[addr].all_received_messages[index];
Message message = Message(_msg);
return message.get_message_sender();
}
function get_one_message_recipient(uint256 index, address addr) public returns (address) {
address _msg = txtrs[addr].all_received_messages[index];
Message message = Message(_msg);
return message.get_message_recipient();
}
function get_sent_user_message_total(address addr) public returns (uint256) {
return txtrs[addr].all_sent_messages.length;
}
function get_one_sent_message(uint256 index, address addr) public returns (string) {
address _msg = txtrs[addr].all_sent_messages[index];
Message message = Message(_msg);
return message.get_message();
}
}