Skip to content

Commit

Permalink
Add kind to nevent decode and encode (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaibearary authored Sep 26, 2023
1 parent 6874f58 commit c73268c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 208 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ let relaysForEvent = pool.seenOn('44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67

pool.close()
```

read more details about `batchedList` on this pr: [https://github.com/nbd-wtf/nostr-tools/pull/279](https://github.com/nbd-wtf/nostr-tools/pull/279#issue-1859315757)

### Parsing references (mentions) from a content using NIP-10 and NIP-27
Expand Down
36 changes: 36 additions & 0 deletions nip19.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
npubEncode,
nrelayEncode,
nsecEncode,
neventEncode,
type AddressPointer,
type ProfilePointer,
EventPointer,
} from './nip19.ts'

test('encode and decode nsec', () => {
Expand Down Expand Up @@ -72,6 +74,40 @@ test('encode and decode naddr', () => {
expect(pointer.identifier).toEqual('banana')
})

test('encode and decode nevent', () => {
let pk = getPublicKey(generatePrivateKey())
let relays = ['wss://relay.nostr.example.mydomain.example.com', 'wss://nostr.banana.com']
let naddr = neventEncode({
id: pk,
relays,
kind: 30023,
})
expect(naddr).toMatch(/nevent1\w+/)
let { type, data } = decode(naddr)
expect(type).toEqual('nevent')
const pointer = data as EventPointer
expect(pointer.id).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.kind).toEqual(30023)
})

test('encode and decode nevent with kind 0', () => {
let pk = getPublicKey(generatePrivateKey())
let relays = ['wss://relay.nostr.example.mydomain.example.com', 'wss://nostr.banana.com']
let naddr = neventEncode({
id: pk,
relays,
kind: 0,
})
expect(naddr).toMatch(/nevent1\w+/)
let { type, data } = decode(naddr)
expect(type).toEqual('nevent')
const pointer = data as EventPointer
expect(pointer.id).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.kind).toEqual(0)
})

test('decode naddr from habla.news', () => {
let { type, data } = decode(
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5',
Expand Down
23 changes: 23 additions & 0 deletions nip19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const Bech32MaxSize = 5000
*/
export const BECH32_REGEX = /[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/

function integerToUint8Array(number: number) {
// Create a Uint8Array with enough space to hold a 32-bit integer (4 bytes).
const uint8Array = new Uint8Array(4)

// Use bitwise operations to extract the bytes.
uint8Array[0] = (number >> 24) & 0xff // Most significant byte (MSB)
uint8Array[1] = (number >> 16) & 0xff
uint8Array[2] = (number >> 8) & 0xff
uint8Array[3] = number & 0xff // Least significant byte (LSB)

return uint8Array
}

export type ProfilePointer = {
pubkey: string // hex
relays?: string[]
Expand All @@ -20,6 +33,7 @@ export type EventPointer = {
id: string // hex
relays?: string[]
author?: string
kind?: number
}

export type AddressPointer = {
Expand Down Expand Up @@ -73,13 +87,15 @@ export function decode(nip19: string): DecodeResult {
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nevent')
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
if (tlv[2] && tlv[2][0].length !== 32) throw new Error('TLV 2 should be 32 bytes')
if (tlv[3] && tlv[3][0].length !== 4) throw new Error('TLV 3 should be 4 bytes')

return {
type: 'nevent',
data: {
id: bytesToHex(tlv[0][0]),
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
author: tlv[2]?.[0] ? bytesToHex(tlv[2][0]) : undefined,
kind: tlv[3]?.[0] ? parseInt(bytesToHex(tlv[3][0]), 16) : undefined,
},
}
}
Expand Down Expand Up @@ -172,11 +188,18 @@ export function nprofileEncode(profile: ProfilePointer): `nprofile1${string}` {
}

export function neventEncode(event: EventPointer): `nevent1${string}` {
let kindArray
if (event.kind != undefined) {

Check failure on line 192 in nip19.ts

View workflow job for this annotation

GitHub Actions / format

Expected '!==' and instead saw '!='
kindArray = integerToUint8Array(event.kind)
}

let data = encodeTLV({
0: [hexToBytes(event.id)],
1: (event.relays || []).map(url => utf8Encoder.encode(url)),
2: event.author ? [hexToBytes(event.author)] : [],
3: kindArray ? [new Uint8Array(kindArray)] : [],
})

return encodeBech32('nevent', data)
}

Expand Down
Loading

0 comments on commit c73268c

Please sign in to comment.