Compare commits

..

6 Commits

Author SHA1 Message Date
fiatjaf
50c3f24b25 replace two packages with a @scure dependency that already existed. 2022-12-27 11:35:21 -03:00
fiatjaf
39ea47660d use a different relay for tests. 2022-12-25 16:01:31 -03:00
Tristan
8071e2f4fa Make opts arg optional for sub method
In the README and the code, it looks like the second argument for the relay's `sub` method is optional:

```typescript
let sub = relay.sub([
  {
    ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
  }
])
```

In the type definitions it's required however, which leads to an error in editors. Let's mark it as optional in the type definitions too! 👍
2022-12-25 14:32:20 -03:00
Tristan
cc2250da1f Add missing "error" event to on and off type definitions 2022-12-25 14:31:38 -03:00
rkfg
c37d10bb9d Fix resolveClose 2022-12-24 20:41:49 -03:00
rkfg
97e28fdf9a Fix connect/close return types and race condition 2022-12-24 18:49:16 -03:00
6 changed files with 22 additions and 21 deletions

View File

@@ -2,7 +2,7 @@
Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients. Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
Very lean on dependencies. Only depends on _@scure_ and _@noble_ packages.
## Usage ## Usage

View File

@@ -1,6 +1,6 @@
import {randomBytes} from '@noble/hashes/utils' import {randomBytes} from '@noble/hashes/utils'
import * as secp256k1 from '@noble/secp256k1' import * as secp256k1 from '@noble/secp256k1'
import {encode as b64encode, decode as b64decode} from 'base64-arraybuffer' import {base64} from '@scure/base'
import {utf8Decoder, utf8Encoder} from './utils' import {utf8Decoder, utf8Encoder} from './utils'
@@ -26,8 +26,8 @@ export async function encrypt(
cryptoKey, cryptoKey,
plaintext plaintext
) )
let ctb64 = b64encode(ciphertext) let ctb64 = base64.encode(new Uint8Array(ciphertext))
let ivb64 = b64encode(iv.buffer) let ivb64 = base64.encode(new Uint8Array(iv.buffer))
return `${ctb64}?iv=${ivb64}` return `${ctb64}?iv=${ivb64}`
} }
@@ -48,8 +48,8 @@ export async function decrypt(
false, false,
['decrypt'] ['decrypt']
) )
let ciphertext = b64decode(ctb64) let ciphertext = base64.decode(ctb64)
let iv = b64decode(ivb64) let iv = base64.decode(ivb64)
let plaintext = await crypto.subtle.decrypt( let plaintext = await crypto.subtle.decrypt(
{name: 'AES-CBC', iv}, {name: 'AES-CBC', iv},

View File

@@ -1,5 +1,5 @@
import * as secp256k1 from '@noble/secp256k1' import * as secp256k1 from '@noble/secp256k1'
import {bech32} from 'bech32' import {bech32} from '@scure/base'
import {utf8Decoder, utf8Encoder} from './utils' import {utf8Decoder, utf8Encoder} from './utils'

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "1.0.0", "version": "1.1.0",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -11,10 +11,9 @@
"dependencies": { "dependencies": {
"@noble/hashes": "^0.5.7", "@noble/hashes": "^0.5.7",
"@noble/secp256k1": "^1.7.0", "@noble/secp256k1": "^1.7.0",
"@scure/base": "^1.1.1",
"@scure/bip32": "^1.1.1", "@scure/bip32": "^1.1.1",
"@scure/bip39": "^1.1.0", "@scure/bip39": "^1.1.0"
"base64-arraybuffer": "^1.0.2",
"bech32": "^2.0.0"
}, },
"keywords": [ "keywords": [
"decentralization", "decentralization",

View File

@@ -9,7 +9,7 @@ const {
signEvent signEvent
} = require('./lib/nostr.cjs') } = require('./lib/nostr.cjs')
let relay = relayInit('wss://nostr-pub.semisol.dev/') let relay = relayInit('wss://nostr-dev.wellorder.net/')
beforeAll(() => { beforeAll(() => {
relay.connect() relay.connect()

View File

@@ -3,15 +3,17 @@
import {Event, verifySignature, validateEvent} from './event' import {Event, verifySignature, validateEvent} from './event'
import {Filter, matchFilters} from './filter' import {Filter, matchFilters} from './filter'
type RelayEvent = 'connect' | 'disconnect' | 'error' | 'notice'
export type Relay = { export type Relay = {
url: string url: string
status: number status: number
connect: () => void connect: () => Promise<void>
close: () => void close: () => Promise<void>
sub: (filters: Filter[], opts: SubscriptionOptions) => Sub sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
publish: (event: Event) => Pub publish: (event: Event) => Pub
on: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void on: (type: RelayEvent, cb: any) => void
off: (type: 'connect' | 'disconnect' | 'notice', cb: any) => void off: (type: RelayEvent, cb: any) => void
} }
export type Pub = { export type Pub = {
on: (type: 'ok' | 'seen' | 'failed', cb: any) => void on: (type: 'ok' | 'seen' | 'failed', cb: any) => void
@@ -73,7 +75,7 @@ export function relayInit(url: string): Relay {
} }
ws.onclose = async () => { ws.onclose = async () => {
listeners.disconnect.forEach(cb => cb()) listeners.disconnect.forEach(cb => cb())
resolveClose() resolveClose && resolveClose()
} }
ws.onmessage = async e => { ws.onmessage = async e => {
@@ -185,7 +187,7 @@ export function relayInit(url: string): Relay {
url, url,
sub, sub,
on: ( on: (
type: 'connect' | 'disconnect' | 'error' | 'notice', type: RelayEvent,
cb: any cb: any
): void => { ): void => {
listeners[type].push(cb) listeners[type].push(cb)
@@ -194,7 +196,7 @@ export function relayInit(url: string): Relay {
} }
}, },
off: ( off: (
type: 'connect' | 'disconnect' | 'error' | 'notice', type: RelayEvent,
cb: any cb: any
): void => { ): void => {
let index = listeners[type].indexOf(cb) let index = listeners[type].indexOf(cb)
@@ -258,7 +260,7 @@ export function relayInit(url: string): Relay {
connect, connect,
close(): Promise<void> { close(): Promise<void> {
ws.close() ws.close()
return new Promise(resolve => { return new Promise<void>(resolve => {
resolveClose = resolve resolveClose = resolve
}) })
}, },