2021-04-10 10:49:23 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-10 14:01:35 +00:00
|
|
|
buildExposure() {
|
|
|
|
return {
|
|
|
|
clientID: this.id,
|
|
|
|
timestamp: (new Date).getTime(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 10:49:23 +00:00
|
|
|
async interactWith(otherClient) {
|
2021-04-10 10:50:51 +00:00
|
|
|
const hash = otherClient.getHash(this.id)
|
2021-04-10 10:49:23 +00:00
|
|
|
const message = openpgp.Message.fromText(hash)
|
|
|
|
const signature = (await openpgp.sign({
|
2021-04-10 12:09:08 +00:00
|
|
|
message,
|
|
|
|
privateKeys: await openpgp.readKey({
|
2021-04-10 10:49:23 +00:00
|
|
|
armoredKey: this.privateKey
|
|
|
|
})
|
2021-04-10 12:09:08 +00:00
|
|
|
}))
|
2021-04-10 10:49:23 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-04-10 14:01:35 +00:00
|
|
|
// Simulate an encounter between A and B
|
2021-04-10 10:49:23 +00:00
|
|
|
console.log(a_transact, b_transact)
|
2021-04-10 12:09:08 +00:00
|
|
|
postTo('/api/v1/encounter', a_transact)
|
|
|
|
postTo('/api/v1/encounter', b_transact)
|
2021-04-10 14:01:35 +00:00
|
|
|
|
|
|
|
// Simulate B being exposed
|
|
|
|
const b_exposure = client_b.buildExposure()
|
|
|
|
console.log(b_exposure)
|
|
|
|
postTo('/api/v1/exposure', b_exposure)
|
2021-04-10 10:49:23 +00:00
|
|
|
})()
|
|
|
|
|
2021-04-10 12:09:08 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|