mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-14 02:48:50 +00:00
Add NIP-18 utils
This commit is contained in:
101
nip18.test.js
Normal file
101
nip18.test.js
Normal file
@@ -0,0 +1,101 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
const {nip18, finishEvent, getPublicKey, Kind} = require('./lib/nostr.cjs')
|
||||
|
||||
describe('finishRepostEvent + getRepostedEventPointer + getRepostedEvent', () => {
|
||||
const privateKey =
|
||||
'd217c1ff2f8a65c3e3a1740db3b9f58b8c848bb45e26d00ed4714e4a0f4ceecf'
|
||||
const relayUrl = 'https://relay.example.com'
|
||||
|
||||
const publicKey = getPublicKey(privateKey)
|
||||
|
||||
const repostedEvent = finishEvent({
|
||||
kind: Kind.Text,
|
||||
tags: [
|
||||
['e', 'replied event id'],
|
||||
['p', 'replied event pubkey'],
|
||||
],
|
||||
content: 'Replied to a post',
|
||||
created_at: 1617932115
|
||||
}, privateKey)
|
||||
|
||||
it('should create a signed event from a minimal template', () => {
|
||||
const template = {
|
||||
created_at: 1617932115
|
||||
}
|
||||
|
||||
const event = nip18.finishRepostEvent(template, repostedEvent, relayUrl, privateKey)
|
||||
|
||||
expect(event.kind).toEqual(Kind.Repost)
|
||||
expect(event.tags).toEqual([
|
||||
[
|
||||
'e',
|
||||
repostedEvent.id,
|
||||
relayUrl,
|
||||
],
|
||||
[
|
||||
'p',
|
||||
repostedEvent.pubkey,
|
||||
],
|
||||
])
|
||||
expect(event.content).toEqual(JSON.stringify(repostedEvent))
|
||||
expect(event.created_at).toEqual(template.created_at)
|
||||
expect(event.pubkey).toEqual(publicKey)
|
||||
expect(typeof event.id).toEqual('string')
|
||||
expect(typeof event.sig).toEqual('string')
|
||||
|
||||
const repostedEventPointer = nip18.getRepostedEventPointer(event)
|
||||
|
||||
expect(repostedEventPointer.id).toEqual(repostedEvent.id)
|
||||
expect(repostedEventPointer.author).toEqual(repostedEvent.pubkey)
|
||||
expect(repostedEventPointer.relays).toEqual([ relayUrl ])
|
||||
|
||||
const repostedEventFromContent = nip18.getRepostedEvent(event)
|
||||
|
||||
expect(repostedEventFromContent).toEqual(repostedEvent)
|
||||
})
|
||||
|
||||
it('should create a signed event from a filled template', () => {
|
||||
const template = {
|
||||
tags: [
|
||||
['nonstandard', 'tag'],
|
||||
],
|
||||
content: '',
|
||||
created_at: 1617932115
|
||||
}
|
||||
|
||||
const event = nip18.finishRepostEvent(template, repostedEvent, relayUrl, privateKey)
|
||||
|
||||
expect(event.kind).toEqual(Kind.Repost)
|
||||
expect(event.tags).toEqual([
|
||||
[
|
||||
'nonstandard',
|
||||
'tag',
|
||||
],
|
||||
[
|
||||
'e',
|
||||
repostedEvent.id,
|
||||
relayUrl,
|
||||
],
|
||||
[
|
||||
'p',
|
||||
repostedEvent.pubkey,
|
||||
],
|
||||
])
|
||||
expect(event.content).toEqual('')
|
||||
expect(event.created_at).toEqual(template.created_at)
|
||||
expect(event.pubkey).toEqual(publicKey)
|
||||
expect(typeof event.id).toEqual('string')
|
||||
expect(typeof event.sig).toEqual('string')
|
||||
|
||||
const repostedEventPointer = nip18.getRepostedEventPointer(event)
|
||||
|
||||
expect(repostedEventPointer.id).toEqual(repostedEvent.id)
|
||||
expect(repostedEventPointer.author).toEqual(repostedEvent.pubkey)
|
||||
expect(repostedEventPointer.relays).toEqual([ relayUrl ])
|
||||
|
||||
const repostedEventFromContent = nip18.getRepostedEvent(event)
|
||||
|
||||
expect(repostedEventFromContent).toEqual(undefined)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user