forked from mwherman2000/csharp-smart-contract-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5_domain.cs
194 lines (172 loc) · 5.85 KB
/
lab5_domain.cs
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
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;
// Testing:
// operation, args
// ---------, --------------------------------------------------
// "query", ["test.com"]
// "register", ["test.com", "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]
// "delete", ["test.com"]
// "transfer", ["test.com", "AZ9Bmz6qmboZ4ry1z8p2KF3ftyA2ckJAym"]
namespace lab5_domain
{
public class lab5_domain : SmartContract
{
public static object Main(string operation, params object[] args)
{
object result = false; // = 0 (zero)
if (args.Length == 0)
{
Runtime.Log("No domain named supplied");
result = 0;
}
else
{
string domain_name = (string)args[0];
if (operation == "query")
{
result = QueryDomain(domain_name);
}
else if (operation == "delete")
{
result = DeleteDomain(domain_name);
}
else if (operation == "register")
{
if (args.Length < 2)
{
Runtime.Log("Required arguments: [\"domain_name\", \"owner\"]");
result = 0;
}
else
{
byte[] owner = (byte[])args[1];
result = RegisterDomain(domain_name, owner);
}
}
else if (operation == "transfer")
{
if (args.Length < 2)
{
Runtime.Log("Required arguments: [\"domain_name\", \"to_address\"]");
result = 0;
}
else
{
byte[] to_address = (byte[])args[1];
result = TransferDomain(domain_name, to_address);
}
}
else
{
result = false;
}
}
return result;
}
private static object QueryDomain(string domain_name)
{
object result = 0;
Runtime.Notify("QueryDomain", domain_name);
StorageContext ctx = Storage.CurrentContext;
byte[] owner = Storage.Get(ctx, domain_name);
if (owner.Length == 0)
{
Runtime.Notify("Domain is not registered", domain_name);
result = 0;
}
else
{
result = owner;
}
return result;
}
private static bool RegisterDomain(string domain_name, byte[] owner)
{
bool result = false;
Runtime.Notify("RegisterDomain", domain_name, owner);
if (false) // !Runtime.CheckWitness(owner))
{
Runtime.Notify("Owner argument is not the same as the sender", owner);
result = false;
}
else
{
StorageContext ctx = Storage.CurrentContext;
byte[] exists = Storage.Get(ctx, domain_name);
if (exists.Length != 0)
{
Runtime.Notify("Domain is already registered", domain_name, exists);
result = false;
}
else
{
Storage.Put(ctx, domain_name, owner);
result = true;
}
}
return result;
}
private static bool TransferDomain(string domain_name, byte[] to_address)
{
bool result = false;
Runtime.Notify("TransferDomain", domain_name, to_address);
StorageContext ctx = Storage.CurrentContext;
byte[] owner = Storage.Get(ctx, domain_name);
if (owner.Length == 0)
{
Runtime.Notify("Domain is not registered", domain_name);
result = false;
}
else
{
if (false) // !Runtime.CheckWitness(owner))
{
Runtime.Notify("Sender is not the owner of this domain, cannot transfer", domain_name);
result = false;
}
else
{
if (to_address.Length != 34)
{
Runtime.Notify("Invalid new owner address. Must be exactly 34 characters", to_address);
result = false;
}
else
{
Storage.Put(ctx, domain_name, to_address);
result = true;
}
}
}
return result;
}
private static bool DeleteDomain(string domain_name)
{
bool result = false;
Runtime.Notify("DeleteDomain", domain_name);
StorageContext ctx = Storage.CurrentContext;
byte[] owner = Storage.Get(ctx, domain_name);
if (owner.Length == 0)
{
Runtime.Notify("Domain is not registered", domain_name);
result = false;
}
else
{
if (false) // !Runtime.CheckWitness(owner))
{
Runtime.Notify("Sender is not the owner on this domain, cannot delete", domain_name);
result = false;
}
else
{
Storage.Delete(ctx, domain_name);
result = true;
}
}
return result;
}
}
}