-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
423 lines (357 loc) · 11.2 KB
/
server.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const config = require('./client/src/Config.js');
const accessKeyId = config.accessKeyId;
const secretAccessKey = config.secretAccessKey;
const authorization = config.authorization;
const caver = config.caver;
// app.get('/api/hello', (req, res) => {
// res.send({message: 'Hello Express!'});
// })
const data = fs.readFileSync("./database.json");
const conf = JSON.parse(data);
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
databsase: conf.database,
});
connection.connect();
const multer = require("multer");
const upload = multer({ dest: "./upload" });
app.get("/api/products", (req, res) => {
connection.query(
"SELECT * FROM TEST.PRODUCT WHERE isDeleted = 0",
(err, rows, fields) => {
res.send(rows);
}
);
});
app.get("/api/products/:id", (req, res) => {
console.log("req.params.id", req.params.id);
connection.query(
"SELECT name, image FROM TEST.PRODUCT AS P JOIN TEST.RECEIPT AS R ON P.id = R.productID where R.tokenID=? and P.isDeleted = 0",
req.params.id,
(err, rows, fields) => {
console.log(rows);
res.send(rows);
}
);
});
app.get("/api/receipts/:id", (req, res) => {
console.log("get /api/receipts");
connection.query(
"SELECT * FROM TEST.PRODUCT WHERE isDeleted = 0 AND id = ?",
req.params.id,
async (err, rows, fields) => {
if (rows.length == 0) {
res.send("{}");
return;
}
contractAddr = rows[0]["contractAddr"];
var options = {
method: "GET",
url:
"https://th-api.klaytnapi.com/v2/contract/nft/" + contractAddr + "/token",
headers: {
"x-Chain-ID": 1001,
"Content-Type": "application/json",
Authorization: authorization,
},
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
items = response.body["items"];
if (!items) {
res.send(response.body);
return;
}
items.map((contract) => {});
});
}
);
});
app.get("/api/customers/:addr", (req, res) => {
console.log("get /api/customers/:addr", req.params.addr);
connection.query(
"SELECT * FROM TEST.RECEIPT WHERE isDeleted = 0 and LOWER(toAddr) like CONCAT('%',LOWER(?),'%');",
req.params.addr,
(err, rows, fields) => {
res.send(rows);
}
);
});
app.get("/api/sellers", (req, res) => {
connection.query(
"SELECT * FROM TEST.SELLER WHERE isDeleted = 0",
(err, rows, fields) => {
res.send(rows);
}
);
});
app.use("/image", express.static("./upload")); // 맵핑이 됨.
// 컨트랙트(=상품) 추가
app.post("/api/products", upload.single("image"), async (req, res) => {
console.log("post /api/products");
console.log(
"name",
req.body.name,
"symbol",
req.body.symbol,
"sellerPrivateKey",
req.body.sellerPrivateKey
);
const keyring = caver.wallet.keyring.createFromPrivateKey(
req.body.sellerPrivateKey
);
if (!caver.wallet.getKeyring(keyring.address)) {
const singleKeyRing = caver.wallet.keyring.createFromPrivateKey(
req.body.sellerPrivateKey
);
caver.wallet.add(singleKeyRing);
}
const kip17 = await caver.kct.kip17.deploy(
{
name: req.body.name,
symbol: req.body.symbol,
},
keyring.address
);
// .then((kip17) => console.log(kip17.options.address));
console.log(kip17.options.address);
let sql =
"INSERT INTO TEST.PRODUCT (name, symbol, contractAddr, image, registeredDate, isDeleted) VALUES (?, ?, ?, ?, now(), 0)";
console.log(req.file);
let image = "/image/" + req.file.filename;
let name = req.body.name;
let symbol = req.body.symbol;
let params = [name, symbol, kip17.options.address, image];
console.log("insert params: " + params);
connection.query(sql, params, (err, rows, fields) => {
res.send(rows);
console.log(err);
});
console.log("end of /api/products post");
});
app.delete("/api/products/:id", (req, res) => {
console.log("received product delete request");
let sql = "UPDATE TEST.PRODUCT SET isDeleted = 1 WHERE id = ?";
let params = [req.params.id];
console.log(params);
connection.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
// 토큰(=영수증) 발행
app.post("/api/receipts", async (req, res) => {
console.log("post /api/receipts");
console.log("req.body.sellerPrivateKey: " + req.body.sellerPrivateKey);
console.log("req.body.productID: " + req.body.productID);
console.log("req.body.toAddr: " + req.body.toAddr);
const keyring = caver.wallet.keyring.createFromPrivateKey(
req.body.sellerPrivateKey
);
if (!caver.wallet.getKeyring(keyring.address)) {
const singleKeyRing = caver.wallet.keyring.createFromPrivateKey(
req.body.sellerPrivateKey
);
caver.wallet.add(singleKeyRing);
}
connection.query(
"SELECT * FROM TEST.PRODUCT WHERE isDeleted = 0 AND id = ?",
req.body.productID,
async (err, rows, fields) => {
console.log("keyring.address", keyring.address);
console.log("contractAddr", rows[0]["contractAddr"]);
contractAddr = rows[0]["contractAddr"];
const kip17 = new caver.kct.kip17(contractAddr);
minted = false;
while (true) {
randomID = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
console.log("randomID", randomID);
try {
owner = await kip17.ownerOf(randomID);
} catch (e) {
console.log("we can mint");
tokenURI = JSON.stringify({
sellerID: 0,
productID: req.body.productID,
});
mintResult = await kip17.mintWithTokenURI(
req.body.toAddr,
randomID,
tokenURI,
{ from: keyring.address }
);
let sql =
"INSERT INTO TEST.RECEIPT " +
"(sellerID, productID, tokenID, tokenURI, contractAddr, fromAddr, toAddr, registeredDate, lastUpdatedDate, isDeleted)" +
" VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), 0)";
let params = [
0,
req.body.productID,
randomID,
tokenURI,
contractAddr,
"0x0000000000000000000000000000000000000000",
req.body.toAddr,
];
connection.query(sql, params, (err, rows, fields) => {
if (!err) {
console.log("error while inserting a new receipt", "err", err);
}
});
minted = true;
console.log("mintResult", mintResult);
//console.error(e);
}
if (minted) {
break;
}
}
res.send(rows);
}
);
console.log("end of /api/products post");
});
app.post("/api/receipts/send", async (req, res) => {
console.log("post /api/receipts");
console.log("req.body.customerPrivateKey: " + req.body.customerPrivateKey);
console.log("req.body.contractAddr: " + req.body.contractAddr);
console.log("req.body.tokenId: " + req.body.tokenId);
console.log("receiverAddr", req.body.receiverAddr);
let senderPrivateKey = req.body.customerPrivateKey;
// customer private key import
const senderKeyring = caver.wallet.keyring.createFromPrivateKey(
senderPrivateKey
);
if (!caver.wallet.getKeyring(senderKeyring.address)) {
const singleKeyRing = caver.wallet.keyring.createFromPrivateKey(
senderPrivateKey
);
caver.wallet.add(singleKeyRing);
}
let contractAddr = req.body.contractAddr;
connection.query(
"UPDATE TEST.RECEIPT SET fromAddr = ?, toAddr = ?, lastUpdatedDate=NOW() WHERE contractAddr=? AND tokenID=?",
[
senderKeyring.address,
req.body.receiverAddr,
contractAddr,
req.body.tokenId,
],
async (err, rows, fields) => {
const kip17 = new caver.kct.kip17(contractAddr);
console.log(`senderKeyring.address: ${senderKeyring.address}`);
console.log(`req.body.receiverAddr: ${req.body.receiverAddr}`);
console.log(`req.body.tokenId: ${typeof req.body.tokenId}`);
console.log(caver.currentProvider);
transferResult = await kip17.transferFrom(
senderKeyring.address,
req.body.receiverAddr,
req.body.tokenId,
{ from: senderKeyring.address, gas: 200000 }
);
console.log(transferResult);
res.send(transferResult);
}
);
});
app.delete("/api/products/:id", (req, res) => {
console.log("received delete request");
let sql = "UPDATE TEST.PRODUCT SET isDeleted = 1 WHERE id = ?";
let params = [req.params.id];
console.log(params);
connection.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
app.get("/api/klay/blocknumber", async (req, res) => {
bb = await getBlockNumber();
console.log("bb: " + bb);
res.send({ blockNumber: bb });
});
app.get("/api/klay/balance/:addr", async (req, res) => {
console.log(req.params.addr);
b = await getBalance(req.params.addr);
console.log("balance: " + b);
res.send({ balance: b });
});
async function getBlockNumber() {
hexBlockNumber = await caver.rpc.klay.getBlockNumber();
decBlockNumber = parseInt(hexBlockNumber, 16);
return decBlockNumber;
}
async function getBalance(addr) {
hexBalance = await caver.rpc.klay.getBalance(addr);
decBalance = parseInt(hexBalance, 16);
return decBalance;
}
async function getAddress(sellerID) {}
let kasWalletHeader = {
Authorization:
"Basic " +
Buffer.from(accessKeyId + ":" + secretAccessKey).toString("base64"),
"x-krn": "krn:1001:wallet:114:account:default",
};
let request = require("request");
app.get("/api/kas/wallet/newaccount", async (req, res) => {
console.log("/api/kas/wallet/newaccount");
newAccount = await createAccount();
console.log("newAccount: " + newAccount);
res.send({ address: newAccount["result"]["address"] });
});
async function createAccount() {
var options = {
url: "https://wallet-api.beta.klaytn.io/v2/account",
headers: kasWalletHeader,
method: "POST",
auth: {
user: accessKeyId,
pass: secretAccessKey,
},
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
return body;
} else {
console.log(
"error: " + error + ", statusCode: " + response.StatusCode,
", response: " + response
);
}
}
request(options, callback);
}
async function getAccount() {
var options = {
url: "https://wallet-api.beta.klaytn.io/v2/account",
headers: kasWalletHeader,
method: "GET",
body: JSON.stringify({
address: "0xA11D5096BB4a1467Fe16D2a90222e6144eB35476",
}),
auth: {
user: accessKeyId,
pass: secretAccessKey,
},
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} else {
console.log("error: " + error + ", response: " + response);
}
}
request(options, callback);
}
app.listen(port, () => console.log(`Listening on port ${port}`));