mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 17:18:51 +00:00
Compare commits
7 Commits
v1.0.0-bet
...
v1.0.0-rc2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74a0d5454a | ||
|
|
c0d1e41424 | ||
|
|
f7e510e1c8 | ||
|
|
c08bdac7a7 | ||
|
|
c5b64404f6 | ||
|
|
c7b26fdba2 | ||
|
|
ac698ef67d |
@@ -53,7 +53,7 @@ import {
|
|||||||
} from 'nostr-tools'
|
} from 'nostr-tools'
|
||||||
|
|
||||||
const relay = relayInit('wss://relay.example.com')
|
const relay = relayInit('wss://relay.example.com')
|
||||||
relay.connect()
|
await relay.connect()
|
||||||
|
|
||||||
relay.on('connect', () => {
|
relay.on('connect', () => {
|
||||||
console.log(`connected to ${relay.url}`)
|
console.log(`connected to ${relay.url}`)
|
||||||
|
|||||||
2
event.ts
2
event.ts
@@ -64,6 +64,6 @@ export function verifySignature(
|
|||||||
|
|
||||||
export async function signEvent(event: Event, key: string): Promise<string> {
|
export async function signEvent(event: Event, key: string): Promise<string> {
|
||||||
return secp256k1.utils.bytesToHex(
|
return secp256k1.utils.bytesToHex(
|
||||||
await secp256k1.schnorr.sign(event.id || getEventHash(event), key)
|
await secp256k1.schnorr.sign(getEventHash(event), key)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export type Filter = {
|
|||||||
authors?: string[]
|
authors?: string[]
|
||||||
since?: number
|
since?: number
|
||||||
until?: number
|
until?: number
|
||||||
|
limit?: number
|
||||||
[key: `#${string}`]: string[]
|
[key: `#${string}`]: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
nip05.ts
8
nip05.ts
@@ -1,6 +1,10 @@
|
|||||||
import {ProfilePointer} from './nip19'
|
import {ProfilePointer} from './nip19'
|
||||||
|
|
||||||
var _fetch = fetch
|
var _fetch: any
|
||||||
|
|
||||||
|
try {
|
||||||
|
_fetch = fetch
|
||||||
|
} catch {}
|
||||||
|
|
||||||
export function useFetchImplementation(fetchImplementation: any) {
|
export function useFetchImplementation(fetchImplementation: any) {
|
||||||
_fetch = fetchImplementation
|
_fetch = fetchImplementation
|
||||||
@@ -32,6 +36,8 @@ export async function queryProfile(
|
|||||||
name = '_'
|
name = '_'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!name.match(/^[a-z0-9-_]+$/)) return null
|
||||||
|
|
||||||
let res = await (
|
let res = await (
|
||||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||||
).json()
|
).json()
|
||||||
|
|||||||
8
nip06.ts
8
nip06.ts
@@ -7,17 +7,13 @@ import {
|
|||||||
} from '@scure/bip39'
|
} from '@scure/bip39'
|
||||||
import {HDKey} from '@scure/bip32'
|
import {HDKey} from '@scure/bip32'
|
||||||
|
|
||||||
export function privateKeyFromSeed(seed: string): string {
|
export function privateKeyFromSeedWords(mnemonic: string): string {
|
||||||
let root = HDKey.fromMasterSeed(secp256k1.utils.hexToBytes(seed))
|
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic))
|
||||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
||||||
if (!privateKey) throw new Error('could not derive private key')
|
if (!privateKey) throw new Error('could not derive private key')
|
||||||
return secp256k1.utils.bytesToHex(privateKey)
|
return secp256k1.utils.bytesToHex(privateKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function seedFromWords(mnemonic: string): string {
|
|
||||||
return secp256k1.utils.bytesToHex(mnemonicToSeedSync(mnemonic))
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateSeedWords(): string {
|
export function generateSeedWords(): string {
|
||||||
return generateMnemonic(wordlist)
|
return generateMnemonic(wordlist)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.0.0-beta2",
|
"version": "1.0.0-rc2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
129
relay.ts
129
relay.ts
@@ -59,73 +59,77 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
} = {}
|
} = {}
|
||||||
|
|
||||||
function connectRelay() {
|
async function connectRelay(): Promise<void> {
|
||||||
ws = new WebSocket(url)
|
return new Promise((resolve, reject) => {
|
||||||
|
ws = new WebSocket(url)
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
listeners.connect.forEach(cb => cb())
|
listeners.connect.forEach(cb => cb())
|
||||||
}
|
resolve()
|
||||||
ws.onerror = () => {
|
}
|
||||||
listeners.error.forEach(cb => cb())
|
ws.onerror = () => {
|
||||||
}
|
listeners.error.forEach(cb => cb())
|
||||||
ws.onclose = async () => {
|
reject()
|
||||||
listeners.disconnect.forEach(cb => cb())
|
}
|
||||||
resolveClose()
|
ws.onclose = async () => {
|
||||||
}
|
listeners.disconnect.forEach(cb => cb())
|
||||||
|
resolveClose()
|
||||||
ws.onmessage = async e => {
|
|
||||||
var data
|
|
||||||
try {
|
|
||||||
data = JSON.parse(e.data)
|
|
||||||
} catch (err) {
|
|
||||||
data = e.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.length >= 1) {
|
ws.onmessage = async e => {
|
||||||
switch (data[0]) {
|
var data
|
||||||
case 'EVENT':
|
try {
|
||||||
if (data.length !== 3) return // ignore empty or malformed EVENT
|
data = JSON.parse(e.data)
|
||||||
|
} catch (err) {
|
||||||
|
data = e.data
|
||||||
|
}
|
||||||
|
|
||||||
let id = data[1]
|
if (data.length >= 1) {
|
||||||
let event = data[2]
|
switch (data[0]) {
|
||||||
if (
|
case 'EVENT':
|
||||||
validateEvent(event) &&
|
if (data.length !== 3) return // ignore empty or malformed EVENT
|
||||||
openSubs[id] &&
|
|
||||||
(openSubs[id].skipVerification || verifySignature(event)) &&
|
let id = data[1]
|
||||||
matchFilters(openSubs[id].filters, event)
|
let event = data[2]
|
||||||
) {
|
if (
|
||||||
openSubs[id]
|
validateEvent(event) &&
|
||||||
subListeners[id]?.event.forEach(cb => cb(event))
|
openSubs[id] &&
|
||||||
|
(openSubs[id].skipVerification || verifySignature(event)) &&
|
||||||
|
matchFilters(openSubs[id].filters, event)
|
||||||
|
) {
|
||||||
|
openSubs[id]
|
||||||
|
;(subListeners[id]?.event || []).forEach(cb => cb(event))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case 'EOSE': {
|
||||||
|
if (data.length !== 2) return // ignore empty or malformed EOSE
|
||||||
|
let id = data[1]
|
||||||
|
;(subListeners[id]?.eose || []).forEach(cb => cb())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
case 'OK': {
|
||||||
case 'EOSE': {
|
if (data.length < 3) return // ignore empty or malformed OK
|
||||||
if (data.length !== 2) return // ignore empty or malformed EOSE
|
let id: string = data[1]
|
||||||
let id = data[1]
|
let ok: boolean = data[2]
|
||||||
subListeners[id]?.eose.forEach(cb => cb())
|
let reason: string = data[3] || ''
|
||||||
return
|
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
||||||
|
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case 'NOTICE':
|
||||||
|
if (data.length !== 2) return // ignore empty or malformed NOTICE
|
||||||
|
let notice = data[1]
|
||||||
|
listeners.notice.forEach(cb => cb(notice))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
case 'OK': {
|
|
||||||
if (data.length < 3) return // ignore empty or malformed OK
|
|
||||||
let id: string = data[1]
|
|
||||||
let ok: boolean = data[2]
|
|
||||||
let reason: string = data[3] || ''
|
|
||||||
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
|
||||||
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case 'NOTICE':
|
|
||||||
if (data.length !== 2) return // ignore empty or malformed NOTICE
|
|
||||||
let notice = data[1]
|
|
||||||
listeners.notice.forEach(cb => cb(notice))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
async function connect(): Promise<void> {
|
||||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||||
connectRelay()
|
await connectRelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
@@ -170,8 +174,9 @@ export function relayInit(url: string): Relay {
|
|||||||
subListeners[subid][type].push(cb)
|
subListeners[subid][type].push(cb)
|
||||||
},
|
},
|
||||||
off: (type: 'event' | 'eose', cb: any): void => {
|
off: (type: 'event' | 'eose', cb: any): void => {
|
||||||
let idx = subListeners[subid][type].indexOf(cb)
|
let listeners = subListeners[subid]
|
||||||
if (idx >= 0) subListeners[subid][type].splice(idx, 1)
|
let idx = listeners[type].indexOf(cb)
|
||||||
|
if (idx >= 0) listeners[type].splice(idx, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,14 +222,14 @@ export function relayInit(url: string): Relay {
|
|||||||
id: `monitor-${id.slice(0, 5)}`
|
id: `monitor-${id.slice(0, 5)}`
|
||||||
})
|
})
|
||||||
let willUnsub = setTimeout(() => {
|
let willUnsub = setTimeout(() => {
|
||||||
pubListeners[id].failed.forEach(cb =>
|
;(pubListeners[id]?.failed || []).forEach(cb =>
|
||||||
cb('event not seen after 5 seconds')
|
cb('event not seen after 5 seconds')
|
||||||
)
|
)
|
||||||
monitor.unsub()
|
monitor.unsub()
|
||||||
}, 5000)
|
}, 5000)
|
||||||
monitor.on('event', () => {
|
monitor.on('event', () => {
|
||||||
clearTimeout(willUnsub)
|
clearTimeout(willUnsub)
|
||||||
pubListeners[id].seen.forEach(cb => cb())
|
;(pubListeners[id]?.seen || []).forEach(cb => cb())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +248,10 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
off: (type: 'ok' | 'seen' | 'failed', cb: any) => {
|
||||||
let idx = pubListeners[id][type].indexOf(cb)
|
let listeners = pubListeners[id]
|
||||||
if (idx >= 0) pubListeners[id][type].splice(idx, 1)
|
if (!listeners) return
|
||||||
|
let idx = listeners[type].indexOf(cb)
|
||||||
|
if (idx >= 0) listeners[type].splice(idx, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user