Compare commits

...

3 Commits

Author SHA1 Message Date
fiatjaf
498c1603b0 nip57: implement "P" tag for sender. 2024-01-01 11:39:22 -03:00
Shusui MOYATANI
4cfc67e294 fix yieldThread memory leak 2023-12-30 13:50:44 -03:00
fiatjaf
da51418f04 update readme example.
fixes https://github.com/nbd-wtf/nostr-tools/issues/337
2023-12-27 11:14:19 -03:00
5 changed files with 26 additions and 26 deletions

View File

@@ -66,18 +66,18 @@ const sub = relay.subscribe([
let sk = generateSecretKey()
let pk = getPublicKey(sk)
let sub = relay.sub([
relay.sub([
{
kinds: [1],
authors: [pk],
},
])
sub.on('event', event => {
console.log('got event:', event)
], {
onevent(event) {
console.log('got event:', event)
}
})
let event = {
let eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
@@ -85,14 +85,9 @@ let event = {
}
// this assigns the pubkey, calculates the event id and signs the event in a single step
const signedEvent = finalizeEvent(event, sk)
const signedEvent = finalizeEvent(eventTemplate, sk)
await relay.publish(signedEvent)
let events = await relay.list([{ kinds: [0, 1] }])
let event = await relay.get({
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'],
})
relay.close()
```

View File

@@ -1,10 +1,15 @@
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
export async function yieldThread() {
return new Promise(resolve => {
return new Promise<void>(resolve => {
const ch = new MessageChannel()
const handler = () => {
// @ts-ignore (typescript thinks this property should be called `removeListener`, but in fact it's `removeEventListener`)
ch.port1.removeEventListener('message', handler)
resolve()
}
// @ts-ignore (typescript thinks this property should be called `addListener`, but in fact it's `addEventListener`)
ch.port1.addEventListener('message', resolve)
ch.port1.addEventListener('message', handler)
ch.port2.postMessage(0)
ch.port1.start()
})

View File

@@ -242,10 +242,11 @@ describe('validateZapRequest', () => {
})
describe('makeZapReceipt', () => {
test('returns a valid Zap receipt with a preimage', () => {
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const target = 'efeb5d6e74ce6ffea6cae4094a9f29c26b5c56d7b44fae9f490f3410fd708c45'
test('returns a valid Zap receipt with a preimage', () => {
const zapRequest = JSON.stringify(
finalizeEvent(
{
@@ -253,7 +254,7 @@ describe('makeZapReceipt', () => {
created_at: Date.now() / 1000,
content: 'content',
tags: [
['p', publicKey],
['p', target],
['amount', '100'],
['relays', 'relay1', 'relay2'],
],
@@ -274,16 +275,14 @@ describe('makeZapReceipt', () => {
expect.arrayContaining([
['bolt11', bolt11],
['description', zapRequest],
['p', publicKey],
['p', target],
['P', publicKey],
['preimage', preimage],
]),
)
})
test('returns a valid Zap receipt without a preimage', () => {
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const zapRequest = JSON.stringify(
finalizeEvent(
{
@@ -291,7 +290,7 @@ describe('makeZapReceipt', () => {
created_at: Date.now() / 1000,
content: 'content',
tags: [
['p', publicKey],
['p', target],
['amount', '100'],
['relays', 'relay1', 'relay2'],
],
@@ -311,7 +310,8 @@ describe('makeZapReceipt', () => {
expect.arrayContaining([
['bolt11', bolt11],
['description', zapRequest],
['p', publicKey],
['p', target],
['P', publicKey],
]),
)
expect(JSON.stringify(result.tags)).not.toContain('preimage')

View File

@@ -119,7 +119,7 @@ export function makeZapReceipt({
kind: 9735,
created_at: Math.round(paidAt.getTime() / 1000),
content: '',
tags: [...tagsFromZapRequest, ['bolt11', bolt11], ['description', zapRequest]],
tags: [...tagsFromZapRequest, ['P', zr.pubkey], ['bolt11', bolt11], ['description', zapRequest]],
}
if (preimage) {

View File

@@ -1,7 +1,7 @@
{
"type": "module",
"name": "nostr-tools",
"version": "2.1.1",
"version": "2.1.2",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",