problem: can't publish merit updates

This commit is contained in:
gsovereignty
2024-07-16 17:10:39 +08:00
parent 931520acf6
commit 2c6d86d821
7 changed files with 153 additions and 114 deletions

View File

@@ -29,6 +29,11 @@
for (let z of $merits) { for (let z of $merits) {
let meritRequest = new MeritRequest(z); let meritRequest = new MeritRequest(z);
if (meritRequest.BasicValidation()) { if (meritRequest.BasicValidation()) {
if (meritRequest.Event.sig) {
//broadcast the events to our relays
//meritRequest.Event.ndk = $ndk
//meritRequest.Event.publish().then(r=>{ console.log(meritRequest.Event.pubkey,r)}).catch(e=>{})
}
map.set(meritRequest.ID, meritRequest); map.set(meritRequest.ID, meritRequest);
} }
} }

View File

@@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import * as Card from '$lib/components/ui/card/index.js'; import * as Card from '$lib/components/ui/card/index.js';
import { Vote, Votes, type MeritRequest } from '@/event_helpers/merits'; import { MapOfVotes, Vote, Votes, type MeritRequest } from '@/event_helpers/merits';
import { ndk } from '@/ndk'; import { ndk } from '@/ndk';
import { NDKEvent, type NDKKind } from '@nostr-dev-kit/ndk'; import { NDKEvent, type NDKKind } from '@nostr-dev-kit/ndk';
import { Avatar, Name } from '@nostr-dev-kit/ndk-svelte-components'; import { Avatar, Name } from '@nostr-dev-kit/ndk-svelte-components';
@@ -11,7 +11,7 @@
import { Separator } from '$lib/components/ui/separator/index.js'; import { Separator } from '$lib/components/ui/separator/index.js';
import * as Table from '@/components/ui/table'; import * as Table from '@/components/ui/table';
import { Rocket, RocketATagFilter } from '@/event_helpers/rockets'; import { Rocket, RocketATagFilter } from '@/event_helpers/rockets';
import { unixToRelativeTime } from '@/helpers'; import { getRocketURL, unixToRelativeTime } from '@/helpers';
import { derived } from 'svelte/store'; import { derived } from 'svelte/store';
import { Button } from '$lib/components/ui/button/index.js'; import { Button } from '$lib/components/ui/button/index.js';
@@ -20,6 +20,8 @@
import Alert from '@/components/ui/alert/alert.svelte'; import Alert from '@/components/ui/alert/alert.svelte';
import { currentUser } from '@/stores/session'; import { currentUser } from '@/stores/session';
import CornerDownLeft from 'lucide-svelte/icons/corner-down-left'; import CornerDownLeft from 'lucide-svelte/icons/corner-down-left';
import { goto } from '$app/navigation';
import { base } from '$app/paths';
export let merit: MeritRequest; export let merit: MeritRequest;
export let rocket: NDKEvent; export let rocket: NDKEvent;
@@ -38,30 +40,7 @@
}); });
let votes = derived(_votes, ($_votes) => { let votes = derived(_votes, ($_votes) => {
let vMap = new Map<string, Vote>(); return new MapOfVotes($_votes, parsedRocket, merit).Votes;
for (let v of $_votes) {
let vote = new Vote(v);
if (
vote.BasicValidation() &&
vote.ValidateAgainstRocket(new Rocket(rocket)) &&
vote.ValidateAgainstMeritRequest(merit)
) {
vMap.set(vote.ID, vote); //only show the latest vote from each pubkey
}
}
let pMap = new Map<string, Vote>();
for (let [_, v] of vMap) {
let existing = pMap.get(v.Pubkey);
if (!existing || (existing && existing.TimeStamp < v.TimeStamp)) {
//todo: check if this merit request has already been included in the rocket. If not, and if we have enough votes to approve it, update the rocket.
pMap.set(v.Pubkey, v);
}
}
vMap = new Map<string, Vote>();
for (let [_, v] of pMap) {
vMap.set(v.ID, v);
}
return vMap;
}); });
let rocketUpdates = derived([votes, currentUser], ([$votes, $currentUser]) => { let rocketUpdates = derived([votes, currentUser], ([$votes, $currentUser]) => {
@@ -69,13 +48,24 @@
if ($currentUser && parsedRocket && parsedRocket.VotePowerForPubkey($currentUser.pubkey) > 0) { if ($currentUser && parsedRocket && parsedRocket.VotePowerForPubkey($currentUser.pubkey) > 0) {
let votes = new Votes(Array.from($votes, ([_, v]) => v)); let votes = new Votes(Array.from($votes, ([_, v]) => v));
let result = votes.Results().Result(parsedRocket); let result = votes.Results().Result(parsedRocket);
if ( if (result && result == 'ratify' && !merit.IncludedInRocketState(parsedRocket)) {
result && let e = parsedRocket.CreateUnsignedAMRProof(merit, votes);
result == 'ratify' && if (e) {
!parsedRocket.ApprovedMeritRequests().get(votes.Request) e.ndk = $ndk;
) { e.sign().then(() => {
//todo: parsedRocket.AppendAMR(votes.ConstructProof()) if (parsedRocket.ValidateAMRProof(e)) {
// let updatedRocket = parsedRocket.UpsertAMR(merit, e);
if (updatedRocket) {
updatedRocket.ndk = $ndk;
updatedRocket.sign().then(() => {
updatedRocket.publish().then(() => {
goto(`${base}/rockets/${getRocketURL(rocket)}`);
});
});
}
}
});
}
} }
} }
return events; return events;

View File

@@ -262,3 +262,33 @@ export class VoteTally {
} }
} }
} }
export class MapOfVotes {
Votes: Map<string, Vote>;
constructor(votes:NDKEvent[], rocket:Rocket, merit:MeritRequest) {
this.Votes = new Map<string, Vote>();
for (let v of votes) {
let vote = new Vote(v);
if (
vote.BasicValidation() &&
vote.ValidateAgainstRocket(rocket) &&
vote.ValidateAgainstMeritRequest(merit) &&
!merit.IncludedInRocketState(rocket)
) {
this.Votes.set(vote.ID, vote); //only show the latest vote from each pubkey
}
}
let pMap = new Map<string, Vote>();
for (let [_, v] of this.Votes) {
let existing = pMap.get(v.Pubkey);
if (!existing || (existing && existing.TimeStamp < v.TimeStamp)) {
//todo: check if this merit request has already been included in the rocket. If not, and if we have enough votes to approve it, update the rocket.
pMap.set(v.Pubkey, v);
}
}
this.Votes = new Map<string, Vote>();
for (let [_, v] of pMap) {
this.Votes.set(v.ID, v);
}
}
}

View File

@@ -1,4 +1,5 @@
import { NDKEvent, type NDKTag } from '@nostr-dev-kit/ndk'; import { NDKEvent, type NDKTag } from '@nostr-dev-kit/ndk';
import { MapOfVotes, MeritRequest, Vote, Votes } from './merits';
export class Rocket { export class Rocket {
Event: NDKEvent; Event: NDKEvent;
@@ -27,24 +28,81 @@ export class Rocket {
} }
return amr; return amr;
} }
AppendAMR(amrProof: NDKEvent): NDKEvent | undefined { ValidateAMRProof(amrProof: NDKEvent): boolean {
//todo let result = false;
let request: NDKEvent | undefined = undefined; if (this.VotePowerForPubkey(amrProof.pubkey) > 0 && amrProof.verifySignature(true)) {
let votes: NDKEvent[] = []; let request: NDKEvent | undefined = undefined;
let _request = amrProof.getMatchingTags('request'); let votes: NDKEvent[] = [];
if (_request.length == 1) { let _request = amrProof.getMatchingTags('request');
try { if (_request.length == 1) {
request = JSON.parse(_request[0][1]); try {
} catch {} let __request = new NDKEvent(undefined, JSON.parse(_request[0][1]));
if (__request.verifySignature(true)) {
request = __request;
}
} catch {}
}
for (let v of amrProof.getMatchingTags('vote')) {
try {
let vEv = new NDKEvent(undefined, JSON.parse(v[1]));
if (vEv.verifySignature(true)) votes.push(vEv);
} catch {}
}
if (request && votes.length > 0) {
let parsedRequest = new MeritRequest(request);
let mapOfVotes = new MapOfVotes(votes, this, parsedRequest).Votes;
let parsedVotes = new Votes(Array.from(mapOfVotes, ([_, v]) => v));
let voteDirection = parsedVotes.Results().Result(this);
if (
voteDirection &&
voteDirection == 'ratify' &&
!parsedRequest.IncludedInRocketState(this)
) {
//note: if it is included in the rocket state, we might be validating this against a previous state
result = true;
}
}
} }
for (let v of amrProof.getMatchingTags('vote')) { return result;
try { }
votes.push(JSON.parse(v[1]));
} catch {} CreateUnsignedAMRProof(request: MeritRequest, votes: Votes): NDKEvent | undefined {
let proof: NDKEvent | undefined = undefined;
let hasInvalidSig = false;
if (request && request.Event && request.Event.sig && votes.Votes.length > 0) {
if (!request.Event.verifySignature(true)) {
hasInvalidSig = true
}
for (let v of votes.Votes) {
if (!(v.Event.sig && v.Event.verifySignature(true))) {
hasInvalidSig = true;
}
}
let result = votes.Results().Result(this);
if (result && result == 'ratify' && !request.IncludedInRocketState(this) && !hasInvalidSig) {
let e = new NDKEvent();
e.kind = 1411;
e.tags.push(['request', JSON.stringify(request.Event.rawEvent())]);
for (let v of votes.Votes) {
e.tags.push(['vote', JSON.stringify(v.Event.rawEvent())]);
}
proof = e;
}
} }
return; return proof;
//add the AMR to the rocket event, and also add a proof //add the AMR to the rocket event, and also add a proof
} }
UpsertAMR(request: MeritRequest, signedProof: NDKEvent): NDKEvent | undefined {
let event: NDKEvent | undefined = undefined;
if (this.ValidateAMRProof(signedProof)) {
event = new NDKEvent(this.Event.ndk, this.Event.rawEvent());
event.created_at = Math.floor(new Date().getTime() / 1000);
event.tags.push(['merit', `${request.Pubkey}:${request.ID}:0:0:${request.Merits}`]);
event.tags.push(['proof_full', JSON.stringify(signedProof)]);
updateIgnitionAndParentTag(event);
}
return event;
}
UpsertProduct(id: string, price: number, maxSales?: number): NDKEvent { UpsertProduct(id: string, price: number, maxSales?: number): NDKEvent {
let event = new NDKEvent(this.Event.ndk, this.Event.rawEvent()); let event = new NDKEvent(this.Event.ndk, this.Event.rawEvent());
event.created_at = Math.floor(new Date().getTime() / 1000); event.created_at = Math.floor(new Date().getTime() / 1000);

View File

@@ -7,12 +7,14 @@ import { browser } from '$app/environment';
const _ndk = new NDKSvelte({ const _ndk = new NDKSvelte({
explicitRelayUrls: [ explicitRelayUrls: [
'wss://purplepag.es', 'wss://purplepag.es',
'wss://relay.higlighter.com',
'wss://relay.nostr.band', 'wss://relay.nostr.band',
'wss://nos.lol' 'wss://nos.lol',
//'wss://relay.nostrocket.org' 'wss://relay.nostrocket.org',
'wss://nostr.mutinywallet.com',
'wss://relay.damus.io'
], ],
enableOutboxModel: false, enableOutboxModel: false,
clientName: 'nostrocket'
//clientNip89: "31990:fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52:1716498133442", //clientNip89: "31990:fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52:1716498133442",
}); });

View File

@@ -5,10 +5,7 @@
import type { ExtendedBaseType, NDKEventStore } from '@nostr-dev-kit/ndk-svelte'; import type { ExtendedBaseType, NDKEventStore } from '@nostr-dev-kit/ndk-svelte';
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import { derived, type Readable } from 'svelte/store'; import { derived, type Readable } from 'svelte/store';
import CreateNewProduct from '../../../components/CreateNewProduct.svelte';
import Heading from '../../../components/Heading.svelte'; import Heading from '../../../components/Heading.svelte';
import ProductCard from '../../../components/ProductCard.svelte';
import ProductsForRocket from '../../../components/ProductsForRocket.svelte';
import RocketDashboard from '../../../components/RocketDashboard.svelte'; import RocketDashboard from '../../../components/RocketDashboard.svelte';
//flow if we only have a d-tag: fetch all 31108's with this d-tag, sort by WoT, put Nostrocket Name Service one at the top. Dedupe same rocket (same state, shadows) from multiple users, just show them all as everyone agreeing. //flow if we only have a d-tag: fetch all 31108's with this d-tag, sort by WoT, put Nostrocket Name Service one at the top. Dedupe same rocket (same state, shadows) from multiple users, just show them all as everyone agreeing.
//second pass: fetch ignition event for each, rebuild current state and validate all proofs, compute votepower and display only the states with > 50%. //second pass: fetch ignition event for each, rebuild current state and validate all proofs, compute votepower and display only the states with > 50%.
@@ -44,21 +41,18 @@
} }
$: { $: {
if (rocketEvents) { if (rocketEvents && !latestRocketEvent) {
latestRocketEvent = derived(rocketEvents, ($events) => { latestRocketEvent = derived(rocketEvents, ($events) => {
if (rocketEvents) { let sorted = $events.filter((e) => {
let sorted = $events.filter((e) => { return e.kind == 31108;
return e.kind == 31108; });
}); sorted = sorted.toSorted((a, b) => {
sorted = sorted.toSorted((a, b) => { return a.created_at - b.created_at;
return a.created_at - b.created_at; });
}); return sorted[0];
return sorted[0];
}
return undefined;
}); });
if ($latestRocketEvent) { if ($latestRocketEvent && !candidateProducts) {
candidateProducts = derived(rocketEvents, ($events) => { candidateProducts = derived(rocketEvents, ($events) => {
return $events.filter((e) => { return $events.filter((e) => {
for (let p of $latestRocketEvent.getMatchingTags('product')) { for (let p of $latestRocketEvent.getMatchingTags('product')) {
@@ -89,29 +83,4 @@
IGNITION: {rIgnitionOrActual} <br /> IGNITION: {rIgnitionOrActual} <br />
NAME: {rName} <br /> NAME: {rName} <br />
PUBKEY: {rPubkey} <br /> PUBKEY: {rPubkey} <br />
{/if} {/if}
{#if latestRocketEvent && $latestRocketEvent && false}
<Heading title={$latestRocketEvent.getMatchingTags('d')[0][1]} />
<div class="flex flex-col gap-1 text-left">
<h3 class="text-xl font-bold tracking-tight">
{$latestRocketEvent.getMatchingTags('d')[0][1].toLocaleUpperCase()} Products
</h3>
<p class="text-sm text-muted-foreground">
If there are products available for purchase they will be listed here
</p>
</div>
<ProductsForRocket rocketEvent={$latestRocketEvent} />
<div class="flex flex-col gap-1 pt-4 text-left">
<h3 class="text-xl font-bold tracking-tight">
{$latestRocketEvent.getMatchingTags('d')[0][1].toLocaleUpperCase()} Product Proposals
</h3>
<p class="text-sm text-muted-foreground">
If particpants of {$latestRocketEvent.getMatchingTags('d')[0][1]} have proposed any new products
that are not yet included for sale, they will be listed here.
</p>
</div>
{#each $candidateProducts as r}<ProductCard rocket={$latestRocketEvent} product={r} />{/each}
<CreateNewProduct rocketEvent={$latestRocketEvent} />
{/if}

View File

@@ -32,7 +32,6 @@
$: { $: {
if (meritRequest && meritRequest.BasicValidation()) { if (meritRequest && meritRequest.BasicValidation()) {
//the user wants the latest valid state of this rocket
rocketEvents = $ndk.storeSubscribe([meritRequest.REQFilter()], { rocketEvents = $ndk.storeSubscribe([meritRequest.REQFilter()], {
subId: meritRequest.RocketTag!.split(':')[2] subId: meritRequest.RocketTag!.split(':')[2]
}); });
@@ -40,32 +39,18 @@
} }
$: { $: {
if (rocketEvents) { if (rocketEvents && !latestRocketEvent) {
latestRocketEvent = derived(rocketEvents, ($events) => { latestRocketEvent = derived(rocketEvents, ($events) => {
if (rocketEvents) { let sorted = $events.filter((e) => {
let sorted = $events.filter((e) => { return e.kind == 31108;
return e.kind == 31108; });
}); sorted = sorted.toSorted((a, b) => {
sorted = sorted.toSorted((a, b) => { return a.created_at - b.created_at;
return a.created_at - b.created_at; });
}); return sorted[0];
return sorted[0];
}
return undefined;
}); });
if ($latestRocketEvent) {
}
} }
} }
//todo: check that this zap is not already included in the payment JSON for the product
//todo: list purchases on the rocket page (from product tags, as well as zap receipts that aren't yet included). Deduct total products available if not 0.
//todo: make the page flash or something and show each time someone buys the product.
//todo: split this out so that we can consume it for the payment page too (so that we know if there are really products left or they're all sold)
//todo: make store of all purchases (in rocket and zaps), sort by timestamp and render with profile of buyer
//todo: handle shadow events (fetch the shadowed event and render it instead)
</script> </script>
{#if latestRocketEvent && $latestRocketEvent && meritRequest} {#if latestRocketEvent && $latestRocketEvent && meritRequest}