apply prettier.

This commit is contained in:
fiatjaf 2023-09-26 12:19:01 -03:00
parent e899cc32b7
commit 6874f58c0a
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
5 changed files with 46 additions and 56 deletions

View File

@ -20,9 +20,9 @@ publish: build emit-types
npm publish npm publish
format: format:
eslint --ext .ts --fix . eslint --ext .ts --fix *.ts
prettier --write . prettier --write *.ts
lint: lint:
eslint --ext .ts . eslint --ext .ts *.ts
prettier --check . prettier --check *.ts

View File

@ -9,13 +9,16 @@ test('identifies proof-of-work difficulty', async () => {
test('mines POW for an event', async () => { test('mines POW for an event', async () => {
const difficulty = 10 const difficulty = 10
const event = minePow({ const event = minePow(
kind: 1, {
tags: [], kind: 1,
content: 'Hello, world!', tags: [],
created_at: 0, content: 'Hello, world!',
pubkey: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6', created_at: 0,
}, difficulty) pubkey: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6',
},
difficulty,
)
expect(getPow(event.id)).toBeGreaterThanOrEqual(difficulty) expect(getPow(event.id)).toBeGreaterThanOrEqual(difficulty)
}) })

View File

@ -1,6 +1,6 @@
import {makeNwcRequestEvent, parseConnectionString} from './nip47' import { makeNwcRequestEvent, parseConnectionString } from './nip47'
import {Kind} from './event' import { Kind } from './event'
import {decrypt} from './nip04.ts' import { decrypt } from './nip04.ts'
import crypto from 'node:crypto' import crypto from 'node:crypto'
// @ts-ignore // @ts-ignore
@ -11,58 +11,46 @@ describe('parseConnectionString', () => {
test('returns pubkey, relay, and secret if connection string is valid', () => { test('returns pubkey, relay, and secret if connection string is valid', () => {
const connectionString = const connectionString =
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' 'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
const {pubkey, relay, secret} = parseConnectionString(connectionString) const { pubkey, relay, secret } = parseConnectionString(connectionString)
expect(pubkey).toBe( expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4')
'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4'
)
expect(relay).toBe('wss://relay.damus.io') expect(relay).toBe('wss://relay.damus.io')
expect(secret).toBe( expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c')
'71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
)
}) })
test('throws an error if no pubkey in connection string', async () => { test('throws an error if no pubkey in connection string', async () => {
const connectionString = const connectionString =
'nostr+walletconnect:relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' 'nostr+walletconnect:relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
expect(() => parseConnectionString(connectionString)).toThrow( expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
'invalid connection string'
)
}) })
test('throws an error if no relay in connection string', async () => { test('throws an error if no relay in connection string', async () => {
const connectionString = const connectionString =
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c' 'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
expect(() => parseConnectionString(connectionString)).toThrow( expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
'invalid connection string'
)
}) })
test('throws an error if no secret in connection string', async () => { test('throws an error if no secret in connection string', async () => {
const connectionString = const connectionString =
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io' 'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io'
expect(() => parseConnectionString(connectionString)).toThrow( expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
'invalid connection string'
)
}) })
}) })
describe('makeNwcRequestEvent', () => { describe('makeNwcRequestEvent', () => {
test('returns a valid NWC request event', async () => { test('returns a valid NWC request event', async () => {
const pubkey = const pubkey = 'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4'
'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4' const secret = '71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
const secret =
'71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
const invoice = const invoice =
'lnbc210n1pjdgyvupp5x43awdarnfd4mdlsklelux0nyckwfu5c708ykuet8vcjnjp3rnpqdqu2askcmr9wssx7e3q2dshgmmndp5scqzzsxqyz5vqsp52l7y9peq9pka3vd3j7aps7gjnalsmy46ndj2mlkz00dltjgqfumq9qyyssq5fasr5dxed8l4qjfnqq48a02jzss3asf8sly7sfaqtr9w3yu2q9spsxhghs3y9aqdf44zkrrg9jjjdg6amade4h0hulllkwk33eqpucp6d5jye' 'lnbc210n1pjdgyvupp5x43awdarnfd4mdlsklelux0nyckwfu5c708ykuet8vcjnjp3rnpqdqu2askcmr9wssx7e3q2dshgmmndp5scqzzsxqyz5vqsp52l7y9peq9pka3vd3j7aps7gjnalsmy46ndj2mlkz00dltjgqfumq9qyyssq5fasr5dxed8l4qjfnqq48a02jzss3asf8sly7sfaqtr9w3yu2q9spsxhghs3y9aqdf44zkrrg9jjjdg6amade4h0hulllkwk33eqpucp6d5jye'
const timeBefore = Date.now() / 1000 const timeBefore = Date.now() / 1000
const result = await makeNwcRequestEvent({ const result = await makeNwcRequestEvent({
pubkey, pubkey,
secret, secret,
invoice invoice,
}) })
const timeAfter = Date.now() / 1000 const timeAfter = Date.now() / 1000
expect(result.kind).toBe(Kind.NwcRequest) expect(result.kind).toBe(Kind.NwcRequest)
@ -72,9 +60,9 @@ describe('makeNwcRequestEvent', () => {
JSON.stringify({ JSON.stringify({
method: 'pay_invoice', method: 'pay_invoice',
params: { params: {
invoice invoice,
} },
}) }),
) )
expect(result.tags).toEqual([['p', pubkey]]) expect(result.tags).toEqual([['p', pubkey]])
expect(result.id).toEqual(expect.any(String)) expect(result.id).toEqual(expect.any(String))

View File

@ -1,9 +1,9 @@
import {finishEvent} from './event.ts' import { finishEvent } from './event.ts'
import {encrypt} from './nip04.ts' import { encrypt } from './nip04.ts'
import {Kind} from './event' import { Kind } from './event'
export function parseConnectionString(connectionString: string) { export function parseConnectionString(connectionString: string) {
const {pathname, searchParams} = new URL(connectionString) const { pathname, searchParams } = new URL(connectionString)
const pubkey = pathname const pubkey = pathname
const relay = searchParams.get('relay') const relay = searchParams.get('relay')
const secret = searchParams.get('secret') const secret = searchParams.get('secret')
@ -12,13 +12,13 @@ export function parseConnectionString(connectionString: string) {
throw new Error('invalid connection string') throw new Error('invalid connection string')
} }
return {pubkey, relay, secret} return { pubkey, relay, secret }
} }
export async function makeNwcRequestEvent({ export async function makeNwcRequestEvent({
pubkey, pubkey,
secret, secret,
invoice invoice,
}: { }: {
pubkey: string pubkey: string
secret: string secret: string
@ -27,19 +27,15 @@ export async function makeNwcRequestEvent({
const content = { const content = {
method: 'pay_invoice', method: 'pay_invoice',
params: { params: {
invoice invoice,
} },
} }
const encryptedContent = await encrypt( const encryptedContent = await encrypt(secret, pubkey, JSON.stringify(content))
secret,
pubkey,
JSON.stringify(content)
)
const eventTemplate = { const eventTemplate = {
kind: Kind.NwcRequest, kind: Kind.NwcRequest,
created_at: Math.round(Date.now() / 1000), created_at: Math.round(Date.now() / 1000),
content: encryptedContent, content: encryptedContent,
tags: [['p', pubkey]] tags: [['p', pubkey]],
} }
return finishEvent(eventTemplate, secret) return finishEvent(eventTemplate, secret)

11
pool.ts
View File

@ -79,10 +79,13 @@ export class SimplePool {
let eosesMissing = relays.length let eosesMissing = relays.length
let eoseSent = false let eoseSent = false
let eoseTimeout = setTimeout(() => { let eoseTimeout = setTimeout(
eoseSent = true () => {
for (let cb of eoseListeners.values()) cb() eoseSent = true
}, opts?.eoseSubTimeout || this.eoseSubTimeout) for (let cb of eoseListeners.values()) cb()
},
opts?.eoseSubTimeout || this.eoseSubTimeout,
)
relays relays
.filter((r, i, a) => a.indexOf(r) === i) .filter((r, i, a) => a.indexOf(r) === i)