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

103 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-12-31 16:08:42 -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-12-31 16:08:42 -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),
2023-01-02 13:14:32 -06:00
registerAvailability: new Action('register availability', scene),
getAssignedWork: new Action('get assigned work', scene),
submitWork: new Action('submit work evidence', 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-12-31 16:08:42 -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,
2022-12-31 16:08:42 -06:00
`(fee: ${options.fee})`,
2022-11-30 09:13:52 -06:00
);
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
}
2023-01-02 13:14:32 -06:00
async castVote(validationPool, {
position, stake, lockingTime, anonymous = true,
}) {
let signingPublicKey;
if (anonymous) {
const signingKey = await CryptoUtil.generateAsymmetricKey();
signingPublicKey = await CryptoUtil.exportKey(signingKey.publicKey);
this.validationPools.set(validationPool.id, { signingPublicKey });
} else {
signingPublicKey = this.reputationPublicKey;
}
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,
2023-01-02 13:14:32 -06:00
`(${position ? 'for' : 'against'}, stake: ${stake}, anonymous: ${anonymous})`,
2022-11-30 09:13:52 -06:00
);
2023-01-02 13:14:32 -06:00
return validationPool.castVote(signingPublicKey, {
position, stake, lockingTime, anonymous,
});
2022-11-11 16:52:57 -06:00
}
2023-01-02 13:14:32 -06:00
async revealIdentity(validationPool) {
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
}
2023-01-01 21:09:02 -06:00
2023-01-02 13:14:32 -06:00
async registerAvailability(availability, stake) {
this.actions.registerAvailability.log(this, availability, `(stake: ${stake})`);
await availability.register(this.reputationPublicKey, stake);
}
async getAssignedWork(availability, business) {
const requestId = await availability.getAssignedWork(this.reputationPublicKey);
const request = await business.getRequest(requestId);
return request;
}
async submitWork(business, requestId, evidence, { tokenLossRatio, duration }) {
this.actions.submitWork.log(this, business);
return business.submitWork(this.reputationPublicKey, requestId, evidence, { tokenLossRatio, duration });
2023-01-01 21:09:02 -06:00
}
}