mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
Compare commits
5 Commits
v2.3.1
...
alexgleaso
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b582a0206 | ||
|
|
8ed2c13c28 | ||
|
|
27a536f41d | ||
|
|
fbc82d0b73 | ||
|
|
9c0ade1329 |
18
.github/workflows/publish.yml
vendored
Normal file
18
.github/workflows/publish.yml
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
name: Publish
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
id-token: write # The OIDC ID token is used for authentication with JSR.
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: oven-sh/setup-bun@v1
|
||||||
|
- run: bun i
|
||||||
|
- run: npx jsr publish
|
||||||
@@ -266,4 +266,8 @@ This is free and unencumbered software released into the public domain. By submi
|
|||||||
|
|
||||||
## Contributing to this repository
|
## Contributing to this repository
|
||||||
|
|
||||||
Use NIP-34 to send your patches to `naddr1qq9kummnw3ez6ar0dak8xqg5waehxw309aex2mrp0yhxummnw3ezucn8qyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqpzemhxue69uhhyetvv9ujuurjd9kkzmpwdejhgq3q80cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsxpqqqpmejdv00jq`.
|
Use NIP-34 to send your patches to:
|
||||||
|
|
||||||
|
```
|
||||||
|
naddr1qq9kummnw3ez6ar0dak8xqg5waehxw309aex2mrp0yhxummnw3ezucn8qyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqpzemhxue69uhhyetvv9ujuurjd9kkzmpwdejhgq3q80cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsxpqqqpmejdv00jq
|
||||||
|
```
|
||||||
|
|||||||
@@ -15,11 +15,11 @@ export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose' | 'id'> & {
|
|||||||
|
|
||||||
export class AbstractSimplePool {
|
export class AbstractSimplePool {
|
||||||
private relays = new Map<string, AbstractRelay>()
|
private relays = new Map<string, AbstractRelay>()
|
||||||
public seenOn = new Map<string, Set<AbstractRelay>>()
|
public seenOn: Map<string, Set<AbstractRelay>> = new Map()
|
||||||
public trackRelays: boolean = false
|
public trackRelays: boolean = false
|
||||||
|
|
||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: Nostr['verifyEvent']
|
||||||
public trustedRelayURLs = new Set<string>()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
|
|
||||||
constructor(opts: { verifyEvent: Nostr['verifyEvent'] }) {
|
constructor(opts: { verifyEvent: Nostr['verifyEvent'] }) {
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
public openSubs = new Map<string, Subscription>()
|
public openSubs: Map<string, Subscription> = new Map()
|
||||||
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
private connectionPromise: Promise<void> | undefined
|
private connectionPromise: Promise<void> | undefined
|
||||||
@@ -44,7 +44,7 @@ export class AbstractRelay {
|
|||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string, opts: { verifyEvent: Nostr['verifyEvent'] }) {
|
static async connect(url: string, opts: { verifyEvent: Nostr['verifyEvent'] }): Promise<AbstractRelay> {
|
||||||
const relay = new AbstractRelay(url, opts)
|
const relay = new AbstractRelay(url, opts)
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
return relay
|
return relay
|
||||||
@@ -228,7 +228,7 @@ export class AbstractRelay {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async auth(signAuthEvent: (evt: EventTemplate) => Promise<VerifiedEvent>) {
|
public async auth(signAuthEvent: (evt: EventTemplate) => Promise<VerifiedEvent>): Promise<string> {
|
||||||
if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
|
if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
|
||||||
const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge))
|
const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge))
|
||||||
const ret = new Promise<string>((resolve, reject) => {
|
const ret = new Promise<string>((resolve, reject) => {
|
||||||
|
|||||||
5
jsr.json
Normal file
5
jsr.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "@nostr/tools",
|
||||||
|
"version": "2.3.1",
|
||||||
|
"exports": "./index.ts"
|
||||||
|
}
|
||||||
8
kinds.ts
8
kinds.ts
@@ -1,20 +1,20 @@
|
|||||||
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
||||||
export function isRegularKind(kind: number) {
|
export function isRegularKind(kind: number): boolean {
|
||||||
return (1000 <= kind && kind < 10000) || [1, 2, 4, 5, 6, 7, 8, 16, 40, 41, 42, 43, 44].includes(kind)
|
return (1000 <= kind && kind < 10000) || [1, 2, 4, 5, 6, 7, 8, 16, 40, 41, 42, 43, 44].includes(kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
||||||
export function isReplaceableKind(kind: number) {
|
export function isReplaceableKind(kind: number): boolean {
|
||||||
return [0, 3].includes(kind) || (10000 <= kind && kind < 20000)
|
return [0, 3].includes(kind) || (10000 <= kind && kind < 20000)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
||||||
export function isEphemeralKind(kind: number) {
|
export function isEphemeralKind(kind: number): boolean {
|
||||||
return 20000 <= kind && kind < 30000
|
return 20000 <= kind && kind < 30000
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Events are **parameterized replaceable**, which means that, for each combination of `pubkey`, `kind` and the `d` tag, only the latest event is expected to be stored by relays, older versions are expected to be discarded. */
|
/** Events are **parameterized replaceable**, which means that, for each combination of `pubkey`, `kind` and the `d` tag, only the latest event is expected to be stored by relays, older versions are expected to be discarded. */
|
||||||
export function isParameterizedReplaceableKind(kind: number) {
|
export function isParameterizedReplaceableKind(kind: number): boolean {
|
||||||
return 30000 <= kind && kind < 40000
|
return 30000 <= kind && kind < 40000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
nip11.ts
4
nip11.ts
@@ -4,11 +4,11 @@ try {
|
|||||||
_fetch = fetch
|
_fetch = fetch
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
export function useFetchImplementation(fetchImplementation: any) {
|
export function useFetchImplementation(fetchImplementation: any): void {
|
||||||
_fetch = fetchImplementation
|
_fetch = fetchImplementation
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchRelayInformation(url: string) {
|
export async function fetchRelayInformation(url: string): Promise<RelayInformation> {
|
||||||
return (await (
|
return (await (
|
||||||
await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), {
|
await fetch(url.replace('ws://', 'http://').replace('wss://', 'https://'), {
|
||||||
headers: { Accept: 'application/nostr+json' },
|
headers: { Accept: 'application/nostr+json' },
|
||||||
|
|||||||
2
nip21.ts
2
nip21.ts
@@ -1,7 +1,7 @@
|
|||||||
import { BECH32_REGEX, decode, type DecodeResult } from './nip19.ts'
|
import { BECH32_REGEX, decode, type DecodeResult } from './nip19.ts'
|
||||||
|
|
||||||
/** Nostr URI regex, eg `nostr:npub1...` */
|
/** Nostr URI regex, eg `nostr:npub1...` */
|
||||||
export const NOSTR_URI_REGEX = new RegExp(`nostr:(${BECH32_REGEX.source})`)
|
export const NOSTR_URI_REGEX: RegExp = new RegExp(`nostr:(${BECH32_REGEX.source})`)
|
||||||
|
|
||||||
/** Test whether the value is a Nostr URI. */
|
/** Test whether the value is a Nostr URI. */
|
||||||
export function test(value: unknown): value is `nostr:${string}` {
|
export function test(value: unknown): value is `nostr:${string}` {
|
||||||
|
|||||||
2
nip27.ts
2
nip27.ts
@@ -2,7 +2,7 @@ import { decode } from './nip19.ts'
|
|||||||
import { NOSTR_URI_REGEX, type NostrURI } from './nip21.ts'
|
import { NOSTR_URI_REGEX, type NostrURI } from './nip21.ts'
|
||||||
|
|
||||||
/** Regex to find NIP-21 URIs inside event content. */
|
/** Regex to find NIP-21 URIs inside event content. */
|
||||||
export const regex = () => new RegExp(`\\b${NOSTR_URI_REGEX.source}\\b`, 'g')
|
export const regex = (): RegExp => new RegExp(`\\b${NOSTR_URI_REGEX.source}\\b`, 'g')
|
||||||
|
|
||||||
/** Match result for a Nostr URI in event content. */
|
/** Match result for a Nostr URI in event content. */
|
||||||
export interface NostrURIMatch extends NostrURI {
|
export interface NostrURIMatch extends NostrURI {
|
||||||
|
|||||||
2
nip30.ts
2
nip30.ts
@@ -2,7 +2,7 @@
|
|||||||
export const EMOJI_SHORTCODE_REGEX = /:(\w+):/
|
export const EMOJI_SHORTCODE_REGEX = /:(\w+):/
|
||||||
|
|
||||||
/** Regex to find emoji shortcodes in content. */
|
/** Regex to find emoji shortcodes in content. */
|
||||||
export const regex = () => new RegExp(`\\B${EMOJI_SHORTCODE_REGEX.source}\\B`, 'g')
|
export const regex = (): RegExp => new RegExp(`\\B${EMOJI_SHORTCODE_REGEX.source}\\B`, 'g')
|
||||||
|
|
||||||
/** Represents a Nostr custom emoji. */
|
/** Represents a Nostr custom emoji. */
|
||||||
export interface CustomEmoji {
|
export interface CustomEmoji {
|
||||||
|
|||||||
85
nip44.ts
85
nip44.ts
@@ -8,54 +8,59 @@ import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils'
|
|||||||
import { base64 } from '@scure/base'
|
import { base64 } from '@scure/base'
|
||||||
|
|
||||||
const decoder = new TextDecoder()
|
const decoder = new TextDecoder()
|
||||||
const u = {
|
|
||||||
minPlaintextSize: 0x0001, // 1b msg => padded to 32b
|
|
||||||
maxPlaintextSize: 0xffff, // 65535 (64kb-1) => padded to 64kb
|
|
||||||
|
|
||||||
utf8Encode: utf8ToBytes,
|
class u {
|
||||||
utf8Decode(bytes: Uint8Array) {
|
static minPlaintextSize = 0x0001 // 1b msg => padded to 32b
|
||||||
|
static maxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb
|
||||||
|
|
||||||
|
static utf8Encode = utf8ToBytes
|
||||||
|
|
||||||
|
static utf8Decode(bytes: Uint8Array): string {
|
||||||
return decoder.decode(bytes)
|
return decoder.decode(bytes)
|
||||||
},
|
}
|
||||||
|
|
||||||
getConversationKey(privkeyA: string, pubkeyB: string): Uint8Array {
|
static getConversationKey(privkeyA: string, pubkeyB: string): Uint8Array {
|
||||||
const sharedX = secp256k1.getSharedSecret(privkeyA, '02' + pubkeyB).subarray(1, 33)
|
const sharedX = secp256k1.getSharedSecret(privkeyA, '02' + pubkeyB).subarray(1, 33)
|
||||||
return hkdf_extract(sha256, sharedX, 'nip44-v2')
|
return hkdf_extract(sha256, sharedX, 'nip44-v2')
|
||||||
},
|
}
|
||||||
|
|
||||||
getMessageKeys(conversationKey: Uint8Array, nonce: Uint8Array) {
|
static getMessageKeys(
|
||||||
|
conversationKey: Uint8Array,
|
||||||
|
nonce: Uint8Array,
|
||||||
|
): { chacha_key: Uint8Array; chacha_nonce: Uint8Array; hmac_key: Uint8Array } {
|
||||||
const keys = hkdf_expand(sha256, conversationKey, nonce, 76)
|
const keys = hkdf_expand(sha256, conversationKey, nonce, 76)
|
||||||
return {
|
return {
|
||||||
chacha_key: keys.subarray(0, 32),
|
chacha_key: keys.subarray(0, 32),
|
||||||
chacha_nonce: keys.subarray(32, 44),
|
chacha_nonce: keys.subarray(32, 44),
|
||||||
hmac_key: keys.subarray(44, 76),
|
hmac_key: keys.subarray(44, 76),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
calcPaddedLen(len: number): number {
|
static calcPaddedLen(len: number): number {
|
||||||
if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')
|
if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')
|
||||||
if (len <= 32) return 32
|
if (len <= 32) return 32
|
||||||
const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1)
|
const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1)
|
||||||
const chunk = nextPower <= 256 ? 32 : nextPower / 8
|
const chunk = nextPower <= 256 ? 32 : nextPower / 8
|
||||||
return chunk * (Math.floor((len - 1) / chunk) + 1)
|
return chunk * (Math.floor((len - 1) / chunk) + 1)
|
||||||
},
|
}
|
||||||
|
|
||||||
writeU16BE(num: number) {
|
static writeU16BE(num: number): Uint8Array {
|
||||||
if (!Number.isSafeInteger(num) || num < u.minPlaintextSize || num > u.maxPlaintextSize)
|
if (!Number.isSafeInteger(num) || num < u.minPlaintextSize || num > u.maxPlaintextSize)
|
||||||
throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')
|
throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')
|
||||||
const arr = new Uint8Array(2)
|
const arr = new Uint8Array(2)
|
||||||
new DataView(arr.buffer).setUint16(0, num, false)
|
new DataView(arr.buffer).setUint16(0, num, false)
|
||||||
return arr
|
return arr
|
||||||
},
|
}
|
||||||
|
|
||||||
pad(plaintext: string): Uint8Array {
|
static pad(plaintext: string): Uint8Array {
|
||||||
const unpadded = u.utf8Encode(plaintext)
|
const unpadded = u.utf8Encode(plaintext)
|
||||||
const unpaddedLen = unpadded.length
|
const unpaddedLen = unpadded.length
|
||||||
const prefix = u.writeU16BE(unpaddedLen)
|
const prefix = u.writeU16BE(unpaddedLen)
|
||||||
const suffix = new Uint8Array(u.calcPaddedLen(unpaddedLen) - unpaddedLen)
|
const suffix = new Uint8Array(u.calcPaddedLen(unpaddedLen) - unpaddedLen)
|
||||||
return concatBytes(prefix, unpadded, suffix)
|
return concatBytes(prefix, unpadded, suffix)
|
||||||
},
|
}
|
||||||
|
|
||||||
unpad(padded: Uint8Array): string {
|
static unpad(padded: Uint8Array): string {
|
||||||
const unpaddedLen = new DataView(padded.buffer).getUint16(0)
|
const unpaddedLen = new DataView(padded.buffer).getUint16(0)
|
||||||
const unpadded = padded.subarray(2, 2 + unpaddedLen)
|
const unpadded = padded.subarray(2, 2 + unpaddedLen)
|
||||||
if (
|
if (
|
||||||
@@ -66,13 +71,13 @@ const u = {
|
|||||||
)
|
)
|
||||||
throw new Error('invalid padding')
|
throw new Error('invalid padding')
|
||||||
return u.utf8Decode(unpadded)
|
return u.utf8Decode(unpadded)
|
||||||
},
|
}
|
||||||
|
|
||||||
hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array) {
|
static hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array): Uint8Array {
|
||||||
if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')
|
if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')
|
||||||
const combined = concatBytes(aad, message)
|
const combined = concatBytes(aad, message)
|
||||||
return hmac(sha256, key, combined)
|
return hmac(sha256, key, combined)
|
||||||
},
|
}
|
||||||
|
|
||||||
// metadata: always 65b (version: 1b, nonce: 32b, max: 32b)
|
// metadata: always 65b (version: 1b, nonce: 32b, max: 32b)
|
||||||
// plaintext: 1b to 0xffff
|
// plaintext: 1b to 0xffff
|
||||||
@@ -80,7 +85,7 @@ const u = {
|
|||||||
// ciphertext: 32b+2 to 0xffff+2
|
// ciphertext: 32b+2 to 0xffff+2
|
||||||
// raw payload: 99 (65+32+2) to 65603 (65+0xffff+2)
|
// raw payload: 99 (65+32+2) to 65603 (65+0xffff+2)
|
||||||
// compressed payload (base64): 132b to 87472b
|
// compressed payload (base64): 132b to 87472b
|
||||||
decodePayload(payload: string) {
|
static decodePayload(payload: string): { nonce: Uint8Array; ciphertext: Uint8Array; mac: Uint8Array } {
|
||||||
if (typeof payload !== 'string') throw new Error('payload must be a valid string')
|
if (typeof payload !== 'string') throw new Error('payload must be a valid string')
|
||||||
const plen = payload.length
|
const plen = payload.length
|
||||||
if (plen < 132 || plen > 87472) throw new Error('invalid payload length: ' + plen)
|
if (plen < 132 || plen > 87472) throw new Error('invalid payload length: ' + plen)
|
||||||
@@ -100,30 +105,28 @@ const u = {
|
|||||||
ciphertext: data.subarray(33, -32),
|
ciphertext: data.subarray(33, -32),
|
||||||
mac: data.subarray(-32),
|
mac: data.subarray(-32),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function encrypt(plaintext: string, conversationKey: Uint8Array, nonce = randomBytes(32)): string {
|
export class v2 {
|
||||||
const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce)
|
static utils = u
|
||||||
const padded = u.pad(plaintext)
|
|
||||||
const ciphertext = chacha20(chacha_key, chacha_nonce, padded)
|
|
||||||
const mac = u.hmacAad(hmac_key, ciphertext, nonce)
|
|
||||||
return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))
|
|
||||||
}
|
|
||||||
|
|
||||||
function decrypt(payload: string, conversationKey: Uint8Array): string {
|
static encrypt(plaintext: string, conversationKey: Uint8Array, nonce: Uint8Array = randomBytes(32)): string {
|
||||||
const { nonce, ciphertext, mac } = u.decodePayload(payload)
|
const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce)
|
||||||
const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce)
|
const padded = u.pad(plaintext)
|
||||||
const calculatedMac = u.hmacAad(hmac_key, ciphertext, nonce)
|
const ciphertext = chacha20(chacha_key, chacha_nonce, padded)
|
||||||
if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')
|
const mac = u.hmacAad(hmac_key, ciphertext, nonce)
|
||||||
const padded = chacha20(chacha_key, chacha_nonce, ciphertext)
|
return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))
|
||||||
return u.unpad(padded)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export const v2 = {
|
static decrypt(payload: string, conversationKey: Uint8Array): string {
|
||||||
utils: u,
|
const { nonce, ciphertext, mac } = u.decodePayload(payload)
|
||||||
encrypt,
|
const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce)
|
||||||
decrypt,
|
const calculatedMac = u.hmacAad(hmac_key, ciphertext, nonce)
|
||||||
|
if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')
|
||||||
|
const padded = chacha20(chacha_key, chacha_nonce, ciphertext)
|
||||||
|
return u.unpad(padded)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default { v2 }
|
export default { v2 }
|
||||||
|
|||||||
16
nip47.ts
16
nip47.ts
@@ -1,8 +1,14 @@
|
|||||||
import { finalizeEvent } from './pure.ts'
|
import { type VerifiedEvent, finalizeEvent } from './pure.ts'
|
||||||
import { NWCWalletRequest } from './kinds.ts'
|
import { NWCWalletRequest } from './kinds.ts'
|
||||||
import { encrypt } from './nip04.ts'
|
import { encrypt } from './nip04.ts'
|
||||||
|
|
||||||
export function parseConnectionString(connectionString: string) {
|
interface NWCConnection {
|
||||||
|
pubkey: string
|
||||||
|
relay: string
|
||||||
|
secret: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseConnectionString(connectionString: string): NWCConnection {
|
||||||
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')
|
||||||
@@ -15,7 +21,11 @@ export function parseConnectionString(connectionString: string) {
|
|||||||
return { pubkey, relay, secret }
|
return { pubkey, relay, secret }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function makeNwcRequestEvent(pubkey: string, secretKey: Uint8Array, invoice: string) {
|
export async function makeNwcRequestEvent(
|
||||||
|
pubkey: string,
|
||||||
|
secretKey: Uint8Array,
|
||||||
|
invoice: string,
|
||||||
|
): Promise<VerifiedEvent> {
|
||||||
const content = {
|
const content = {
|
||||||
method: 'pay_invoice',
|
method: 'pay_invoice',
|
||||||
params: {
|
params: {
|
||||||
|
|||||||
2
pure.ts
2
pure.ts
@@ -50,7 +50,7 @@ export function getEventHash(event: UnsignedEvent): string {
|
|||||||
return bytesToHex(eventHash)
|
return bytesToHex(eventHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
const i = new JS()
|
const i: JS = new JS()
|
||||||
|
|
||||||
export const generateSecretKey = i.generateSecretKey
|
export const generateSecretKey = i.generateSecretKey
|
||||||
export const getPublicKey = i.getPublicKey
|
export const getPublicKey = i.getPublicKey
|
||||||
|
|||||||
2
relay.ts
2
relay.ts
@@ -13,7 +13,7 @@ export class Relay extends AbstractRelay {
|
|||||||
super(url, { verifyEvent })
|
super(url, { verifyEvent })
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string) {
|
static async connect(url: string): Promise<Relay> {
|
||||||
const relay = new Relay(url)
|
const relay = new Relay(url)
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
return relay
|
return relay
|
||||||
|
|||||||
8
utils.ts
8
utils.ts
@@ -1,7 +1,7 @@
|
|||||||
import type { Event } from './core.ts'
|
import type { Event } from './core.ts'
|
||||||
|
|
||||||
export const utf8Decoder = new TextDecoder('utf-8')
|
export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
|
||||||
export const utf8Encoder = new TextEncoder()
|
export const utf8Encoder: TextEncoder = new TextEncoder()
|
||||||
|
|
||||||
export function normalizeURL(url: string): string {
|
export function normalizeURL(url: string): string {
|
||||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||||
@@ -14,7 +14,7 @@ export function normalizeURL(url: string): string {
|
|||||||
return p.toString()
|
return p.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event) {
|
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] {
|
||||||
const [idx, found] = binarySearch(sortedArray, b => {
|
const [idx, found] = binarySearch(sortedArray, b => {
|
||||||
if (event.id === b.id) return 0
|
if (event.id === b.id) return 0
|
||||||
if (event.created_at === b.created_at) return -1
|
if (event.created_at === b.created_at) return -1
|
||||||
@@ -26,7 +26,7 @@ export function insertEventIntoDescendingList(sortedArray: Event[], event: Event
|
|||||||
return sortedArray
|
return sortedArray
|
||||||
}
|
}
|
||||||
|
|
||||||
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event) {
|
export function insertEventIntoAscendingList(sortedArray: Event[], event: Event): Event[] {
|
||||||
const [idx, found] = binarySearch(sortedArray, b => {
|
const [idx, found] = binarySearch(sortedArray, b => {
|
||||||
if (event.id === b.id) return 0
|
if (event.id === b.id) return 0
|
||||||
if (event.created_at === b.created_at) return -1
|
if (event.created_at === b.created_at) return -1
|
||||||
|
|||||||
Reference in New Issue
Block a user