nostr-dev-kit / ndk

Nostr Development Kit with outbox-model support
MIT License
359 stars 96 forks source link

Calling publish directly on the NDK instance does not auto-sign the event #60

Closed steliosrammos closed 1 year ago

steliosrammos commented 1 year ago

When calling NDKEvent.publish the event is auto-signed, but when calling NDK.publish it is not.

Suggested solution:

The NDK.publish could attempt to auto-sign the event as well, based on the signer.

If there is not signer then it'd be helpful to throw an NDK error like "Attempting to publish unsigned event". Right now you'd rely on the relay letting you know the signature is missing, which they don't always do.

Reproducible Code

require('websocket-polyfill');

const { default: NDK } = require('@nostr-dev-kit/ndk');
const { NDKEvent, NDKRelay, NDKRelaySet, NDKPrivateKeySigner } = require('@nostr-dev-kit/ndk');

console.log(NDK);
const signer = new NDKPrivateKeySigner('826077ecbf2e20545a243086a4fc4ebc27e4aa4df89294bc9526b3b944aace4a');
main = async () => {
    try {
        const ndk = new NDK({ signer, explicitRelayUrls: ['wss://relay.damus.io/'] });
        await ndk.connect();

        const ndkRelays = [new NDKRelay('wss://relay.damus.io/')];
        const relaySet = new NDKRelaySet(ndkRelays, ndk);
        relaySet.relays.forEach(async (relay) => {
            await relay.connect();

            relay.on('connect', () => {
                console.log('connected');
            });
        });

        const ndkEvent = new NDKEvent(ndk);
        ndkEvent.kind = 1;
        ndkEvent.content = "Hello, world!";

        // const successfulRelays = await ndkEvent.publish(relaySet, 3000).catch((err) => console.error(err)); <-- this works
        // this doesn't work
        const successfulRelays = await ndk.publish(ndkEvent, relaySet, 3000).catch((err) => console.error(err));

        console.log('relays', successfulRelays);
    } catch (err) {
        console.error(err);
    }
}

main();
pablof7z commented 1 year ago

I think ndk.publish was a bad idea -- I'm adding a deprecation notice to it in favor of using event.publish() instead (which does the signing for you)