Sure! Here are a few code snippets for a micro app using Nostr and nostr-tools:
1. Generating a private key and a public key
```js
import { generatePrivateKey, getPublicKey } from 'nostr-tools';
const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
console.log(privateKey); // prints the generated private key
console.log(publicKey); // prints the corresponding public key
```
2. Creating and signing an event
```js
import { signEvent } from 'nostr-tools';
const event = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'hello',
pubkey: 'YOUR_PUBLIC_KEY'
};
event.id = getEventHash(event);
event.sig = signEvent(event, 'YOUR_PRIVATE_KEY');
console.log(event); // prints the signed event object
```
3. Interacting with a relay
```js
import { relayInit, generatePrivateKey, getPublicKey, signEvent } from 'nostr-tools';
const relay = relayInit('wss://relay.example.com');
relay.on('connect', () => {
console.log(`connected to ${relay.url}`);
});
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`);
});
await relay.connect();
const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
const event = {
kind: 1,
pubkey: publicKey,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: 'hello world'
};
event.id = getEventHash(event);
event.sig = signEvent(event, privateKey);
const pub = relay.publish(event);
pub.on('ok', () => {
console.log(`${relay.url} has accepted our event`);
});
pub.on('failed', reason => {
console.log(`failed to publish to ${relay.url}: ${reason}`);
});
const events = await relay.list([{kinds: [0, 1]}]);
const event = await relay.get({ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']});
relay.close();
```
These code snippets should give you an idea of how to use nostr-tools to create a Nostr micro app. Of course, you'll need to customize them to fit your specific use case.