POW everything
This commit is contained in:
parent
2a851c442d
commit
1c5e61e020
@ -1,6 +1,10 @@
|
||||
import useLogin from "Hooks/useLogin";
|
||||
import { DefaultPowWorker } from "index";
|
||||
|
||||
export default function useEventPublisher() {
|
||||
const { publisher } = useLogin();
|
||||
const { publisher, preferences } = useLogin();
|
||||
if (preferences.pow) {
|
||||
publisher?.pow(preferences.pow, DefaultPowWorker);
|
||||
}
|
||||
return publisher;
|
||||
}
|
||||
|
@ -66,6 +66,11 @@ export interface UserPreferences {
|
||||
* Auto-zap every post
|
||||
*/
|
||||
autoZap: boolean;
|
||||
|
||||
/**
|
||||
* Proof-of-Work to apply to all events
|
||||
*/
|
||||
pow?: number;
|
||||
}
|
||||
|
||||
export const DefaultPreferences = {
|
||||
|
@ -157,6 +157,24 @@ const PreferencesPage = () => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="card flex">
|
||||
<div className="flex f-col f-grow">
|
||||
<div>
|
||||
<FormattedMessage defaultMessage="Proof of Work" />
|
||||
</div>
|
||||
<small>
|
||||
<FormattedMessage defaultMessage="Amount of work to apply to all published events" />
|
||||
</small>
|
||||
</div>
|
||||
<div>
|
||||
<input
|
||||
type="number"
|
||||
defaultValue={perf.pow}
|
||||
min={0}
|
||||
onChange={e => updatePreferences(login, { ...perf, pow: parseInt(e.target.value || "0") })}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="card flex">
|
||||
<div className="flex f-col f-grow">
|
||||
<div>
|
||||
|
@ -1,17 +1,9 @@
|
||||
import { ExternalStore, dedupe } from "@snort/shared";
|
||||
import {
|
||||
EventKind,
|
||||
NostrPrefix,
|
||||
encodeTLVEntries,
|
||||
TLVEntryType,
|
||||
TLVEntry,
|
||||
decodeTLV,
|
||||
PowWorker,
|
||||
NostrEvent,
|
||||
} from "@snort/system";
|
||||
import { EventKind, NostrPrefix, encodeTLVEntries, TLVEntryType, TLVEntry, decodeTLV, NostrEvent } from "@snort/system";
|
||||
import { GiftWrapCache } from "Cache/GiftWrapCache";
|
||||
import { UnwrappedGift } from "Db";
|
||||
import { Chat, ChatSystem, ChatType, lastReadInChat } from "chat";
|
||||
import { DefaultPowWorker } from "index";
|
||||
|
||||
export class Nip24ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem {
|
||||
#cache: GiftWrapCache;
|
||||
@ -107,17 +99,10 @@ export class Nip24ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
|
||||
const messages: Array<Promise<NostrEvent>> = [];
|
||||
const powTarget = 4 * 4; // 4-char zero
|
||||
for (const pt of participants) {
|
||||
const recvSealedN = pub.giftWrap(
|
||||
await pub.sealRumor(gossip, pt.id),
|
||||
pt.id,
|
||||
powTarget,
|
||||
new PowWorker("/pow.js")
|
||||
);
|
||||
const recvSealedN = pub.giftWrap(await pub.sealRumor(gossip, pt.id), pt.id, powTarget);
|
||||
messages.push(recvSealedN);
|
||||
}
|
||||
messages.push(
|
||||
pub.giftWrap(await pub.sealRumor(gossip, pub.pubKey), pub.pubKey, powTarget, new PowWorker("/pow.js"))
|
||||
);
|
||||
messages.push(pub.giftWrap(await pub.sealRumor(gossip, pub.pubKey), pub.pubKey, powTarget, DefaultPowWorker));
|
||||
return await Promise.all(messages);
|
||||
},
|
||||
sendMessage: (ev, system) => {
|
||||
|
@ -6,7 +6,7 @@ import { StrictMode } from "react";
|
||||
import * as ReactDOM from "react-dom/client";
|
||||
import { Provider } from "react-redux";
|
||||
import { createBrowserRouter, RouterProvider } from "react-router-dom";
|
||||
import { EventPublisher, NostrSystem, ProfileLoaderService, Nip7Signer } from "@snort/system";
|
||||
import { EventPublisher, NostrSystem, ProfileLoaderService, Nip7Signer, PowWorker } from "@snort/system";
|
||||
|
||||
import * as serviceWorkerRegistration from "serviceWorkerRegistration";
|
||||
import { IntlProvider } from "IntlProvider";
|
||||
@ -60,6 +60,11 @@ export const System = new NostrSystem({
|
||||
*/
|
||||
export const ProfileLoader = new ProfileLoaderService(System, UserCache);
|
||||
|
||||
/**
|
||||
* Singleton POW worker
|
||||
*/
|
||||
export const DefaultPowWorker = new PowWorker("/pow.js");
|
||||
|
||||
// @ts-expect-error Setting webpack nonce
|
||||
window.__webpack_nonce__ = "ZmlhdGphZiBzYWlkIHNub3J0LnNvY2lhbCBpcyBwcmV0dHkgZ29vZCwgd2UgbWFkZSBpdCE=";
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
/// <reference lib="webworker" />
|
||||
import {} from ".";
|
||||
declare const self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { clientsClaim } from "workbox-core";
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user