nappup 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/helpers/nip19.js +25 -22
- package/src/services/nostr-relays.js +1 -1
package/package.json
CHANGED
package/src/helpers/nip19.js
CHANGED
|
@@ -3,41 +3,50 @@ import { bytesToBase62, base62ToBytes, ALPHABET as base62Alphabet } from '#helpe
|
|
|
3
3
|
import { isNostrAppDTagSafe } from '#helpers/app.js'
|
|
4
4
|
|
|
5
5
|
const MAX_SIZE = 5000
|
|
6
|
-
export const BASE62_ENTITY_REGEX = new RegExp(
|
|
7
|
-
|
|
6
|
+
export const BASE62_ENTITY_REGEX = new RegExp(`^\\+{1,3}[${base62Alphabet}]{,${MAX_SIZE}}$`)
|
|
7
|
+
const textEncoder = new TextEncoder()
|
|
8
|
+
const textDecoder = new TextDecoder()
|
|
9
|
+
|
|
10
|
+
const kindByChannel = {
|
|
8
11
|
main: 37448,
|
|
9
12
|
next: 37449,
|
|
10
13
|
draft: 37450
|
|
11
14
|
}
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const channelByKind = Object.fromEntries(
|
|
16
|
+
Object.entries(kindByChannel).map(([k, v]) => [v, k])
|
|
17
|
+
)
|
|
18
|
+
const prefixByChannel = {
|
|
19
|
+
main: '+',
|
|
20
|
+
next: '++',
|
|
21
|
+
draft: '+++'
|
|
22
|
+
}
|
|
23
|
+
const channelByPrefix = Object.fromEntries(
|
|
24
|
+
Object.entries(prefixByChannel).map(([k, v]) => [v, k])
|
|
25
|
+
)
|
|
16
26
|
export function appEncode (ref) {
|
|
17
27
|
if (!isNostrAppDTagSafe(ref.dTag)) { throw new Error('Invalid deduplication tag') }
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
if (channelIndex === -1) throw new Error('Wrong channel')
|
|
28
|
+
const channel = ref.channel ? (prefixByChannel[ref.channel] && ref.channel) : channelByKind[ref.kind]
|
|
29
|
+
if (!channel) throw new Error('Wrong channel')
|
|
21
30
|
const tlv = toTlv([
|
|
22
31
|
[textEncoder.encode(ref.dTag)], // type 0 (the array index)
|
|
23
32
|
(ref.relays || []).map(url => textEncoder.encode(url)), // type 1
|
|
24
|
-
[base16ToBytes(ref.pubkey)]
|
|
25
|
-
[uintToBytes(channelIndex)] // type 3
|
|
33
|
+
[base16ToBytes(ref.pubkey)] // type 2
|
|
26
34
|
])
|
|
27
35
|
const base62 = bytesToBase62(tlv)
|
|
28
|
-
|
|
36
|
+
const prefix = prefixByChannel[channel]
|
|
37
|
+
return `${prefix}${base62}`
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
export function appDecode (entity) {
|
|
32
|
-
const
|
|
41
|
+
const prefix = entity.match(/^\+*/)[0]
|
|
42
|
+
const channel = channelByPrefix[prefix]
|
|
43
|
+
if (!channel) throw new Error('Invalid channel')
|
|
44
|
+
const base62 = entity.slice(prefix.length)
|
|
33
45
|
const tlv = tlvToObj(base62ToBytes(base62))
|
|
34
46
|
if (!tlv[0]?.[0]) throw new Error('Missing deduplication tag')
|
|
35
47
|
if (!tlv[2]?.[0]) throw new Error('Missing author pubkey')
|
|
36
48
|
if (tlv[2][0].length !== 32) throw new Error('Author pubkey should be 32 bytes')
|
|
37
|
-
if (!tlv[3]?.[0]) throw new Error('Missing channel enum')
|
|
38
|
-
if (tlv[3][0].length !== 1) throw new Error('Channel enum should be 1 byte')
|
|
39
49
|
|
|
40
|
-
const channel = channelEnum[parseInt(tlv[3][0])]
|
|
41
50
|
return {
|
|
42
51
|
dTag: textDecoder.decode(tlv[0][0]),
|
|
43
52
|
pubkey: bytesToBase16(tlv[2][0]),
|
|
@@ -47,12 +56,6 @@ export function appDecode (entity) {
|
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
// Return shortest uint8Array size (not fixed size)
|
|
51
|
-
function uintToBytes (n, bytes = []) {
|
|
52
|
-
do { bytes.unshift(n & 255) } while ((n >>= 8) > 0)
|
|
53
|
-
return new Uint8Array(bytes)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
59
|
function toTlv (tlvConfig) {
|
|
57
60
|
const arrays = []
|
|
58
61
|
tlvConfig
|
|
@@ -44,7 +44,7 @@ export class NostrRelays {
|
|
|
44
44
|
async disconnect (url) {
|
|
45
45
|
if (this.#relays.has(url)) {
|
|
46
46
|
const relay = this.#relays.get(url)
|
|
47
|
-
if (relay.ws.readyState < 2) await relay.close()
|
|
47
|
+
if (relay.ws.readyState < 2) await relay.close()?.catch(console.log)
|
|
48
48
|
this.#relays.delete(url)
|
|
49
49
|
clearTimeout(this.#relayTimeouts.get(url))
|
|
50
50
|
this.#relayTimeouts.delete(url)
|