-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidatorSocketSession.cs
133 lines (123 loc) · 5.92 KB
/
ValidatorSocketSession.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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using NetCoreServer;
namespace XLS_20_Bridge_MasterProcess
{
class ValidatorSocketSession : WsSession
{
private string ApiKey;
private database db;
private Settings config;
public ValidatorSocketSession(WsServer server, database _db, Settings _config) : base(server)
{
ApiKey = _config._validatorKey;
db = _db;
config = _config;
}
public override void OnWsConnected(HttpRequest request)
{
string tempApiKeyStorage = "";
long totalHeaders = request.Headers;
for (long j = 0; j < totalHeaders; j++)
{
if (request.Header((int)j).Item1 == "token")
{
tempApiKeyStorage = request.Header((int)j).Item2;
}
}
if (tempApiKeyStorage != ApiKey)
{
this.Disconnect();
}
else
{
Console.WriteLine("Validator Connected!");
}
}
public override void OnWsDisconnected()
{
Console.WriteLine($"WebSocket session with Id {Id} disconnected!");
}
public override void OnWsReceived(byte[] buffer, long offset, long size)
{
try
{
string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
var request = JsonSerializer.Deserialize<Payload>(message);
if (request.type == "Request")
{
if (request.command == "GetNewNFTs")
{
ResponsePayloadBridgeNFT responsePayload = new ResponsePayloadBridgeNFT();
List<BridgeNFT> list = db.GetPendingNFTs(request.validator);
responsePayload.bridgelist = list;
responsePayload.type = "Response";
responsePayload.command = "NewBridgeNFTs";
responsePayload.validator = request.validator;
var response = JsonSerializer.Serialize(responsePayload);
SendText(response);
}
}
if (request.type == "Response")
{
if (request.command == "Ping")
{
var payloadPingResponse = JsonSerializer.Deserialize<Payload>(message);
db.ValidatorUpdatePing(payloadPingResponse.validator);
}
if (request.command == "SignMessage")
{
var payloadSign = JsonSerializer.Deserialize<PayloadSign>(message);
db.UpdateSignMessage(payloadSign.originOwner, payloadSign.contractAddress, Convert.ToInt32(payloadSign.tokenId), payloadSign.signMessage, payloadSign.validator);
PayloadSign responsePayload = new PayloadSign();
responsePayload.type = "Response";
responsePayload.command = "SignMessageConfirmed";
responsePayload.validator = request.validator;
responsePayload.originOwner = payloadSign.originOwner;
responsePayload.contractAddress = payloadSign.contractAddress;
responsePayload.tokenId = payloadSign.tokenId;
var response = JsonSerializer.Serialize(responsePayload);
SendText(response);
}
if (request.command == "CombineMultiSig")
{
var responseCombined = JsonSerializer.Deserialize<ResponseCombined>(message);
db.UpdateSignMessageCombined(responseCombined.originOwner, responseCombined.contractAddress, Convert.ToInt32(responseCombined.tokenId), responseCombined.txn_blob, responseCombined.validator, config);
}
if (request.command == "CombineMultiSigOffer")
{
var responseCombined = JsonSerializer.Deserialize<ResponseCombined>(message);
db.UpdateSignOfferMessageCombined(responseCombined.originOwner, responseCombined.contractAddress, Convert.ToInt32(responseCombined.tokenId), responseCombined.txn_blob, responseCombined.validator, config);
}
if (request.command == "OfferSignMessage")
{
var payloadSign = JsonSerializer.Deserialize<ResponseOfferSign>(message);
db.UpdateOfferSignMessage(payloadSign.originOwner, payloadSign.contractAddress, Convert.ToInt32(payloadSign.tokenId), payloadSign.signMessage, payloadSign.validator, payloadSign.xrplTokenId);
PayloadSign responsePayload = new PayloadSign();
responsePayload.type = "Response";
responsePayload.command = "SignOfferMessageConfirmed";
responsePayload.validator = request.validator;
responsePayload.originOwner = payloadSign.originOwner;
responsePayload.contractAddress = payloadSign.contractAddress;
responsePayload.tokenId = payloadSign.tokenId;
var response = JsonSerializer.Serialize(responsePayload);
SendText(response);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"WebSocket session caught an error with code {error}");
}
}
}