import { Actor } from "./actor.js"; import { Action } from "./action.js"; import { PostMessage } from "./message.js"; import { CryptoUtil } from "./crypto.js"; export class Member extends Actor { constructor(name, scene) { super(name, scene); this.actions = { submitPost: new Action("submit post", scene), initiateValidationPool: new Action("initiate validation pool", scene), castVote: new Action("cast vote", scene), revealIdentity: new Action("reveal identity", scene), }; this.validationPools = new Map(); } async initialize() { this.reputationKey = await CryptoUtil.generateAsymmetricKey(); // this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey); this.reputationPublicKey = this.name; this.status.set("Initialized"); this.activate(); return this; } async submitPost(forumNode, post, stake) { // TODO: Include fee const postMessage = new PostMessage({ post, stake }); await postMessage.sign(this.reputationKey); this.actions.submitPost.log(this, forumNode, null, { id: post.id }); // For now, directly call forumNode.receiveMessage(); await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON())); } async initiateValidationPool(bench, options) { // For now, directly call bench.initiateValidationPool(); const signingKey = await CryptoUtil.generateAsymmetricKey(); const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey); this.actions.initiateValidationPool.log( this, bench, `(fee: ${options.fee})` ); const pool = bench.initiateValidationPool(this.reputationPublicKey, { ...options, signingPublicKey, }); this.validationPools.set(pool.id, { signingPublicKey }); return pool; } async castVote(validationPool, { position, stake, lockingTime }) { const signingKey = await CryptoUtil.generateAsymmetricKey(); const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey); this.validationPools.set(validationPool.id, { signingPublicKey }); // TODO: encrypt vote // TODO: sign message this.actions.castVote.log( this, validationPool, `(${position ? "for" : "against"}, stake: ${stake})` ); validationPool.castVote(signingPublicKey, position, stake, lockingTime); } async revealIdentity(validationPool) { const { signingPublicKey } = this.validationPools.get(validationPool.id); // TODO: sign message this.actions.revealIdentity.log(this, validationPool); validationPool.revealIdentity(signingPublicKey, this.reputationPublicKey); } }