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

74 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-11-30 09:13:52 -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 = {
2022-11-30 09:13:52 -06:00
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),
};
2022-11-13 12:23:30 -06:00
this.validationPools = new Map();
}
async initialize() {
2022-11-12 16:20:42 -06:00
this.reputationKey = await CryptoUtil.generateAsymmetricKey();
2022-11-17 08:30:06 -06:00
// this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey);
this.reputationPublicKey = this.name;
2022-11-30 09:13:52 -06:00
this.status.set("Initialized");
2022-11-13 12:23:30 -06:00
this.activate();
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-17 08:30:06 -06:00
async initiateValidationPool(bench, options) {
// For now, directly call bench.initiateValidationPool();
2022-11-17 08:30:06 -06:00
const signingKey = await CryptoUtil.generateAsymmetricKey();
2022-11-30 09:13:52 -06:00
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 });
2022-11-17 08:30:06 -06:00
return pool;
2022-11-12 16:20:42 -06:00
}
2022-11-30 09:13:52 -06:00
async castVote(validationPool, { position, stake, lockingTime }) {
2022-11-12 16:20:42 -06:00
const signingKey = await CryptoUtil.generateAsymmetricKey();
2022-11-30 09:13:52 -06:00
const signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey);
this.validationPools.set(validationPool.id, { signingPublicKey });
2022-11-12 16:20:42 -06:00
// TODO: encrypt vote
// TODO: sign message
2022-11-30 09:13:52 -06:00
this.actions.castVote.log(
this,
validationPool,
`(${position ? "for" : "against"}, stake: ${stake})`
);
2022-11-13 12:23:30 -06:00
validationPool.castVote(signingPublicKey, position, stake, lockingTime);
2022-11-11 16:52:57 -06:00
}
2022-11-13 12:23:30 -06:00
async revealIdentity(validationPool) {
2022-11-30 09:13:52 -06:00
const { signingPublicKey } = this.validationPools.get(validationPool.id);
2022-11-12 16:20:42 -06:00
// TODO: sign message
2022-11-11 16:52:57 -06:00
this.actions.revealIdentity.log(this, validationPool);
2022-11-13 12:23:30 -06:00
validationPool.revealIdentity(signingPublicKey, this.reputationPublicKey);
2022-11-11 16:52:57 -06:00
}
}