-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidator.js
More file actions
332 lines (309 loc) · 9.98 KB
/
validator.js
File metadata and controls
332 lines (309 loc) · 9.98 KB
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
const xrpl = require("xrpl");
const WebSocket = require("ws");
const web3 = require("web3");
const sqlite3 = require("sqlite3").verbose();
var Contract = require("web3-eth-contract");
const fs = require("fs");
const Recovery = require("./recovery.js");
const Storage = require("./storage.js");
const XrplHelpers = require("./xrplhelpers.js");
require("dotenv").config();
const autoReconnectDelay = 5000;
var db;
var bridgeABI;
var erc721baseABI;
var ws;
var storage;
var recovery;
var xrplHelpers;
const requestNFTPayload = {
type: "Request",
command: "GetNewNFTs",
validator: process.env.VALIDATOR_NUMBER,
};
const responseCombineMultiSigPayload = {
type: "Response",
command: "CombineMultiSig",
validator: process.env.VALIDATOR_NUMBER,
originOwner: "",
contractAddress: "",
tokenId: "",
txn_blob: "",
};
const responseCombineMultiSigPayloadOffer = {
type: "Response",
command: "CombineMultiSigOffer",
validator: process.env.VALIDATOR_NUMBER,
originOwner: "",
contractAddress: "",
tokenId: "",
txn_blob: "",
};
const responseSignPayload = {
type: "Response",
command: "SignMessage",
validator: process.env.VALIDATOR_NUMBER,
originOwner: "",
contractAddress: "",
tokenId: "",
xrplAddress: "",
signMessage: {},
};
const offerResponseSignPayload = {
type: "Response",
command: "OfferSignMessage",
validator: process.env.VALIDATOR_NUMBER,
originOwner: "",
contractAddress: "",
tokenId: "",
xrplAddress: "",
xrplTokenId: "",
signMessage: {},
};
const responsePing = {
type: "Response",
command: "Ping",
validator: process.env.VALIDATOR_NUMBER,
};
var reconnectInterval = 1000 * 5;
var connect = function () {
ws = new WebSocket(process.env.MAIN_SERVER, {
headers: {
token: process.env.VALIDATOR_WSS_KEY,
},
});
ws.on("open", function () {
console.log("Validator " + process.env.VALIDATOR_NUMBER + " Connected!");
});
ws.on("error", function () {
console.log("socket error");
});
ws.on("close", function () {
console.log("socket close...attempting reconnect");
setTimeout(connect, reconnectInterval);
});
ws.on("message", async (e) => {
try {
var request = JSON.parse(Buffer.from(e).toString("utf8"));
if (request.command == "Ping") {
await ws.send(JSON.stringify(responsePing));
}
if (request.command == "SignMessageConfirmed") {
storage.updateSigned(
db,
request.contractAddress,
request.originOwner,
request.tokenId
);
}
if (request.command == "SignOfferMessageConfirmed") {
storage.updateOfferCreated(
db,
request.contractAddress,
request.originOwner,
request.tokenId
);
}
if (request.command == "CombineMultiSig") {
if (request.txn_blob.length > 1) {
var blobArray = [];
for (j = 0; j < request.txn_blob.length; j++) {
blobArray.push(request.txn_blob[j]);
}
const combined = await xrpl.multisign(blobArray);
var responsePayload = responseCombineMultiSigPayload;
responsePayload.originOwner = request.originOwner;
responsePayload.contractAddress = request.contractAddress;
responsePayload.tokenId = request.tokenId;
responsePayload.txn_blob = combined;
await ws.send(JSON.stringify(responsePayload));
} else {
console.log("Invalid # of transaction blobs to be combined");
}
}
if (request.command == "CombineMultiSigOffer") {
if (request.txn_blob.length > 1) {
var blobArray = [];
for (j = 0; j < request.txn_blob.length; j++) {
blobArray.push(request.txn_blob[j]);
}
const client = new xrpl.Client(process.env.XRPL_RPC);
const combined = await xrpl.multisign(blobArray);
var responsePayload = responseCombineMultiSigPayloadOffer;
responsePayload.originOwner = request.originOwner;
responsePayload.contractAddress = request.contractAddress;
responsePayload.tokenId = request.tokenId;
responsePayload.txn_blob = combined;
await ws.send(JSON.stringify(responsePayload));
} else {
console.log("Invalid # of transaction blobs to be combined");
}
}
if (request.command == "CreateOffer") {
await CreateOffer(request);
}
if (request.command == "NewBridgeNFTs") {
let recordExists = await storage.recordexists(
db,
request.contractAddress,
request.originOwner,
request.tokenId
);
if (recordExists == false) {
await ValidateAndSign(request);
}
}
} catch (error) {
console.log(error);
}
});
};
async function main() {
//Startup
try {
bridgeABI = fs.readFileSync("./bridge-abi.txt", "utf8");
erc721baseABI = fs.readFileSync("./erc721-base.txt", "utf8");
} catch (err) {
console.error(err);
}
try {
storage = new Storage();
recovery = new Recovery();
xrplHelpers = new XrplHelpers();
if (fs.existsSync("./storage.db")) {
console.log("Database found");
db = storage.getInstance();
console.log("ERC721-XLS20 Validator Starting");
await connect();
} else {
console.log("Database not found. Starting in recovery mode...");
db = storage.createDatabase();
const recoveryValid = await recovery.RunRecovery(db);
if (recoveryValid == true) {
console.log("Recovery Successful");
await connect();
} else {
console.log("Recovery Failed...Quitting");
return;
}
}
} catch (err) {
console.error(err);
}
}
async function CreateOffer(response) {
try {
const result = await xrplHelpers.getAccountNFTs();
for (i = 0; i < result.account_nfts.length; i++) {
if (
xrpl.convertHexToString(result.account_nfts[i].URI) == response.tokenUri
) {
const client = new xrpl.Client(process.env.XRPL_RPC);
var offerPayload = xrplHelpers.CreateOfferPayload();
offerPayload.NFTokenID = result.account_nfts[i].NFTokenID;
offerPayload.Destination = response.xrplAddress;
offerPayload.Sequence = await xrplHelpers.getNextSequence();
let memoData;
memoData = await xrpl.convertStringToHex(
'{ "contractAddress":"' +
response.contractAddress +
'", "originOwner": "' +
response.originOwner +
'", "tokenId":"' +
response.tokenId +
'" }'
);
offerPayload.Memos[0].Memo.MemoData = memoData;
const wallet = xrpl.Wallet.fromSeed(process.env.VALIDATOR_XRPL_SECRET);
const signedMessage = await wallet.sign(offerPayload, true);
var payload = offerResponseSignPayload;
payload.originOwner = response.originOwner;
payload.contractAddress = response.contractAddress;
payload.tokenId = response.tokenId;
payload.xrplAddress = response.xrplAddress;
payload.xrplTokenId = result.account_nfts[i].NFTokenID;
payload.signMessage = signedMessage.tx_blob;
console.log(payload);
ws.send(JSON.stringify(payload));
break;
}
}
} catch (error) {
console.log(error);
}
}
async function ValidateAndSign(request) {
try {
Contract.setProvider(process.env.ETH_ENDPOINT);
var MyContract = new Contract(
JSON.parse(bridgeABI),
process.env.BRIDGE_CONTRACT
);
var originOwner = request.originOwner;
var tokenId = request.tokenId;
var contractAddress = request.contractAddress;
var xrplAddress = request.xrplAddress;
//Validate that data is good straight from ETH
MyContract.methods
.returnBridgeNFTsByWallet(request.originOwner)
.call()
.then(async function (result) {
for (j = 0; j < result.length; j++) {
if (
originOwner == result[j].originOwner &&
tokenId == result[j].tokenId &&
contractAddress == result[j].contractAddress
) {
//Valid data. Proceed to sign and send to master process
var signMessage = xrplHelpers.TokenMintPayload();
//Check token URI
var TokenContract = new Contract(
JSON.parse(erc721baseABI),
result[j].contractAddress
);
TokenContract.methods
.tokenURI(result[j].tokenId)
.call()
.then(async function (result) {
const tokenUri = result;
//Create Signed Payload and send back to Master Process
signMessage.URI = xrpl.convertStringToHex(tokenUri);
let memoData;
memoData = await xrpl.convertStringToHex(
'{ "contractAddress":"' +
contractAddress +
'", "originOwner": "' +
originOwner +
'", "tokenId":"' +
tokenId +
'" }'
);
signMessage.Memos[0].Memo.MemoData = memoData;
signMessage.Sequence = await xrplHelpers.getNextSequence();
const wallet = xrpl.Wallet.fromSeed(
process.env.VALIDATOR_XRPL_SECRET
);
const signedMessage = await wallet.sign(signMessage, true);
var payload = responseSignPayload;
payload.originOwner = originOwner;
payload.contractAddress = contractAddress;
payload.tokenId = tokenId;
payload.xrplAddress = xrplAddress;
payload.signMessage = signedMessage.tx_blob;
ws.send(JSON.stringify(payload));
storage.insert(
db,
contractAddress,
originOwner,
tokenId,
xrplAddress
);
});
}
}
});
} catch (error) {
console.log("Validate and sign error: " + error);
}
}
main();