2022-11-11 16:52:57 -06:00
|
|
|
import { Actor } from './actor.js';
|
|
|
|
import { Action } from './action.js';
|
2022-11-07 17:44:57 -06:00
|
|
|
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),
|
2022-11-11 16:52:57 -06:00
|
|
|
initiateVote: new Action('initiate vote', scene),
|
|
|
|
castVote: new Action('cast vote', scene),
|
|
|
|
revealIdentity: new Action('reveal identity', scene),
|
2022-11-07 17:44:57 -06:00
|
|
|
};
|
2022-11-11 16:52:57 -06:00
|
|
|
this.votes = new Map();
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
2022-11-11 16:52:57 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-11-07 17:44:57 -06:00
|
|
|
}
|