forked from XRPLBounties/Proof-of-Attendance-API
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
322 lines (310 loc) · 9.96 KB
/
index.js
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
const express = require("express");
const xrpl = require("xrpl");
require("dotenv").config();
const { Attendify } = require("./attendify");
const {
postToIPFS,
ascii_to_hexa,
makeid,
ERR_ATTENDIFY,
ERR_IPFS,
ERR_NOT_FOUND,
ERR_PARAMS,
ERR_XRPL,
} = require("./utils");
const fs = require("fs");
const app = express();
let AttendifyLib = new Attendify();
/**
* Checks if all required params were provided for requested endpoint
* @param {Array} params - list of parameters that are necessary for correct execution of code in checked endpoint
*/
const requireParams = (params) => {
return (req, res, next) => {
const reqParamList = Object.keys(req.query);
const reqValueList = Object.values(req.query);
const hasAllRequiredParams = params.every((param) =>
reqParamList.includes(param)
);
let hasNonEmptyParams = false;
if (hasAllRequiredParams) {
hasNonEmptyParams = reqValueList.every(
(paramValue) => paramValue.length != 0
);
}
if (!hasAllRequiredParams || !hasNonEmptyParams)
return res
.status(400)
.send(
`The following parameters are all required for this route: ${params.join(
", "
)}`
);
next();
};
};
/**
* Creates new event,
* Uploads its metadata to IPFS
* and mints a batch of NFTs for it
* @route GET /api/mint
* @param {string} walletAddress - The wallet address to mint the NFT tokens to
* @param {integer} tokenCount - The number of NFT tokens to mint
* @param {string} url - The URL of the event
* @param {string} title - The title of the event
* @param {string} desc - The description of the event
* @param {string} loc - The location of the event
* @returns {object} result - An object with details for the claim event that was successfully created
* @throws {Error} If any of the walletAddress, tokenCount, url, title, desc, or loc parameters are missing or have an invalid value
*/
app.get(
"/api/mint",
requireParams(["walletAddress", "tokenCount", "url", "title", "desc", "loc"]),
(req, res) => {
(async () => {
try {
const { walletAddress, tokenCount, url, title, desc, loc } =
await req.query;
const minterWallet = await xrpl.Wallet.fromSeed(
process.env.WALLET_SEED
);
let metadataStructure = {
name: title,
description: desc,
collectionSize: tokenCount,
location: loc,
date: new Date().toLocaleDateString().toString(),
image: url,
owner: minterWallet.address,
};
const metadata = await postToIPFS(JSON.stringify(metadataStructure)); //.substring(21);
console.log(metadata);
return res.send({
result: await AttendifyLib.batchMint(
walletAddress,
parseInt(tokenCount),
metadata,
title,
process.env.WALLET_SEED
),
});
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
}
);
/**
* Requests claim for specified event
* * The sell offer that's returned has to be accepted by user to finish process of claiming NFT
* @route GET /api/claim
* @param {string} walletAddress - The wallet address of the user trying to claim
* @param {integer} type - The type of claim (1 for checking claim status and metadata, 2 for claiming)
* @param {string} minter - The minter address of the event
* @param {string} eventId - The ID of the event
* @returns {object} result - An object with the event metadata and offer for NFT
* @throws {Error} If any of the walletAddress, id, type, minter, or eventId parameters are missing or have an invalid value
*/
app.get(
"/api/claim",
requireParams(["walletAddress", "type", "minter", "eventId"]),
(req, res) => {
(async () => {
try {
const { walletAddress, type, minter, eventId } = await req.query;
let data = await fs.promises.readFile("participants.json", "utf-8");
let requestedClaim = JSON.parse(data.toString()).data[
parseInt(eventId)
];
if (
xrpl.Wallet.fromSeed(process.env.WALLET_SEED).classicAddress != minter
)
res.status(500).send({
statusText: `minter address does not match local minter account`,
});
const claimableTokens = await AttendifyLib.getBatchNFTokens(
minter,
eventId
);
//Check if the requested claim event exists
if (!requestedClaim) {
return res.send({
status: "404",
// result: "The requested claim event does not exist.",
});
}
// Check if user already claimed NFT
if (
requestedClaim.find((obj) => {
return obj.user === walletAddress;
}) != undefined
)
return res.send({
status: "claimed",
// result: requestedClaim,
});
// Check if there are any remaining NFTs
if (claimableTokens.length == 0) {
return res.send({
status: "empty",
// result: requestedClaim,
});
}
// Checking which type of action should be performed
if (type == 1) {
return res.send({
status: "success",
result: claimableTokens.length,
});
} else {
const claimOffer = await AttendifyLib.createSellOfferForClaim(
walletAddress,
process.env.WALLET_SEED,
claimableTokens[0].NFTokenID
);
return res.send({
status: "transferred",
// result: claimableTokens,
offer: claimOffer,
});
}
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
}
);
/**
* Starts ownership verification process by generating unique ID for user that has to later be included in a Memo of signed tx
* @route GET /api/startVerification
* @param {string} walletAddress - The wallet address of the ticket owner
* @returns {object} result - An object with generated Memo ID string
*/
app.get(
"/api/startVerification",
requireParams(["walletAddress"]),
(req, res) => {
(async () => {
try {
const { walletAddress } = await req.query;
const EXPECTED_MEMO_ID = await makeid(256);
console.log(EXPECTED_MEMO_ID);
await AttendifyLib.signatureMap.set(walletAddress, EXPECTED_MEMO_ID);
return res.send({
result: EXPECTED_MEMO_ID,
});
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
}
);
/**
* Verifies ownership of NFT with provided id for particular user
* * Signature has to match with walletAddress account
* @route GET /api/verifyOwnership
* @param {string} walletAddress - The wallet address of the ticket owner
* @param {string} signature - The signature of the ticket
* @param {string} minter - The minter address of the event
* @param {string} eventId - The ID of the event
* @returns {object} result - An object with the verification result
* @throws {Error} If any of the walletAddress, signature, minter, or eventId parameters are missing or have an invalid value
*/
app.get(
"/api/verifyOwnership",
requireParams(["walletAddress", "signature", "minter", "eventId"]),
(req, res) => {
(async () => {
try {
const { walletAddress, signature, minter, eventId } = await req.query;
if (
xrpl.Wallet.fromSeed(process.env.WALLET_SEED).classicAddress != minter
)
res.status(500).send({
statusText: `minter address does not match local minter account`,
});
const isOwnershipVerified = await AttendifyLib.verifyOwnership(
walletAddress,
signature,
minter,
eventId
);
console.log(isOwnershipVerified);
if (isOwnershipVerified === true || isOwnershipVerified === false) {
return res.status(200).send({
result: isOwnershipVerified,
});
} else {
return res.status(500).send({
statusText: isOwnershipVerified.toString(),
});
}
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
}
);
/**
* Looks up attendees for particular event
* @route GET /api/attendees
* @param {string} minter - The minter address of the event
* @param {string} eventId - The ID of the event
* @returns {object} result - An object with the list of attendees
* @throws {Error} If either the minter or eventId parameters are missing or have an invalid value
*/
app.get("/api/attendees", requireParams(["minter", "eventId"]), (req, res) => {
(async () => {
try {
const { minter, eventId } = await req.query;
if (
xrpl.Wallet.fromSeed(process.env.WALLET_SEED).classicAddress != minter
)
res.status(500).send({
statusText: `minter address does not match local minter account`,
});
return res.send({
result: await AttendifyLib.attendeesLookup(minter, eventId),
});
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
});
/**
* Creates new XRPL account for the end user and funds it.
* * Currently used with UI for testing purposes
* @route GET /api/getNewAccount
* @returns {object} result - An object with the new wallet
* @throws {Error} If creating and funding the new wallet wasn't completed
*/
app.get("/api/getNewAccount", (req, res) => {
(async () => {
try {
return res.send({
result: await AttendifyLib.getNewAccount(),
});
} catch (error) {
console.error(error);
res.status(500).send({
statusText: `${error}`,
});
}
})();
});
module.exports = app;