Ruulul / hyper-nostr

33 stars 9 forks source link

📒 Cook Book - Learning: Hyper-nostr API #10

Open benzmuircroft opened 9 months ago

benzmuircroft commented 9 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

📒 Hyper-nostr API

A full list of the hyper-nostr API

Send A REQ Event To Subscribe To A Subscription

const me = await createSwarm(sdk, 'topic');

me.sendEvent('REQ', 'news', {});

# or with filters

me.sendEvent('REQ', 'news', {
  id: '<32-bytes lowercase hex-encoded sha256 of the serialized event data>', // see nostr-tools module
  pubkey: '<32-bytes lowercase hex-encoded public key of the event creator>', // see nostr-tools module
  created_at: '<unix timestamp in seconds>',
  kind: '<integer between 0 and 65535>',
  tags: [
    ['<arbitrary string>...'],
    ...
  ],
  content: '<arbitrary string>',
  sig: '<64-bytes lowercase hex of the signature of the sha256 hash of the serialized event data, which is the same as the "id" field>' // see nostr-tools module
});

Send A CLOSE Event To Unsubscribe From A Subscription

const me = await createSwarm(sdk, 'topic');

me.sendEvent('CLOSE', 'news');

Send An EVENT:

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

  const yourStorageFolder = './test';

  const sdk = await SDK.create({
      storage: yourStorageFolder,
      autoJoin: true
  });
  goodbye(_ => sdk.close());

  const me = await createSwarm(sdk, 'test');

  await me.update();

  const userPrivK = generatePrivateKey(); // or string from previous generation
  const userPubK = getPublicKey(userPrivK);

  let event = {
    kind: 1000, // normal (see nip 1)
    content: 'hello', // can be json?
    created_at: Math.floor(+new Date() / 1000), // seconds
    pubkey: userPubK,
    tags: []
  };

  event.id = getEventHash(deleteEvent);
  event.sign = signEvent(deleteEvent, userPrivK);

  await me.sendEvent('EVENT', event);
  await me.update();

})();

Delete An Event:

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

  const yourStorageFolder = './test';

  const sdk = await SDK.create({
      storage: yourStorageFolder,
      autoJoin: true
  });
  goodbye(_ => sdk.close());

  const me = await createSwarm(sdk, 'test');

  await me.update();

  const userPrivK = generatePrivateKey(); // or string from previous generation
  const userPubK = getPublicKey(userPrivK);

  let deleteEvent = await me.queryEvents({id: 'xyz'});

  deleteEvent = {
    kind: 5, // this means delete (see nip 9)
    content: '',
    created_at: Math.floor(+new Date() / 1000), // seconds
    pubkey: userPubK,
    tags: [
      ['e', deleteEvent.id]
    ]
  };

  deleteEvent.id = getEventHash(deleteEvent);
  deleteEvent.sign = signEvent(deleteEvent, userPrivK);

  await me.sendEvent('EVENT', deleteEvent);
  await me.update();

})();

List Subscriptions

???

Query Events

???

Send A Query To Subscriptions

What is this for ?

Update Your Database To Sync To/From The Network

const me = await createSwarm(sdk, 'test');
await me.update();