-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.js
70 lines (66 loc) · 1.77 KB
/
run.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
const express = require('express')
/**
* Initializes the router for handling run requests.
* @param {Object} node - The node object.
* @returns {Object} - The router object.
*/
const init = (node) => {
const runKey = async (req, res) => {
console.log("run called");
let body = req.body;
const args = {
...req.params
};
delete args.pk;
try {
const kp = {
publicKey: Buffer.from(req.params.pk, "hex")
};
console.log(kp.publicKey.toString("hex"));
const lbkey = await node.lbfind(kp);
console.log({
body: body,
args: args
});
const output = await node.runKey(Buffer.from(lbkey[0], "hex"), body || args, {
reusableSocket: false
});
console.log("output:", output);
if (typeof output == "object")
res.write(JSON.stringify(output));
else if (typeof output == "string")
res.write(output);
res.status(200).end();
} catch (err) {
console.log([err]);
res.write(JSON.stringify(err));
res.status(500).end();
}
};
const findKey = async (req, res) => {
console.log("find called", req.params);
try {
const kp = {
publicKey: Buffer.from(req.params.pk, "hex")
};
console.log(kp.publicKey.toString("hex"));
const lbkey = await node.lbfind(kp);
const found = JSON.stringify(lbkey);
console.log({
found: found
});
res.write(found);
res.status(200).end();
} catch (err) {
console.log(err);
res.write(JSON.stringify(err));
res.status(500).end();
}
};
const router = express.Router();
router.get("/key/:pk", runKey);
router.get("/key/find/:pk", findKey);
router.post("/key/:pk", runKey);
return router;
};
module.exports = init;