2023-03-13 14:52:14 +00:00
|
|
|
import { ID, game } from "./main.js";
|
2023-03-03 17:34:15 +00:00
|
|
|
|
|
|
|
export class Packet {
|
2023-02-02 11:27:52 +00:00
|
|
|
static _createBase(name) {
|
|
|
|
return {
|
|
|
|
type: name,
|
|
|
|
id: window.crypto.randomUUID(),
|
2023-03-07 15:43:47 +00:00
|
|
|
timestamp: Date.now(),
|
2023-02-02 11:27:52 +00:00
|
|
|
author: ID,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-04 14:19:26 +00:00
|
|
|
static _sign(data) {
|
|
|
|
// compute hash of data
|
|
|
|
let hash = CryptoJS.SHA3(JSON.stringify(data));
|
|
|
|
let res = 0n;
|
|
|
|
for (let word32 of hash.words) {
|
|
|
|
// remove any sign
|
|
|
|
let bi = BigInt.asUintN(32, BigInt(word32));
|
|
|
|
res = (res << 32n) | bi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sign hash
|
|
|
|
let sig = window.rsa.privKey.decrypt(res);
|
|
|
|
|
|
|
|
// return new packet
|
2023-02-02 11:27:52 +00:00
|
|
|
return {
|
2023-03-04 14:19:26 +00:00
|
|
|
sig: "0x" + sig.toString(16),
|
|
|
|
payload: data,
|
2023-02-02 11:27:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:52:14 +00:00
|
|
|
static createAnnounce(rsaKeyPair, paillierKeyPair) {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign({
|
|
|
|
...this._createBase("ANNOUNCE"),
|
|
|
|
rsaPubKey: rsaKeyPair.pubKey.toJSON(),
|
2023-03-13 14:52:14 +00:00
|
|
|
paillierPubKey: paillierKeyPair.pubKey.toJSON(),
|
2023-03-04 14:19:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:27:52 +00:00
|
|
|
static createDisconnect() {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign(this._createBase("DISCONNECT"));
|
2023-02-02 11:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static createKeepAlive() {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign(this._createBase("KEEPALIVE"));
|
2023-02-02 11:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static createSetReady(nowReady) {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign({
|
2023-03-03 17:34:15 +00:00
|
|
|
...this._createBase("ACT"),
|
|
|
|
action: "READY",
|
2023-02-02 11:27:52 +00:00
|
|
|
ready: nowReady,
|
2023-03-04 14:19:26 +00:00
|
|
|
});
|
2023-02-02 11:27:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 17:19:37 +00:00
|
|
|
static createRandomCyphertext(sessionId, range, cipherText) {
|
|
|
|
return this._sign({
|
|
|
|
...this._createBase("RANDOM"),
|
|
|
|
session: sessionId,
|
|
|
|
range: range,
|
|
|
|
stage: "CIPHERTEXT",
|
|
|
|
cipherText: cipherText,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static createRandomKey(sessionId, key) {
|
|
|
|
return this._sign({
|
|
|
|
...this._createBase("RANDOM"),
|
|
|
|
session: sessionId,
|
|
|
|
stage: "DECRYPT",
|
|
|
|
cipherKey: key,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-02 11:27:52 +00:00
|
|
|
static createBarrierSignal() {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign(this._createBase("BARRIER"));
|
2023-02-02 11:27:52 +00:00
|
|
|
}
|
2023-02-06 11:04:37 +00:00
|
|
|
|
|
|
|
static createRegionClaim(region) {
|
2023-03-05 17:19:37 +00:00
|
|
|
return this._sign({
|
2023-02-10 15:47:21 +00:00
|
|
|
...this._createBase("ACT"),
|
|
|
|
region: region,
|
2023-03-05 17:19:37 +00:00
|
|
|
});
|
2023-02-10 15:47:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 14:52:14 +00:00
|
|
|
static createReinforce(region) {
|
|
|
|
// todo cache some of these, possibly by pregenerating cyphertexts in a web worker
|
|
|
|
let regions = {};
|
|
|
|
for (let ourRegion of game.us.getRegions()) {
|
|
|
|
if (ourRegion.name !== region) {
|
|
|
|
regions[ourRegion.name] =
|
|
|
|
"0x" + game.us.paillierPubKey.encrypt(0n).toString(16);
|
|
|
|
} else {
|
|
|
|
regions[ourRegion.name] =
|
|
|
|
"0x" + game.us.paillierPubKey.encrypt(1n).toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._sign({
|
|
|
|
...this._createBase("ACT"),
|
|
|
|
regions: regions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:46:21 +00:00
|
|
|
static createAction(action, startRegion, endRegion, amount) {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign({
|
2023-02-10 15:47:21 +00:00
|
|
|
...this._createBase("ACT"),
|
2023-02-17 12:46:21 +00:00
|
|
|
startRegion: startRegion,
|
|
|
|
endRegion: endRegion,
|
|
|
|
strength: amount,
|
|
|
|
action: action,
|
2023-03-04 14:19:26 +00:00
|
|
|
});
|
2023-02-06 11:04:37 +00:00
|
|
|
}
|
2023-02-16 09:38:03 +00:00
|
|
|
|
2023-02-18 15:12:06 +00:00
|
|
|
static createDefense(amount) {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign({
|
2023-02-18 15:12:06 +00:00
|
|
|
...this._createBase("ACT"),
|
|
|
|
action: "DEFENSE",
|
|
|
|
amount: amount,
|
2023-03-04 14:19:26 +00:00
|
|
|
});
|
2023-02-18 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 09:38:03 +00:00
|
|
|
static createEndTurn() {
|
2023-03-04 14:19:26 +00:00
|
|
|
return this._sign({
|
2023-02-16 09:38:03 +00:00
|
|
|
...this._createBase("ACT"),
|
|
|
|
action: "END",
|
2023-03-04 14:19:26 +00:00
|
|
|
});
|
2023-02-16 09:38:03 +00:00
|
|
|
}
|
2023-02-02 11:27:52 +00:00
|
|
|
}
|