185 lines
4.4 KiB
JavaScript
185 lines
4.4 KiB
JavaScript
import { ID } from "./main.js";
|
|
|
|
export class Packet {
|
|
static _createBase(name) {
|
|
return {
|
|
type: name,
|
|
id: window.crypto.randomUUID(),
|
|
timestamp: Date.now(),
|
|
author: ID,
|
|
};
|
|
}
|
|
|
|
static _sign(data) {
|
|
// compute hash of data
|
|
let hasher = new jsSHA("SHA3-256", "TEXT");
|
|
hasher.update(JSON.stringify(data));
|
|
let hash = BigInt("0x" + hasher.getHash("HEX"));
|
|
|
|
// sign hash
|
|
let sig = window.rsa.privKey.decrypt(hash);
|
|
|
|
// return new packet
|
|
return {
|
|
sig: "0x" + sig.toString(16),
|
|
payload: data,
|
|
};
|
|
}
|
|
|
|
static createAnnounce(rsaKeyPair, paillierKeyPair) {
|
|
return this._sign({
|
|
...this._createBase("ANNOUNCE"),
|
|
rsaPubKey: rsaKeyPair.pubKey.toJSON(),
|
|
paillierPubKey: paillierKeyPair.pubKey.toJSON(),
|
|
});
|
|
}
|
|
|
|
static createDisconnect() {
|
|
return this._sign(this._createBase("DISCONNECT"));
|
|
}
|
|
|
|
static createKeepAlive() {
|
|
return this._sign(this._createBase("KEEPALIVE"));
|
|
}
|
|
|
|
static createSetReady(nowReady) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
action: "READY",
|
|
ready: nowReady,
|
|
});
|
|
}
|
|
|
|
static createRandomHMAC(sessionId, range, hmac, key) {
|
|
return this._sign({
|
|
...this._createBase("RANDOM"),
|
|
session: sessionId,
|
|
range: range,
|
|
stage: "COMMIT",
|
|
hmac: hmac,
|
|
key: key,
|
|
});
|
|
}
|
|
|
|
static createRandomNoise(sessionId, noise) {
|
|
return this._sign({
|
|
...this._createBase("RANDOM"),
|
|
session: sessionId,
|
|
stage: "REVEAL",
|
|
noise: noise,
|
|
});
|
|
}
|
|
|
|
static createBarrierSignal() {
|
|
return this._sign(this._createBase("BARRIER"));
|
|
}
|
|
|
|
static createRegionClaim(region, text) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
region: region,
|
|
cipherText: text,
|
|
});
|
|
}
|
|
|
|
static createReinforce(regionCipherTexts, verification) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
regions: regionCipherTexts,
|
|
verification: verification,
|
|
});
|
|
}
|
|
|
|
static createAction(action, startRegion, endRegion, amount) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
startRegion: startRegion,
|
|
endRegion: endRegion,
|
|
strength: amount,
|
|
action: action,
|
|
});
|
|
}
|
|
|
|
static createFortify(fortify) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
action: "FORTIFY",
|
|
fortify: fortify,
|
|
});
|
|
}
|
|
|
|
static createDefense(amount) {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
action: "DEFENSE",
|
|
amount: amount,
|
|
});
|
|
}
|
|
|
|
static createEndTurn() {
|
|
return this._sign({
|
|
...this._createBase("ACT"),
|
|
action: "END",
|
|
});
|
|
}
|
|
|
|
static createProofRequest(region) {
|
|
return this._sign({
|
|
...this._createBase("PROOF"),
|
|
stage: "REQUEST",
|
|
region: region,
|
|
});
|
|
}
|
|
|
|
static createProofConjecture(region, plainText, a) {
|
|
return this._sign({
|
|
...this._createBase("PROOF"),
|
|
stage: "CONJECTURE",
|
|
plainText: plainText,
|
|
a: a,
|
|
region: region,
|
|
});
|
|
}
|
|
|
|
static createProofChallenge(region, challenge) {
|
|
return this._sign({
|
|
...this._createBase("PROOF"),
|
|
stage: "CHALLENGE",
|
|
challenge: challenge,
|
|
region: region,
|
|
});
|
|
}
|
|
|
|
static createProof(region, z) {
|
|
return this._sign({
|
|
...this._createBase("PROOF"),
|
|
stage: "PROOF",
|
|
z: z,
|
|
region: region,
|
|
});
|
|
}
|
|
|
|
static createRegionProof(proof) {
|
|
return this._sign({
|
|
...this._createBase("RESOLVE"),
|
|
action: "MAINTAIN",
|
|
proof: proof,
|
|
});
|
|
}
|
|
|
|
static createRegionYield() {
|
|
return this._sign({
|
|
...this._createBase("RESOLVE"),
|
|
action: "YIELD",
|
|
});
|
|
}
|
|
|
|
static createRegionCapture(cipherText) {
|
|
return this._sign({
|
|
...this._createBase("RESOLVE"),
|
|
action: "CAPTURE",
|
|
cipherText: cipherText,
|
|
});
|
|
}
|
|
}
|