Ruulul / hyper-nostr

33 stars 9 forks source link

📒 Cook Book - Problem 1: Missed Messages #6

Open benzmuircroft opened 10 months ago

benzmuircroft commented 10 months ago

Please note that this code is more than likely totally wrong (Until updated/fixed by @5-142857 as discussed on discord)

NIPs

For a full list of the Nostr protocol NIPS (Nostr Implementation Protocols) see: nostr-protocol/nips

📒 Missed Messages

Problem: In a p2p application with no central sever, any time a user is online, they will see messages meant for them but, they may not always be online. This example assumes a user is offline and would otherwise miss their messages if it were not for the implementation of hyper-nostr

Conclusion: Nostr does not miss messages for offline users but, p2p apps without hyper-nostr do!

Other assumptions

(Should be clarified)

;(async function(){
  const SDK = await import("hyper-sdk");
  const createSwarm = await import('hyper-nostr');
  const { generatePrivateKey, getPublicKey, getEventHash, signEvent } = require('nostr-tools');
  const goodbye = require('graceful-goodbye');

  const yourStorageFolder1 = './missed-events1';
  const yourStorageFolder2 = './missed-events1';

  const sdk1 = await SDK.create({
    storage: yourStorageFolder1,
    autoJoin: true
  });

  const sdk2 = await SDK.create({
    storage: yourStorageFolder2,
    autoJoin: true
  });

  goodbye(() => {
    sdk1.close();
    sdk2.close();
  });

  const bob = await createSwarm(sdk1, 'missed-events'); // an online user bob, who knows that alice is offline
  bob.priv = generatePrivateKey();
  bob.pub = getPublicKey(bob.priv);

  let ev = {
    kind: 1000, // regular (see nip 1)
    created_at: Math.floor(+new Date() / 1000), // seconds
    pubkey: bob.pub,
    content: {
      type: 'payment',
      currency: 'usd',
      amount: '59.74043',
      for: 'art shop painting 63'
    },
    tags: [
      ['p', 'alice']
    ]
  };

  ev.id = getEventHash(ev);
  ev.sign = signEvent(ev, bob.priv);

  bob.sendEvent('EVENT', ev);

  await bob.update();

  console.log(await bob.queryEvents([{ '#p': ['alice'] }]));

  // ... later alice comes back online ...

  const alice = await createSwarm(sdk2, 'missed-events');
  alice.priv = generatePrivateKey();
  alice.pub = getPublicKey(alice.priv);

  await alice.update();

  let result = await alice.queryEvents([{ '#p': ['alice'] }]);

  // alice sees this:
  /*
  [ // a list
    {
      id: '57e75e7de7e75e65yghfh546',
      kind: 1000, // regular (see nip 1)
      created_at: 1239455461100,
      pubkey: 'd75d7dutdy5ehtbgeguyjgfu'
      content: {
        type: 'payment',
        currency: 'usd',
        amount: '59.74043',
        for: 'art shop painting 63'
      },
      tags: [
        ['p', 'alice']
      ]
    }
  ]
  */

  ev = {
    kind: 5, // delete (see nip 9)
    content: '',
    created_at: Math.floor(+new Date() / 1000), // seconds
    pubkey: alice.pub,
    tags: [
      ['e', result[0].id]
    ]
  };

  ev.id = getEventHash(ev);
  ev.sign = signEvent(ev, alice.priv);

  alice.sendEvent('EVENT', ev);

  await alice.update();

})();