mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 00:58:51 +00:00
fix types, imports and other stuff on nip17 and nip59.
This commit is contained in:
@@ -2,11 +2,12 @@ import { test, expect } from 'bun:test'
|
|||||||
import { getPublicKey } from './pure.ts'
|
import { getPublicKey } from './pure.ts'
|
||||||
import { decode } from './nip19.ts'
|
import { decode } from './nip19.ts'
|
||||||
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
|
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
|
||||||
|
import { hexToBytes } from '@noble/hashes/utils'
|
||||||
|
|
||||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
||||||
|
|
||||||
const sk1 = 'f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840'
|
const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840')
|
||||||
const sk2 = '5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747'
|
const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747')
|
||||||
|
|
||||||
const recipients = [
|
const recipients = [
|
||||||
{ publicKey: getPublicKey(sk1), relayUrl: 'wss://relay1.com' },
|
{ publicKey: getPublicKey(sk1), relayUrl: 'wss://relay1.com' },
|
||||||
|
|||||||
11
nip17.ts
11
nip17.ts
@@ -1,6 +1,6 @@
|
|||||||
import { PrivateDirectMessage } from './kinds.ts'
|
import { PrivateDirectMessage } from './kinds.ts'
|
||||||
import { getPublicKey } from './pure'
|
import { EventTemplate, getPublicKey } from './pure.ts'
|
||||||
import * as nip59 from './nip59'
|
import * as nip59 from './nip59.ts'
|
||||||
|
|
||||||
type Recipient = {
|
type Recipient = {
|
||||||
publicKey: string
|
publicKey: string
|
||||||
@@ -17,10 +17,11 @@ function createEvent(
|
|||||||
message: string,
|
message: string,
|
||||||
conversationTitle?: string,
|
conversationTitle?: string,
|
||||||
replyTo?: ReplyTo,
|
replyTo?: ReplyTo,
|
||||||
) {
|
): EventTemplate {
|
||||||
const baseEvent = {
|
const baseEvent: EventTemplate = {
|
||||||
|
created_at: Math.ceil(Date.now() / 1000),
|
||||||
kind: PrivateDirectMessage,
|
kind: PrivateDirectMessage,
|
||||||
tags: [] as (string | string[])[],
|
tags: [],
|
||||||
content: message,
|
content: message,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { test, expect } from 'bun:test'
|
import { test, expect } from 'bun:test'
|
||||||
import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents, getWrappedEvents } from './nip59.ts'
|
import { wrapEvent, wrapManyEvents, unwrapEvent, unwrapManyEvents } from './nip59.ts'
|
||||||
import { decode } from './nip19.ts'
|
import { decode } from './nip19.ts'
|
||||||
import { getPublicKey } from './pure.ts'
|
import { NostrEvent, getPublicKey } from './pure.ts'
|
||||||
|
import { SimplePool } from './pool.ts'
|
||||||
|
import { GiftWrap } from './kinds.ts'
|
||||||
|
import { hexToBytes } from '@noble/hashes/utils'
|
||||||
|
|
||||||
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
|
||||||
const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data
|
const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data
|
||||||
@@ -97,9 +100,11 @@ test('getWrappedEvents and unwrapManyEvents', async () => {
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
const relays = ['wss://relay.damus.io', 'wss://nos.lol']
|
const relays = ['wss://relay.damus.io', 'wss://nos.lol']
|
||||||
const privateKey = '582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6'
|
const privateKey = hexToBytes('582c3e7902c10c84d1cfe899a102e56bde628972d58d63011163ce0cdf4279b6')
|
||||||
const publicKey = '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa'
|
const publicKey = '33d6bb037bf2e8c4571708e480e42d141bedc5a562b4884ec233b22d6fdea6aa'
|
||||||
const wrappedEvents = await getWrappedEvents(publicKey, relays)
|
|
||||||
|
const pool = new SimplePool()
|
||||||
|
const wrappedEvents: NostrEvent[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [publicKey] })
|
||||||
const unwrappedEvents = unwrapManyEvents(wrappedEvents, privateKey)
|
const unwrappedEvents = unwrapManyEvents(wrappedEvents, privateKey)
|
||||||
|
|
||||||
unwrappedEvents.forEach((event, index) => {
|
unwrappedEvents.forEach((event, index) => {
|
||||||
|
|||||||
21
nip59.ts
21
nip59.ts
@@ -2,7 +2,6 @@ import { EventTemplate, UnsignedEvent, Event } from './core.ts'
|
|||||||
import { getConversationKey, decrypt, encrypt } from './nip44.ts'
|
import { getConversationKey, decrypt, encrypt } from './nip44.ts'
|
||||||
import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'
|
import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'
|
||||||
import { Seal, GiftWrap } from './kinds.ts'
|
import { Seal, GiftWrap } from './kinds.ts'
|
||||||
import { SimplePool } from './pool'
|
|
||||||
|
|
||||||
type Rumor = UnsignedEvent & { id: string }
|
type Rumor = UnsignedEvent & { id: string }
|
||||||
|
|
||||||
@@ -86,13 +85,13 @@ export function wrapManyEvents(
|
|||||||
return wrappeds
|
return wrappeds
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array) {
|
export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array): Rumor {
|
||||||
const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)
|
const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)
|
||||||
return nip44Decrypt(unwrappedSeal, recipientPrivateKey)
|
return nip44Decrypt(unwrappedSeal, recipientPrivateKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array) {
|
export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array): Rumor[] {
|
||||||
let unwrappedEvents = []
|
let unwrappedEvents: Rumor[] = []
|
||||||
|
|
||||||
wrappedEvents.forEach(e => {
|
wrappedEvents.forEach(e => {
|
||||||
unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))
|
unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))
|
||||||
@@ -102,17 +101,3 @@ export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Ui
|
|||||||
|
|
||||||
return unwrappedEvents
|
return unwrappedEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getWrappedEvents(pubKey: string, relays: string[] = []): Promise<Event[] | undefined> {
|
|
||||||
const pool = new SimplePool()
|
|
||||||
|
|
||||||
try {
|
|
||||||
const events: Event[] = await pool.querySync(relays, { kinds: [GiftWrap], '#p': [pubKey] })
|
|
||||||
pool.close(relays)
|
|
||||||
|
|
||||||
return events
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to:', error)
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user