-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgate.js
72 lines (53 loc) · 2.46 KB
/
gate.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
const Orbs = require("orbs-client-sdk")
const sha256 = require("sha256")
async function checkIn(contractName, employee, ownerId, secret, ticketId, confirmation) {
const client = new Orbs.Client("http://localhost:8080", 42, Orbs.NetworkType.NETWORK_TYPE_TEST_NET);
const [ tx, txid ] = client.createTransaction(employee.publicKey, employee.privateKey, contractName, "checkIn", [Orbs.argBytes(ownerId), Orbs.argBytes(secret), Orbs.argUint32(ticketId), Orbs.argString(confirmation)]);
const result = await client.sendTransaction(tx);
console.log(result);
return result.outputArguments[0].value
}
console.log(process.argv)
const contractName = process.argv[2];
const auditContractName = process.argv[5];
const employee = {
publicKey: Orbs.addressToBytes(process.argv[3]),
privateKey: Orbs.addressToBytes(process.argv[4]),
}
const express = require('express')
const app = express()
const port = 4000
app.use(express.urlencoded());
const sessions = {}
let nextSessionId = 0
app.get('/checkin', async (req, res) => {
sessions[""+nextSessionId] = req.query;
const confirmButton = `<a href='/confirmed?session=${nextSessionId}'><H1>CONFIRM</H1></a>`;
const rejectButton = `<a href='/rejected?session=${nextSessionId}'><H1>REJECT</H1></a>`;
res.send(`<H1>Ticket info</H1>${JSON.stringify(req.query, null, "<br>")}${confirmButton}${rejectButton}`)
nextSessionId++;
});
app.get('/confirmed', async (req, res) => {
console.log(req.query);
console.log(sessions);
const { id, name, secret, ticketId } = sessions[req.query.session];
const ownerId = sha256(id+name);
const secretHash = sha256(secret);
const status = await checkIn(contractName, employee, Orbs.addressToBytes(ownerId), Orbs.addressToBytes(secretHash), ticketId, "CONFIRMED");
res.send(status);
});
app.get('/rejected', async (req, res) => {
console.log(req.query);
console.log(sessions);
const { id, name, secret, ticketId } = sessions[req.query.session];
const ownerId = sha256(id+name);
const secretHash = sha256(secret);
const status = await checkIn(contractName, employee, Orbs.addressToBytes(ownerId), Orbs.addressToBytes(secretHash), ticketId, "BAD_ID");
res.send(status);
});
app.get('/events', async (req, res) => {
const getEvents = require("./audit/flow").getEvents;
const events = await getEvents(auditContractName, employee);
res.send(events);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))