-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCardanoManager.cs
384 lines (326 loc) · 14.1 KB
/
CardanoManager.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace BlockClocksWindows
{
public partial class CardanoManager
{
public static CardanoManager Instance { get; set; }
const string APIROOT = "https://cardano-mainnet.blockfrost.io/api/v0/";
const string JSONPOLICYFILENAMEFORMAT = "policy/{0}-{1}.json";
const string JSONWALLETFILENAMEFORMAT = "wallet/{0}-{1}.json";
const string JSONASSETFILENAMEFORMAT = "asset/{0}.json";
public CardanoManager()
{
Instance = this;
}
public void GetNFTDetails(string asset, bool cache, System.Action<string, string, JToken, string, string> callback)
{
GetNFTInfo(asset, cache, (info) =>
{
ProcessDetails(info, callback);
});
}
public void ProcessDetails(string info, System.Action<string, string, JToken, string, string> callback)
{
JObject jsoninfo = JObject.Parse(info);
JToken onchainmetadata = jsoninfo.SelectToken("onchain_metadata");
string encodeddata = null;
string videohash = null;
if (onchainmetadata != null)
{
JToken src = onchainmetadata.SelectToken("files[0].src");
if (src != null)
{
if (onchainmetadata.SelectToken("files[0].mediaType")?.ToString() == "video/mp4")
{
videohash = src.ToString();
videohash = Stripipfs(videohash);
}
else
{
if (src.Type.ToString() == "Array")
{
var filesrc = onchainmetadata.SelectToken("files[0].src")?.ToObject<string[]>();
if (filesrc != null)
{
encodeddata = string.Concat(filesrc).Replace("data:text/html;base64,", "");
}
}
}
}
}
string image = FindImageIPFSHash(jsoninfo);
callback(image, FindName(jsoninfo), onchainmetadata, encodeddata, videohash);
}
private string FindImageIPFSHash(JObject jobject)
{
try
{
if (jobject.SelectToken("onchain_metadata.image")?.Type == JTokenType.Array)
{
//sometimes an onchain array of the file... how to handle this??
return null;
}
else
{
string image = jobject.SelectToken("onchain_metadata.image")?.Value<string>();
if (image != null)
{
return Stripipfs(image);
}
foreach (var item in jobject)
{
if (item.Value.GetType() == typeof(JObject))
{
var stringFound = FindImageIPFSHash((JObject)item.Value);
if (stringFound != null)
{
return stringFound;
}
}
else
{
if (item.Value.Type.ToString() == "Array")
{
foreach (var arrayItem in ((JContainer)item.Value).Children())
{
foreach (var child in arrayItem.Children())
{
if (child.First.ToString().StartsWith("ipfs://"))
{
return Stripipfs(child.First.ToString());
}
}
}
}
return Stripipfs(item.Value.ToString());
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
return null;
}
private string Stripipfs(string tostrip)
{
if (tostrip.StartsWith("ipfs://"))
{
tostrip = tostrip.Substring(7).Replace("ipfs", "").Replace("/", "");
}
return tostrip;
}
private string FindName(JObject jobject)
{
foreach (var item in jobject)
{
if (item.Value.GetType() == typeof(JObject))
{
var stringFound = FindName((JObject)item.Value);
if (stringFound != null)
{
return stringFound;
}
}
else
{
if (string.Equals(item.Key, "name", System.StringComparison.OrdinalIgnoreCase) ||
string.Equals(item.Key, "title", System.StringComparison.OrdinalIgnoreCase))
{
return item.Value.ToString();
}
}
}
return null;
}
public void GetNFTInfo(string asset, bool cache, System.Action<string> callback)
{
GetNFTInfoFromBlockchain(asset, cache, callback);
}
public void GetNFTInfoFromBlockchain(string asset, bool cache, System.Action<string> callback, System.Action<string, string> errorCallback = null)
{
WebRequest request = WebRequest.Create(APIROOT + "assets/" + asset);
request.Headers.Add("project_id", ApiKey);
string responseFromServer;
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
}
// Close the response.
response.Close();
callback(responseFromServer);
}
public void GetPolicyAssetListFromBlockchain(string policy, string order, System.Action<List<assetinfo>> callback)
{
WebRequest request = WebRequest.Create(APIROOT + "assets/policy/" + policy + "?order=" + order);
request.Headers.Add("project_id", ApiKey);
string responseFromServer;
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}
// Close the response.
response.Close();
List<assetinfo> assetlist = JsonConvert.DeserializeObject<List<assetinfo>>(responseFromServer);
callback(assetlist);
}
public void GetWalletStakeFromAddress(string wallet, System.Action<string> callback, System.Action<string> error = null)
{
//get stake key from address
WebRequest request = WebRequest.Create(APIROOT + "addresses/" + wallet);
request.Headers.Add("project_id", ApiKey);
string responseFromServer;
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}
// Close the response.
response.Close();
address address = JsonConvert.DeserializeObject<address>(responseFromServer);
callback(address.stake_address);
}
public void GetWalletAssetListFromBlockchainProcess(string req, System.Action<List<assetinfo>> callback)
{
//get stake key from address
WebRequest request = WebRequest.Create(req);
request.Headers.Add("project_id", ApiKey);
string responseFromServer;
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}
// Close the response.
response.Close();
List<assetinfo> assetlist = JsonConvert.DeserializeObject<List<assetinfo>>(responseFromServer);
callback(assetlist);
}
public void GetAllWalletAssetListFromBlockchain(string wallet, string order, System.Action<List<assetinfo>> callback)
{
int count = 100;
int page = 1;
List<assetinfo> assets = new List<assetinfo>();
getWalletAssetListPage(wallet, order, count, page, assets, callback);
}
private void getWalletAssetListPage(string wallet, string order, int count, int page, List<assetinfo> assets, System.Action<List<assetinfo>> callback)
{
GetWalletAssetListFromBlockchain(wallet, order, count, page, (List<assetinfo> assetspage) => {
assets.AddRange(assetspage);
if (assetspage.Count == count)
{
getWalletAssetListPage(wallet, order, count, page + 1, assets, callback);
}
else
{
callback(assets);
}
});
}
public void GetWalletAssetListFromBlockchain(string wallet, string order, int count, int page, System.Action<List<assetinfo>> callback)
{
string req = "";
if (wallet.StartsWith("addr"))
{
GetWalletStakeFromAddress(wallet, (stake) => {
req = $"{APIROOT}accounts/{stake}/addresses/assets?order={order}&count={count}&page={page}";
GetWalletAssetListFromBlockchainProcess(req, callback);
});
}
else
{
req = $"{APIROOT}accounts/{wallet}/addresses/assets?order={order}&count={count}&page={page}";
GetWalletAssetListFromBlockchainProcess(req, callback);
}
}
public void GetTransactionUTXOs(string transaction, System.Action<transaction> callback, System.Action<string> error = null)
{
//get stake key from address
WebRequest request = WebRequest.Create(APIROOT + "txs/" + transaction + "/utxos");
request.Headers.Add("project_id", ApiKey);
string responseFromServer;
WebResponse response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
}
// Close the response.
response.Close();
transaction transactionResult = JsonConvert.DeserializeObject<transaction>(responseFromServer);
callback(transactionResult);
}
public void ParseMetadata(clockassetcontents extnft, string metadata)
{
JObject jsoninfo = JObject.Parse(metadata);
extnft.ipfshash = FindImageIPFSHash(jsoninfo);
if (!string.IsNullOrEmpty(extnft.ipfshash) && extnft.ipfshash.Contains("base64,"))
{
//actually base64 encoded file, decode it instead
extnft.ipfshash = null;
//do decoding here, probably an svg, check type
}
else if (!string.IsNullOrEmpty(extnft.ipfshash) && extnft.ipfshash.StartsWith("http"))
{
extnft.image = extnft.ipfshash;
extnft.ipfshash = null;
}
string videohash = null;
JToken src = jsoninfo.SelectToken("files[0].src");
if (src != null)
{
if (jsoninfo.SelectToken("files[0].mediaType")?.ToString() == "video/mp4")
{
videohash = src.ToString();
videohash = Stripipfs(videohash);
extnft.videohash = videohash;
}
else
{
if (src.Type.ToString() == "Array")
{
var filesrc = jsoninfo.SelectToken("files[0].src")?.ToObject<string[]>();
if (filesrc != null)
{
extnft.onchainhtml = string.Concat(filesrc).Replace("data:text/html;base64,", "");
}
}
}
}
}
}
}