Riskless/static/js/modules/interface/packet.js

185 lines
4.4 KiB
JavaScript
Raw Normal View History

import { ID } from "./main.js";
export class Packet {
static _createBase(name) {
return {
type: name,
id: window.crypto.randomUUID(),
2023-03-07 15:43:47 +00:00
timestamp: Date.now(),
author: ID,
};
}
2023-03-04 14:19:26 +00:00
static _sign(data) {
// compute hash of data
2023-05-02 13:19:15 +00:00
let hasher = new jsSHA("SHA3-256", "TEXT");
hasher.update(JSON.stringify(data));
let hash = BigInt("0x" + hasher.getHash("HEX"));
2023-03-04 14:19:26 +00:00
// sign hash
2023-05-02 13:19:15 +00:00
let sig = window.rsa.privKey.decrypt(hash);
2023-03-04 14:19:26 +00:00
// return new packet
return {
2023-03-04 14:19:26 +00:00
sig: "0x" + sig.toString(16),
payload: data,
};
}
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
});
}
static createDisconnect() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("DISCONNECT"));
}
static createKeepAlive() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("KEEPALIVE"));
}
static createSetReady(nowReady) {
2023-03-04 14:19:26 +00:00
return this._sign({
...this._createBase("ACT"),
action: "READY",
ready: nowReady,
2023-03-04 14:19:26 +00:00
});
}
2023-05-02 13:19:15 +00:00
static createRandomHMAC(sessionId, range, hmac, key) {
2023-03-05 17:19:37 +00:00
return this._sign({
...this._createBase("RANDOM"),
session: sessionId,
range: range,
2023-05-02 13:19:15 +00:00
stage: "COMMIT",
hmac: hmac,
key: key,
2023-03-05 17:19:37 +00:00
});
}
2023-05-02 13:19:15 +00:00
static createRandomNoise(sessionId, noise) {
2023-03-05 17:19:37 +00:00
return this._sign({
...this._createBase("RANDOM"),
session: sessionId,
2023-05-02 13:19:15 +00:00
stage: "REVEAL",
noise: noise,
2023-03-05 17:19:37 +00:00
});
}
static createBarrierSignal() {
2023-03-04 14:19:26 +00:00
return this._sign(this._createBase("BARRIER"));
}
2023-02-06 11:04:37 +00:00
2023-03-17 10:42:11 +00:00
static createRegionClaim(region, text) {
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-17 10:42:11 +00:00
cipherText: text,
2023-03-05 17:19:37 +00:00
});
2023-02-10 15:47:21 +00:00
}
2023-04-18 19:29:39 +00:00
static createReinforce(regionCipherTexts, verification) {
2023-03-13 14:52:14 +00:00
return this._sign({
...this._createBase("ACT"),
regions: regionCipherTexts,
2023-04-18 19:29:39 +00:00
verification: verification,
2023-03-13 14:52:14 +00:00
});
}
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-04-27 11:52:02 +00:00
static createFortify(fortify) {
2023-04-24 13:20:44 +00:00
return this._sign({
...this._createBase("ACT"),
2023-04-27 11:52:02 +00:00
action: "FORTIFY",
2023-04-24 13:20:44 +00:00
fortify: fortify,
});
}
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-04-10 10:19:11 +00:00
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,
});
}
2023-04-30 17:42:52 +00:00
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,
});
}
}