-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
54 lines (44 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { bech32 } from 'bech32'
// Function to convert string to Uint8Array
function stringToUint8Array (str) {
return new TextEncoder().encode(str)
}
// Function to convert a number to 32-bit big-endian Uint8Array
function toBigEndianUint8Array (num) {
const buffer = new ArrayBuffer(4)
new DataView(buffer).setUint32(0, num, false)
return new Uint8Array(buffer)
}
// Function to encode TLV items
function encodeTLV (identifier, authorPubkey, kind, relay) {
const tlvItems = []
// Encode identifier
const identifierBytes = stringToUint8Array(identifier)
tlvItems.push(new Uint8Array([0]), new Uint8Array([identifierBytes.length]), identifierBytes)
// Encode author pubkey
tlvItems.push(new Uint8Array([2]), new Uint8Array([32]), authorPubkey)
// Encode kind
const kindBytes = toBigEndianUint8Array(kind)
tlvItems.push(new Uint8Array([3]), new Uint8Array([4]), kindBytes)
// Encode relay if provided
if (relay) {
const relayBytes = stringToUint8Array(relay)
tlvItems.push(new Uint8Array([1]), new Uint8Array([relayBytes.length]), relayBytes)
}
// Concatenate all TLV items
return Buffer.concat(tlvItems)
}
// Function to encode naddr
export function encodeNaddr (identifier, authorPubkeyHex, kind, relay) {
const authorPubkey = Buffer.from(authorPubkeyHex, 'hex')
const tlv = encodeTLV(identifier, authorPubkey, kind, relay)
const words = bech32.toWords(tlv)
return bech32.encode('naddr', words, 1000)
}
// Example usage
const identifier = 'example_id'
const authorPubkeyHex = '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
const kind = 1234
const relay = 'wss://example.com'
const encodedNaddr = encodeNaddr(identifier, authorPubkeyHex, kind, relay)
console.log('Encoded naddr:', encodedNaddr)