Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem to public on #399

Open
maccali opened this issue Apr 16, 2024 · 2 comments
Open

Problem to public on #399

maccali opened this issue Apr 16, 2024 · 2 comments

Comments

@maccali
Copy link

maccali commented Apr 16, 2024

"use client";

import { getEventHash, relayInit, signEvent } from "nostr-tools";

import {
  generateSecretKey,
  getPublicKey,
  finalizeEvent,
  verifyEvent,
} from "nostr-tools/pure";
import { AbstractRelay, Relay } from "nostr-tools/relay";

import { useEffect, useState } from "react";

export default function Home() {
  const [sk, setSk] = useState(generateSecretKey());
  const [pk, setPk] = useState(getPublicKey(sk));
  const [relay, setRelay] = useState<AbstractRelay | null>(null);
  const [pubStatus, setPubStatus] = useState("");
  const [newEvent, setNewEvent] = useState(null);
  const [events, setEvents] = useState(null);

  // const topicId =
  //   "d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9029"; // ID do tópico
  const topicId =
    "27a48981ef3d88d54bfab3117e8ae2f1b8bd5c49da6d510bc7d5bf6eda460b48"; // ID do tópico

  const connectRelay = async () => {
    // const relay = await Relay.connect("wss://relay.damus.io");
    const relay = await Relay.connect("wss://relay.nostr.band");

    console.log(`connected to ${relay.url}`);
    console.log("relay", relay);

    setRelay(relay);
  };

  const subscribeRelay = async (relay: any) => {
    const sub = relay.subscribe(
      [
        {
          kinds: [1], // Subscreva-se no tópico desejado
          "#e": [
            //   //      "b6adb87b63ac97c91deafa21a31494ce15a56e53cc1d1a74871dd84f283157ff",
            //   topicId,
            topicId,
          ],
        },
      ],
      {
        onevent(event) {
          console.log("we got the event we wanted:", event);
          // setEvents((oldEvents) => [...oldEvents, event]);
        },
        oneose() {
          console.log("??");
          sub.close();
        },
      }
    );
  };

  useEffect(() => {
    connectRelay();
  }, []);

  useEffect(() => {
    if (relay) {
      subscribeRelay(relay);
    }
  }, [relay]);

  const publishEvent = async () => {
    let eventTemplate = {
      kind: 1,
      created_at: Math.floor(Date.now() / 1000),
      tags: [["e", topicId]],
      content: `???? ${Math.random()}`,
    };

    console.log("publishEvent", eventTemplate);
    console.log("publishEvent sk", sk);

    // Adicione o topicId ao evento antes de finalizá-lo
    // eventTemplate.ids = [topicId];

    const signedEvent = finalizeEvent(eventTemplate, sk);
    let isGood = verifyEvent(signedEvent);

    console.log("isGood", isGood);
    if (relay) {
      console.log("signedEvent", signedEvent);

      await relay.publish(signedEvent);
    }
  };

  return (
    <div>
      <p>private key: {sk}</p>
      <p>public key: {pk}</p>
      {relay ? (
        <p>Connected to {relay.url}</p>
      ) : (
        <p>Could not connect to relay</p>
      )}
      <button onClick={() => publishEvent()}>Publish event</button>
      <p>Publish status: {pubStatus}</p>
      {newEvent ? (
        <p>Subscribed event content: {newEvent.content}</p>
      ) : (
        <p>no new event</p>
      )}
      {events !== null &&
        events.map((event) => (
          <p key={event.sig} style={{ borderStyle: "ridge", padding: 10 }}>
            {event.content}
          </p>
        ))}
    </div>
  );
}

I does'n get nothing on subscribe when a publish my message, is something wrong on publish?

@fiatjaf
Copy link
Collaborator

fiatjaf commented Apr 17, 2024

Looks good (or I couldn't easily spot anything wrong). But are you sure subscribeRelay is getting called? The two useEffect calls seem to be racy. Maybe merge subscribeRelay into connectRelay and make them sequential.

@maccali
Copy link
Author

maccali commented Apr 18, 2024

I solve the problem, thx, one more little question. Is pool.subscribeMany a function which observe the topics when the topics get inside?, or for this we need to use other thing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants