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), initiateVote: new Action('initiate vote', scene), castVote: new Action('cast vote', scene), revealIdentity: new Action('reveal identity', scene), }; this.votes = new Map(); } async initialize() { this.keyPair = await CryptoUtil.generateSigningKey(); this.status.set('Initialized'); return this; } async submitPost(forumNode, post, stake) { const postMessage = new PostMessage({ post, stake }); await postMessage.sign(this.keyPair); this.actions.submitPost.log(this, forumNode, null, { id: post.id }); // For now, directly call forumNode.receiveMessage(); await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON())); } async castVote(validationPool, voteId, position, stake) { const signingKey = await CryptoUtil.generateSigningKey(); this.votes.set(voteId, {signingKey}); // TODO: signed CastVoteMessage this.actions.castVote.log(this, validationPool); validationPool.castVote(voteId, signingKey.publicKey, position, stake); } async revealIdentity(validationPool, voteId) { const {signingKey} = this.votes.get(voteId); // TODO: signed RevealIdentityMessage this.actions.revealIdentity.log(this, validationPool); validationPool.revealIdentity(voteId, signingKey.publicKey, this.keyPair.publicKey); } }