dao-governance-framework/forum-network/public/classes/member.js

47 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-11-11 16:52:57 -06:00
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),
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-11 16:52:57 -06:00
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()));
}
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);
}
}