Skip to content

Commit

Permalink
apply prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 26, 2023
1 parent e899cc3 commit 6874f58
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 56 deletions.
8 changes: 4 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ publish: build emit-types
npm publish

format:
eslint --ext .ts --fix .
prettier --write .
eslint --ext .ts --fix *.ts
prettier --write *.ts

lint:
eslint --ext .ts .
prettier --check .
eslint --ext .ts *.ts
prettier --check *.ts
17 changes: 10 additions & 7 deletions nip13.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ test('identifies proof-of-work difficulty', async () => {
test('mines POW for an event', async () => {
const difficulty = 10

const event = minePow({
kind: 1,
tags: [],
content: 'Hello, world!',
created_at: 0,
pubkey: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6',
}, difficulty)
const event = minePow(
{
kind: 1,
tags: [],
content: 'Hello, world!',
created_at: 0,
pubkey: '79c2cae114ea28a981e7559b4fe7854a473521a8d22a66bbab9fa248eb820ff6',
},
difficulty,
)

expect(getPow(event.id)).toBeGreaterThanOrEqual(difficulty)
})
42 changes: 15 additions & 27 deletions nip47.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {makeNwcRequestEvent, parseConnectionString} from './nip47'
import {Kind} from './event'
import {decrypt} from './nip04.ts'
import { makeNwcRequestEvent, parseConnectionString } from './nip47'
import { Kind } from './event'
import { decrypt } from './nip04.ts'
import crypto from 'node:crypto'

// @ts-ignore
Expand All @@ -11,58 +11,46 @@ describe('parseConnectionString', () => {
test('returns pubkey, relay, and secret if connection string is valid', () => {
const connectionString =
'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(
'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4'
)
expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4')
expect(relay).toBe('wss://relay.damus.io')
expect(secret).toBe(
'71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
)
expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c')
})

test('throws an error if no pubkey in connection string', async () => {
const connectionString =
'nostr+walletconnect:relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'

expect(() => parseConnectionString(connectionString)).toThrow(
'invalid connection string'
)
expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
})

test('throws an error if no relay in connection string', async () => {
const connectionString =
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'

expect(() => parseConnectionString(connectionString)).toThrow(
'invalid connection string'
)
expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
})

test('throws an error if no secret in connection string', async () => {
const connectionString =
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io'

expect(() => parseConnectionString(connectionString)).toThrow(
'invalid connection string'
)
expect(() => parseConnectionString(connectionString)).toThrow('invalid connection string')
})
})

describe('makeNwcRequestEvent', () => {
test('returns a valid NWC request event', async () => {
const pubkey =
'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4'
const secret =
'71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
const pubkey = 'b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4'
const secret = '71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
const invoice =
'lnbc210n1pjdgyvupp5x43awdarnfd4mdlsklelux0nyckwfu5c708ykuet8vcjnjp3rnpqdqu2askcmr9wssx7e3q2dshgmmndp5scqzzsxqyz5vqsp52l7y9peq9pka3vd3j7aps7gjnalsmy46ndj2mlkz00dltjgqfumq9qyyssq5fasr5dxed8l4qjfnqq48a02jzss3asf8sly7sfaqtr9w3yu2q9spsxhghs3y9aqdf44zkrrg9jjjdg6amade4h0hulllkwk33eqpucp6d5jye'
const timeBefore = Date.now() / 1000
const result = await makeNwcRequestEvent({
pubkey,
secret,
invoice
invoice,
})
const timeAfter = Date.now() / 1000
expect(result.kind).toBe(Kind.NwcRequest)
Expand All @@ -72,9 +60,9 @@ describe('makeNwcRequestEvent', () => {
JSON.stringify({
method: 'pay_invoice',
params: {
invoice
}
})
invoice,
},
}),
)
expect(result.tags).toEqual([['p', pubkey]])
expect(result.id).toEqual(expect.any(String))
Expand Down
24 changes: 10 additions & 14 deletions nip47.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {finishEvent} from './event.ts'
import {encrypt} from './nip04.ts'
import {Kind} from './event'
import { finishEvent } from './event.ts'
import { encrypt } from './nip04.ts'
import { Kind } from './event'

export function parseConnectionString(connectionString: string) {
const {pathname, searchParams} = new URL(connectionString)
const { pathname, searchParams } = new URL(connectionString)
const pubkey = pathname
const relay = searchParams.get('relay')
const secret = searchParams.get('secret')
Expand All @@ -12,13 +12,13 @@ export function parseConnectionString(connectionString: string) {
throw new Error('invalid connection string')
}

return {pubkey, relay, secret}
return { pubkey, relay, secret }
}

export async function makeNwcRequestEvent({
pubkey,
secret,
invoice
invoice,
}: {
pubkey: string
secret: string
Expand All @@ -27,19 +27,15 @@ export async function makeNwcRequestEvent({
const content = {
method: 'pay_invoice',
params: {
invoice
}
invoice,
},
}
const encryptedContent = await encrypt(
secret,
pubkey,
JSON.stringify(content)
)
const encryptedContent = await encrypt(secret, pubkey, JSON.stringify(content))
const eventTemplate = {
kind: Kind.NwcRequest,
created_at: Math.round(Date.now() / 1000),
content: encryptedContent,
tags: [['p', pubkey]]
tags: [['p', pubkey]],
}

return finishEvent(eventTemplate, secret)
Expand Down
11 changes: 7 additions & 4 deletions pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ export class SimplePool {
let eosesMissing = relays.length

let eoseSent = false
let eoseTimeout = setTimeout(() => {
eoseSent = true
for (let cb of eoseListeners.values()) cb()
}, opts?.eoseSubTimeout || this.eoseSubTimeout)
let eoseTimeout = setTimeout(
() => {
eoseSent = true
for (let cb of eoseListeners.values()) cb()
},
opts?.eoseSubTimeout || this.eoseSubTimeout,
)

relays
.filter((r, i, a) => a.indexOf(r) === i)
Expand Down

0 comments on commit 6874f58

Please sign in to comment.