import { Action } from './action.js'; import { PostMessage } from './message.js'; import { CryptoUtil } from './crypto.js'; import { ReputationHolder } from './reputation-holder.js'; export class Expert extends ReputationHolder { constructor(name, scene) { super(undefined, name, scene); this.actions = { submitPostViaNetwork: new Action('submit post via network', scene), submitPost: new Action('submit post', scene), initiateValidationPool: new Action('initiate validation pool', scene), stake: new Action('stake on post', scene), registerAvailability: new Action('register availability', scene), getAssignedWork: new Action('get assigned work', scene), submitWork: new Action('submit work evidence', scene), }; this.validationPools = new Map(); this.tokens = []; } async initialize() { this.reputationKey = await CryptoUtil.generateAsymmetricKey(); // this.reputationPublicKey = await CryptoUtil.exportKey(this.reputationKey.publicKey); this.reputationPublicKey = this.name; this.status.set('Initialized'); return this; } async submitPostViaNetwork(forumNode, post, stake) { // TODO: Include fee const postMessage = new PostMessage({ post, stake }); await postMessage.sign(this.reputationKey); await this.actions.submitPostViaNetwork.log(this, forumNode, null, { id: post.id }); // For now, directly call forumNode.receiveMessage(); await forumNode.receiveMessage(JSON.stringify(postMessage.toJSON())); } async submitPost(forum, postContent) { // TODO: Include fee await this.actions.submitPost.log(this, forum); return forum.addPost(this.reputationPublicKey, postContent); } async submitPostWithFee(bench, forum, postContent, poolOptions) { await this.actions.submitPost.log(this, forum); const postId = await forum.addPost(this.reputationPublicKey, postContent); const pool = await this.initiateValidationPool(bench, { ...poolOptions, postId }); this.tokens.push(pool.tokenId); return { postId, pool }; } async initiateValidationPool(bench, poolOptions) { // For now, directly call bench.initiateValidationPool(); poolOptions.reputationPublicKey = this.reputationPublicKey; await this.actions.initiateValidationPool.log( this, bench, `(fee: ${poolOptions.fee}, stake: ${poolOptions.authorStakeAmount ?? 0})`, ); const pool = await bench.initiateValidationPool(poolOptions); this.tokens.push(pool.tokenId); this.validationPools.set(pool.id, poolOptions); return pool; } async stake(validationPool, { position, amount, lockingTime, }) { // TODO: encrypt stake // TODO: sign message await this.actions.stake.log( this, validationPool, `(${position ? 'for' : 'against'}, stake: ${amount})`, ); return validationPool.stake(this.reputationPublicKey, { position, amount, lockingTime, tokenId: this.tokens[0], }); } async registerAvailability(availability, stakeAmount, duration) { await this.actions.registerAvailability.log(this, availability, `(stake: ${stakeAmount}, duration: ${duration})`); this.workerId = await availability.register(this.reputationPublicKey, { stakeAmount, tokenId: this.tokens[0], duration, }); } async getAssignedWork(availability, business) { const requestId = await availability.getAssignedWork(this.workerId); const request = await business.getRequest(requestId); return request; } async submitWork(business, requestId, evidence, { tokenLossRatio, duration }) { await this.actions.submitWork.log(this, business); return business.submitWork(this.reputationPublicKey, requestId, evidence, { tokenLossRatio, duration }); } }