mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
/* eslint-env jest */
|
|
|
|
const {nip19, generatePrivateKey, getPublicKey} = require('./lib/nostr.cjs')
|
|
|
|
test('encode and decode nsec', () => {
|
|
let sk = generatePrivateKey()
|
|
let nsec = nip19.nsecEncode(sk)
|
|
expect(nsec).toMatch(/nsec1\w+/)
|
|
let {type, data} = nip19.decode(nsec)
|
|
expect(type).toEqual('nsec')
|
|
expect(data).toEqual(sk)
|
|
})
|
|
|
|
test('encode and decode npub', () => {
|
|
let pk = getPublicKey(generatePrivateKey())
|
|
let npub = nip19.npubEncode(pk)
|
|
expect(npub).toMatch(/npub1\w+/)
|
|
let {type, data} = nip19.decode(npub)
|
|
expect(type).toEqual('npub')
|
|
expect(data).toEqual(pk)
|
|
})
|
|
|
|
test('encode and decode nprofile', () => {
|
|
let pk = getPublicKey(generatePrivateKey())
|
|
let relays = [
|
|
'wss://relay.nostr.example.mydomain.example.com',
|
|
'wss://nostr.banana.com'
|
|
]
|
|
let nprofile = nip19.nprofileEncode({pubkey: pk, relays})
|
|
expect(nprofile).toMatch(/nprofile1\w+/)
|
|
let {type, data} = nip19.decode(nprofile)
|
|
expect(type).toEqual('nprofile')
|
|
expect(data.pubkey).toEqual(pk)
|
|
expect(data.relays).toContain(relays[0])
|
|
expect(data.relays).toContain(relays[1])
|
|
})
|
|
|
|
test('encode and decode naddr', () => {
|
|
let pk = getPublicKey(generatePrivateKey())
|
|
let relays = [
|
|
'wss://relay.nostr.example.mydomain.example.com',
|
|
'wss://nostr.banana.com'
|
|
]
|
|
let naddr = nip19.naddrEncode({
|
|
pubkey: pk,
|
|
relays,
|
|
kind: 30023,
|
|
identifier: 'banana'
|
|
})
|
|
expect(naddr).toMatch(/naddr1\w+/)
|
|
let {type, data} = nip19.decode(naddr)
|
|
expect(type).toEqual('naddr')
|
|
expect(data.pubkey).toEqual(pk)
|
|
expect(data.relays).toContain(relays[0])
|
|
expect(data.relays).toContain(relays[1])
|
|
expect(data.kind).toEqual(30023)
|
|
expect(data.identifier).toEqual('banana')
|
|
})
|
|
|
|
test('encode and decode naddr from habla.news', () => {
|
|
let {type, data} = nip19.decode(
|
|
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
|
)
|
|
expect(type).toEqual('naddr')
|
|
expect(data.pubkey).toEqual(
|
|
'7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194'
|
|
)
|
|
expect(data.kind).toEqual(30023)
|
|
expect(data.identifier).toEqual('references')
|
|
})
|