-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHathorPlugin.cs
240 lines (179 loc) · 6.59 KB
/
HathorPlugin.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
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
using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;
using System.Text;
public class HathorPlugin : CanvasLayer
{
private string url = "localhost";
private string urlBase = "http://localhost:8000";
private string tokenUid = "";
private string passphrase = "";
private Error err;
private TextEdit txtEditConsole;
private string wallet_id = "my_wallet";
private string seedKey = "default";
public override void _Ready()
{
GetNode("HTTPRequest").Connect("request_completed", this, "OnRequestCompleted");
txtEditConsole = GetNode<TextEdit>("txtEditConsole");
GetNode("btnStartWallet").Connect("pressed", this, "OnButtonStartWalletPressed");
GetNode("btnStatusWallet").Connect("pressed", this, "OnButtonStatusWalletPressed");
GetNode("btnBalance").Connect("pressed", this, "OnButtonBalancePressed");
GetNode("btnCurrentAddress").Connect("pressed", this, "OnButtonCurrentAddressPressed");
GetNode("btnAddresses").Connect("pressed", this, "OnButtonAddressesPressed");
GetNode("btnSimpleSendTx").Connect("pressed", this, "OnButtonSimpleSendTxPressed");
GetNode("btnCreateToken").Connect("pressed", this, "OnButtonCreateTokenPressed");
}
public void OnButtonStartWalletPressed()
{
Godot.Collections.Dictionary bodyDict = new Godot.Collections.Dictionary();
bodyDict.Add("wallet-id", wallet_id);
bodyDict.Add("seedKey", seedKey);
if (passphrase != "")
{
bodyDict.Add("passphrase", passphrase);
}
string query = JSON.Print(bodyDict);
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
string[] headers = new string[] { "Content-Type: application/json" };
httpRequest.Request(urlBase + "/start", headers, false, HTTPClient.Method.Post, query);
}
public void OnButtonStatusWalletPressed()
{
string[] headers = new string[] { "x-wallet-id: " + wallet_id };
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/wallet/status", headers);
}
public void OnButtonBalancePressed()
{
string[] headers = new string[] { "x-wallet-id: " + wallet_id };
if (tokenUid != "")
{
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + " /wallet/balance?token=" + tokenUid, headers);
}
else
{
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/wallet/balance", headers);
}
}
public void OnButtonCurrentAddressPressed()
{
string[] headers = new string[] { "x-wallet-id: " + wallet_id };
bool mark_as_used = false;
int index = -1;
string query = "";
if (mark_as_used && index >= 0){
query = "?mark_as_used="+mark_as_used;
}else if (mark_as_used && index >= 0){
query = "?mark_as_used="+mark_as_used+"&index="+index.ToString();
}else if (!mark_as_used && index >= 0){
query = "?index="+index.ToString();
}
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
if (query != ""){
httpRequest.Request(urlBase + "/wallet/address"+query, headers);
}else {
httpRequest.Request(urlBase + "/wallet/address", headers);
}
}
public void OnButtonAddressIndexPressed()
{
string[] headers = new string[] { "x-wallet-id: " + wallet_id };
string address = "";
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/wallet/address-index?address=" + address, headers);
}
public void OnButtonAddressesPressed()
{
string[] headers = new string[] { "x-wallet-id: " + wallet_id };
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/wallet/addresses", headers);
}
public void OnButtonSimpleSendTxPressed()
{
string address = "";
int value = 0;
string token = "";
string change_address = "";
string[] headers = new string[] { "Content-Type: application/json", "x-wallet-id: " + wallet_id };
Godot.Collections.Dictionary bodyDict = new Godot.Collections.Dictionary();
bodyDict.Add("wallet-id", wallet_id);
bodyDict.Add("seedKey", seedKey);
if (token != "")
{
bodyDict.Add("token", token);
}
if (change_address != "")
{
bodyDict.Add("change_address", change_address);
}
string query = JSON.Print(bodyDict);
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/start", headers, false, HTTPClient.Method.Post, query);
}
// public void OnButtonSendTxPressed()
// {
// string[] outputs = new string[] {};
// string[] inputs = new string[] {};
// //
// int value = 0;
// string token = "";
// Godot.Collections.Dictionary itemOutput = new Godot.Collections.Dictionary();
// itemOutput.Add("address", wallet_id);
// itemOutput.Add("value", value);
// if(token != ""){
// itemOutput.Add("token", token);
// }
// outputs.Add(JSON.Print(itemOutput));
// Godot.Collections.Dictionary itemInput = new Godot.Collections.Dictionary();
// string[] headers = new string[] { "Content-Type: application/json", "x-wallet-id: " + wallet_id };
// Godot.Collections.Dictionary bodyDict = new Godot.Collections.Dictionary();
// bodyDict.Add("wallet-id", wallet_id);
// bodyDict.Add("seedKey", seedKey);
// if (token != "")
// {
// bodyDict.Add("token", token);
// }
// if (change_address != "")
// {
// bodyDict.Add("change_address", change_address);
// }
// string query = JSON.Print(bodyDict);
// HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
// httpRequest.Request(urlBase + "/start", headers, false, HTTPClient.Method.Post, query);
// }
public void OnButtonCreateTokenPressed()
{
string name = "";
string symbol = "";
string address = "";
int amount = 0;
string change_address = "";
string[] headers = new string[] { "Content-Type: application/json", "x-wallet-id: " + wallet_id };
Godot.Collections.Dictionary bodyDict = new Godot.Collections.Dictionary();
bodyDict.Add("name", name);
bodyDict.Add("symbol", symbol);
bodyDict.Add("amount", amount);
if (address != "")
{
bodyDict.Add("address", token);
}
if (change_address != "")
{
bodyDict.Add("change_address", change_address);
}
string query = JSON.Print(bodyDict);
HTTPRequest httpRequest = GetNode<HTTPRequest>("HTTPRequest");
httpRequest.Request(urlBase + "/wallet/create-token", headers, false, HTTPClient.Method.Post, query);
}
public void OnRequestCompleted(int result, int response_code, string[] headers, byte[] body)
{
JSONParseResult json = JSON.Parse(Encoding.UTF8.GetString(body));
GD.Print(json.Result);
//txtEditConsole.setShowLineNumbers(true);
txtEditConsole.Text = json.Result.ToString();
}
}