-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (105 loc) · 5.86 KB
/
app.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
class App {
constructor(Orbs, { endpoint, prismEndpoint, virtualChainId, contractName, channel, employee }, { publicKey, privateKey, address }, { friendPublicKey, friendPrivateKey, friendAddress }) {
this.channel = channel;
this.publicKey = publicKey;
this.privateKey = privateKey;
this.address = address;
this.friendPublicKey = friendPublicKey;
this.friendPrivateKey = friendPrivateKey;
this.friendAddress = friendAddress;
this.friendId = "12344123435644";
this.friendName = "Hogeg";
this.virtualChainId = virtualChainId;
this.prismEndpoint = prismEndpoint;
this.contractName = contractName;
this.employee = employee;
this.tickets = [];
this.secretStore = {};
}
generateSecret() {
document.getElementById('one_time_secret').value = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
async buy(id, name, secret, orbsAddr) {
const ownerId = Orbs.addressToBytes(sha256(id+name));
const secretHash = Orbs.addressToBytes(sha256(secret));
const client = new Orbs.Client("http://localhost:8080", 42, Orbs.NetworkType.NETWORK_TYPE_TEST_NET);
const [ tx, txid ] = client.createTransaction(this.employee.publicKey, this.employee.privateKey, this.contractName, "buyTicket", [Orbs.argBytes(ownerId), Orbs.argBytes(secretHash), Orbs.argAddress(orbsAddr)]);
const result = await client.sendTransaction(tx);
console.log(result);
try {
const ticket = JSON.parse(result.outputArguments[0].value);
this.secretStore[ticket.ID] = {
id: id,
name: name,
secret: secret
};
this.tickets.push(ticket);
this.renderTickets();
// const messageId = await this.conversation.sendMessageToChannel(this.channel, text);
console.log(`got a ticket!`, ticket);
this.generateSecret()
} catch (e) {
alert(result.outputArguments[0].value)
}
return false;
}
async renderTickets() {
const container = document.getElementById("tickets");
container.innerHTML = "<div class=\"row\" style=\"margin-bottom: 10px;background: azure;\"><div class=\"column column-10\">Ticket ID</div>" +
"<div class=\"column column-20\">Ticket Status</div> <div class=\"column column-20\">Check in </div> " +
"<div class=\"column column-20\">Wrong info check in</div> <div class=\"column column-20\"> Allow a friend</div><div class=\"column column-20\"> Friend check in</div>" ;
for (const t of this.tickets) {
const row = document.createElement("div");
row.classList = ["row"];
let buttonAddFriend = `<button onclick='javascript:window.app.allowFriendButton(${t.ID})'>Add Friend!</button>`;
const id = this.secretStore[t.ID].id;
const name = this.secretStore[t.ID].name;
const secret = this.secretStore[t.ID].secret;
const wrongSecret = "Wrong secret"
let checkInLink = t.Status == "purchased" ? `<a href='http://localhost:4000/checkin?ticketId=${t.ID}&id=${id}&name=${name}&secret=${secret}' target=_blank>Check in link</a>` : "";
let checkInViaLinkWrongInfo = t.Status == "purchased" ? `<a href='http://localhost:4000/checkin?ticketId=${t.ID}&id=${id}&name=${name}&secret=${wrongSecret}' target=_blank>Check in link</a>` : "";
let checkAsFriend = t.Status == "purchased" ? `<a href='http://localhost:4000/checkin?ticketId=${t.ID}&id=${this.friendId}&name=${this.friendName}&secret=${secret}' target=_blank>Check in </a>` : "";
row.innerHTML = `<div class="column column-10">${t.ID}</div><div class="column column-20"><strong>${t.Status}</strong></div>
<div class="column column-20">${checkInLink}</div>
<div class="column column-20">${checkInViaLinkWrongInfo}</div>
<div class="column column-20">${buttonAddFriend}</div>
<div class="column column-20">${checkAsFriend}</div>`
container.appendChild(row, container.childNodes[0]);
}
}
async allowFriendButton(ticketId, secretOverride) {
const elem = this.secretStore[ticketId];
const friendId = Orbs.addressToBytes(sha256(this.friendId + this.friendName));
const secret = Orbs.addressToBytes(sha256(secretOverride || elem.secret));
const client = new Orbs.Client("http://localhost:8080", 42, Orbs.NetworkType.NETWORK_TYPE_TEST_NET);
const [ tx, txid ] = client.createTransaction(this.publicKey, this.privateKey, this.contractName, "addOwner", [Orbs.argUint32(ticketId), Orbs.argBytes(friendId), Orbs.argBytes(this.friendAddress)]);
const result = await client.sendTransaction(tx);
try {
const ticket = JSON.parse(result.outputArguments[0].value);
const t = _.find(this.tickets, { ID: ticket.ID });
_.assignIn(t, ticket);
this.renderTickets();
} catch (e) {
console.log(e)
alert(result.outputArguments[0].value);
}
return false;
}
async submitForm() {
const idInput = document.getElementById('id_number');
const nameInput = document.getElementById('name');
const secretInput = document.getElementById('one_time_secret');
await this.buy(idInput.value, nameInput.value, secretInput.value, this.address);
return false;
}
showInfo() {
this.setElementValue("address", this.address);
this.setElementValue("contract_name", this.contractName);
}
setElementValue(id, value) {
document.getElementById(id).innerHTML = value;
}
}