POW everything

This commit is contained in:
2023-08-18 17:56:50 +01:00
parent 2a851c442d
commit 1c5e61e020
7 changed files with 49 additions and 24 deletions

View File

@ -28,6 +28,8 @@ type EventBuilderHook = (ev: EventBuilder) => EventBuilder;
export class EventPublisher {
#pubKey: string;
#signer: EventSigner;
#pow?: number;
#miner?: PowMiner;
constructor(signer: EventSigner, pubKey: string) {
this.#signer = signer;
@ -59,14 +61,21 @@ export class EventPublisher {
return this.#pubKey;
}
/**
* Apply POW to every event
*/
pow(target:number, miner?: PowMiner) {
this.#pow = target;
this.#miner = miner;
}
#eb(k: EventKind) {
const eb = new EventBuilder();
return eb.pubKey(this.#pubKey).kind(k);
}
async #sign(eb: EventBuilder) {
const ev = eb.build();
return await this.#signer.sign(ev);
return await (this.#pow ? eb.pow(this.#pow, this.#miner) : eb).buildAndSign(this.#signer);
}
async nip4Encrypt(content: string, otherKey: string) {