-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_clients.js
82 lines (69 loc) · 2.32 KB
/
test_clients.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
const fs = require('fs')
const openpgp = require('openpgp')
const bcrypt = require('bcrypt')
class TestClient {
constructor(keyName, id) {
this.keyName = keyName
this.id = id
this.publicKey = fs.readFileSync(`./${this.keyName}.public.gpg.key`).toString('utf-8')
this.privateKey = fs.readFileSync(`./${this.keyName}.private.gpg.key`).toString('utf-8')
this.salt = fs.readFileSync(`./${this.keyName}.salt`).toString('utf-8')
}
getHash(otherClientId) {
const [lesserId, greaterId] = [otherClientId, this.id].sort()
return bcrypt.hashSync(`${lesserId}-${greaterId}`, this.salt)
}
buildExposure() {
return {
clientID: this.id,
timestamp: (new Date).getTime(),
}
}
async interactWith(otherClient) {
const hash = otherClient.getHash(this.id)
const message = openpgp.Message.fromText(hash)
const signature = (await openpgp.sign({
message,
privateKeys: await openpgp.readKey({
armoredKey: this.privateKey
})
}))
return {
combinedHash: hash,
timestamp: (new Date).getTime(),
encodedGPSLocation: JSON.stringify({ none: true }),
partnerPublicKey: otherClient.publicKey,
validationSignature: signature
}
}
}
;(async () => {
const client_a = new TestClient('test_a', 'a893irwe')
const client_b = new TestClient('test_b', 'bawesfdi')
const a_transact = await client_a.interactWith(client_b)
const b_transact = await client_b.interactWith(client_a)
// Simulate an encounter between A and B
console.log(a_transact, b_transact)
postTo('/api/v1/encounter', a_transact)
postTo('/api/v1/encounter', b_transact)
// Simulate B being exposed
const b_exposure = client_b.buildExposure()
console.log(b_exposure)
postTo('/api/v1/exposure', b_exposure)
})()
function postTo(endpoint, data) {
data = JSON.stringify(data)
const options = {
hostname: 'localhost',
port: 8000,
path: endpoint,
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': data.length,
}
}
const req = require('http').request(options)
req.write(data)
req.end()
}