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

57 lines
2.1 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() {
2022-11-12 16:20:42 -06:00
this.reputationKey = await CryptoUtil.generateAsymmetricKey();
this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey);
this.status.set('Initialized');
return this;
}
async submitPost(forumNode, post, stake) {
2022-11-12 16:20:42 -06:00
// TODO: Include fee
const postMessage = new PostMessage({ post, stake });
2022-11-12 16:20:42 -06:00
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()));
}
2022-11-11 16:52:57 -06:00
2022-11-12 16:20:42 -06:00
initiateVote(validationPool, options) {
// For now, directly call validationPool.initiateVote();
this.actions.initiateVote.log(this, validationPool);
return validationPool.initiateVote(this.reputationPublicKey, options);
}
async castVote(validationPool, voteId, position, stake, lockingTime) {
const signingKey = await CryptoUtil.generateAsymmetricKey();
const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey)
this.votes.set(voteId, {signingPublicKey});
// TODO: encrypt vote
// TODO: sign message
2022-11-11 16:52:57 -06:00
this.actions.castVote.log(this, validationPool);
2022-11-12 16:20:42 -06:00
validationPool.castVote(voteId, signingPublicKey, position, stake, lockingTime);
2022-11-11 16:52:57 -06:00
}
async revealIdentity(validationPool, voteId) {
2022-11-12 16:20:42 -06:00
const {signingPublicKey} = this.votes.get(voteId);
// TODO: sign message
2022-11-11 16:52:57 -06:00
this.actions.revealIdentity.log(this, validationPool);
2022-11-12 16:20:42 -06:00
validationPool.revealIdentity(voteId, signingPublicKey, this.reputationPublicKey);
2022-11-11 16:52:57 -06:00
}
}