mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac212cb5c8 | ||
|
|
204ae0eff1 | ||
|
|
f17ab41d72 | ||
|
|
f6f5ee8223 | ||
|
|
a05506468d | ||
|
|
674ff66b6f | ||
|
|
731705047a | ||
|
|
94b382a49f | ||
|
|
199411a971 | ||
|
|
a1dc6f41b9 | ||
|
|
5b59b93d86 | ||
|
|
12acd7bdca | ||
|
|
3bdb68020d | ||
|
|
b0a58e2ca4 | ||
|
|
b063be76ae | ||
|
|
e3cea5db16 | ||
|
|
9ee58bd6c7 | ||
|
|
f1eb9a3bc7 | ||
|
|
ce081bb4cb | ||
|
|
7413072e9f | ||
|
|
4c464b39cf | ||
|
|
11ef43abdc | ||
|
|
3e67f9b014 | ||
|
|
0933fba6d5 |
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
22
event.ts
22
event.ts
@@ -1,5 +1,6 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {schnorr} from '@noble/curves/secp256k1'
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
import {sha256} from '@noble/hashes/sha256'
|
||||||
|
import {bytesToHex} from '@noble/hashes/utils'
|
||||||
|
|
||||||
import {utf8Encoder} from './utils'
|
import {utf8Encoder} from './utils'
|
||||||
import {getPublicKey} from './keys'
|
import {getPublicKey} from './keys'
|
||||||
@@ -13,6 +14,7 @@ export enum Kind {
|
|||||||
EncryptedDirectMessage = 4,
|
EncryptedDirectMessage = 4,
|
||||||
EventDeletion = 5,
|
EventDeletion = 5,
|
||||||
Reaction = 7,
|
Reaction = 7,
|
||||||
|
BadgeAward = 8,
|
||||||
ChannelCreation = 40,
|
ChannelCreation = 40,
|
||||||
ChannelMetadata = 41,
|
ChannelMetadata = 41,
|
||||||
ChannelMessage = 42,
|
ChannelMessage = 42,
|
||||||
@@ -23,6 +25,8 @@ export enum Kind {
|
|||||||
Zap = 9735,
|
Zap = 9735,
|
||||||
RelayList = 10002,
|
RelayList = 10002,
|
||||||
ClientAuth = 22242,
|
ClientAuth = 22242,
|
||||||
|
BadgeDefinition = 30008,
|
||||||
|
ProfileBadge = 30009,
|
||||||
Article = 30023
|
Article = 30023
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,11 +79,13 @@ export function serializeEvent(evt: UnsignedEvent): string {
|
|||||||
|
|
||||||
export function getEventHash(event: UnsignedEvent): string {
|
export function getEventHash(event: UnsignedEvent): string {
|
||||||
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
||||||
return secp256k1.utils.bytesToHex(eventHash)
|
return bytesToHex(eventHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateEvent(event: UnsignedEvent): boolean {
|
const isRecord = (obj: unknown): obj is Record<string, unknown> => obj instanceof Object
|
||||||
if (typeof event !== 'object') return false
|
|
||||||
|
export function validateEvent<T>(event: T): event is T & UnsignedEvent {
|
||||||
|
if (!isRecord(event)) return false
|
||||||
if (typeof event.kind !== 'number') return false
|
if (typeof event.kind !== 'number') return false
|
||||||
if (typeof event.content !== 'string') return false
|
if (typeof event.content !== 'string') return false
|
||||||
if (typeof event.created_at !== 'number') return false
|
if (typeof event.created_at !== 'number') return false
|
||||||
@@ -98,8 +104,8 @@ export function validateEvent(event: UnsignedEvent): boolean {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export function verifySignature(event: Event & {sig: string}): boolean {
|
export function verifySignature(event: Event): boolean {
|
||||||
return secp256k1.schnorr.verifySync(
|
return schnorr.verify(
|
||||||
event.sig,
|
event.sig,
|
||||||
getEventHash(event),
|
getEventHash(event),
|
||||||
event.pubkey
|
event.pubkey
|
||||||
@@ -107,7 +113,7 @@ export function verifySignature(event: Event & {sig: string}): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function signEvent(event: UnsignedEvent, key: string): string {
|
export function signEvent(event: UnsignedEvent, key: string): string {
|
||||||
return secp256k1.utils.bytesToHex(
|
return bytesToHex(
|
||||||
secp256k1.schnorr.signSync(getEventHash(event), key)
|
schnorr.sign(getEventHash(event), key)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,16 @@ describe('Filter', () => {
|
|||||||
expect(result).toEqual(false)
|
expect(result).toEqual(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return true when the event id starts with a prefix', () => {
|
||||||
|
const filter = {ids: ['22', '00']}
|
||||||
|
|
||||||
|
const event = {id: '001'}
|
||||||
|
|
||||||
|
const result = matchFilter(filter, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
it('should return false when the event kind is not in the filter', () => {
|
it('should return false when the event kind is not in the filter', () => {
|
||||||
const filter = {kinds: [1, 2, 3]}
|
const filter = {kinds: [1, 2, 3]}
|
||||||
|
|
||||||
@@ -132,6 +142,20 @@ describe('Filter', () => {
|
|||||||
expect(result).toEqual(true)
|
expect(result).toEqual(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return true when at least one prefix matches the event', () => {
|
||||||
|
const filters = [
|
||||||
|
{ids: ['1'], kinds: [1], authors: ['a']},
|
||||||
|
{ids: ['4'], kinds: [2], authors: ['d']},
|
||||||
|
{ids: ['9'], kinds: [3], authors: ['g']}
|
||||||
|
]
|
||||||
|
|
||||||
|
const event = {id: '987', kind: 3, pubkey: 'ghi'}
|
||||||
|
|
||||||
|
const result = matchFilters(filters, event)
|
||||||
|
|
||||||
|
expect(result).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
it('should return true when event matches one or more filters and some have limit set', () => {
|
it('should return true when event matches one or more filters and some have limit set', () => {
|
||||||
const filters = [
|
const filters = [
|
||||||
{ids: ['123'], limit: 1},
|
{ids: ['123'], limit: 1},
|
||||||
|
|||||||
17
filter.ts
17
filter.ts
@@ -13,12 +13,19 @@ export type Filter = {
|
|||||||
|
|
||||||
export function matchFilter(
|
export function matchFilter(
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
event: Event & {id: string}
|
event: Event
|
||||||
): boolean {
|
): boolean {
|
||||||
if (filter.ids && filter.ids.indexOf(event.id) === -1) return false
|
if (filter.ids && filter.ids.indexOf(event.id) === -1) {
|
||||||
|
if (!filter.ids.some(prefix => event.id.startsWith(prefix))) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
|
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
|
||||||
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1)
|
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1) {
|
||||||
return false
|
if (!filter.authors.some(prefix => event.pubkey.startsWith(prefix))) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let f in filter) {
|
for (let f in filter) {
|
||||||
if (f[0] === '#') {
|
if (f[0] === '#') {
|
||||||
@@ -42,7 +49,7 @@ export function matchFilter(
|
|||||||
|
|
||||||
export function matchFilters(
|
export function matchFilters(
|
||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
event: Event & {id: string}
|
event: Event
|
||||||
): boolean {
|
): boolean {
|
||||||
for (let i = 0; i < filters.length; i++) {
|
for (let i = 0; i < filters.length; i++) {
|
||||||
if (matchFilter(filters[i], event)) return true
|
if (matchFilter(filters[i], event)) return true
|
||||||
|
|||||||
11
index.ts
11
index.ts
@@ -8,6 +8,8 @@ export * from './references'
|
|||||||
export * as nip04 from './nip04'
|
export * as nip04 from './nip04'
|
||||||
export * as nip05 from './nip05'
|
export * as nip05 from './nip05'
|
||||||
export * as nip06 from './nip06'
|
export * as nip06 from './nip06'
|
||||||
|
export * as nip10 from './nip10'
|
||||||
|
export * as nip13 from './nip13'
|
||||||
export * as nip19 from './nip19'
|
export * as nip19 from './nip19'
|
||||||
export * as nip26 from './nip26'
|
export * as nip26 from './nip26'
|
||||||
export * as nip39 from './nip39'
|
export * as nip39 from './nip39'
|
||||||
@@ -15,12 +17,3 @@ export * as nip57 from './nip57'
|
|||||||
|
|
||||||
export * as fj from './fakejson'
|
export * as fj from './fakejson'
|
||||||
export * as utils from './utils'
|
export * as utils from './utils'
|
||||||
|
|
||||||
// monkey patch secp256k1
|
|
||||||
import * as secp256k1 from '@noble/secp256k1'
|
|
||||||
import {hmac} from '@noble/hashes/hmac'
|
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
|
||||||
secp256k1.utils.hmacSha256Sync = (key, ...msgs) =>
|
|
||||||
hmac(sha256, key, secp256k1.utils.concatBytes(...msgs))
|
|
||||||
secp256k1.utils.sha256Sync = (...msgs) =>
|
|
||||||
sha256(secp256k1.utils.concatBytes(...msgs))
|
|
||||||
|
|||||||
7
keys.ts
7
keys.ts
@@ -1,9 +1,10 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {schnorr} from '@noble/curves/secp256k1'
|
||||||
|
import {bytesToHex} from '@noble/hashes/utils'
|
||||||
|
|
||||||
export function generatePrivateKey(): string {
|
export function generatePrivateKey(): string {
|
||||||
return secp256k1.utils.bytesToHex(secp256k1.utils.randomPrivateKey())
|
return bytesToHex(schnorr.utils.randomPrivateKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPublicKey(privateKey: string): string {
|
export function getPublicKey(privateKey: string): string {
|
||||||
return secp256k1.utils.bytesToHex(secp256k1.schnorr.getPublicKey(privateKey))
|
return bytesToHex(schnorr.getPublicKey(privateKey))
|
||||||
}
|
}
|
||||||
|
|||||||
2
nip04.ts
2
nip04.ts
@@ -1,5 +1,5 @@
|
|||||||
import {randomBytes} from '@noble/hashes/utils'
|
import {randomBytes} from '@noble/hashes/utils'
|
||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {secp256k1} from '@noble/curves/secp256k1'
|
||||||
import {base64} from '@scure/base'
|
import {base64} from '@scure/base'
|
||||||
|
|
||||||
import {utf8Decoder, utf8Encoder} from './utils'
|
import {utf8Decoder, utf8Encoder} from './utils'
|
||||||
|
|||||||
@@ -17,4 +17,9 @@ test('fetch nip05 profiles', async () => {
|
|||||||
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
'32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'
|
||||||
)
|
)
|
||||||
expect(p2.relays).toEqual(['wss://relay.damus.io'])
|
expect(p2.relays).toEqual(['wss://relay.damus.io'])
|
||||||
|
|
||||||
|
let p3 = await nip05.queryProfile('channel.ninja@channel.ninja')
|
||||||
|
expect(p3.pubkey).toEqual(
|
||||||
|
'36e65b503eba8a6b698e724a59137603101166a1cddb45ddc704247fc8aa0fce'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
2
nip05.ts
2
nip05.ts
@@ -36,7 +36,7 @@ export async function queryProfile(
|
|||||||
name = '_'
|
name = '_'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
|
if (!name.match(/^[A-Za-z0-9-_.]+$/)) return null
|
||||||
if (!domain.includes('.')) return null
|
if (!domain.includes('.')) return null
|
||||||
|
|
||||||
let res
|
let res
|
||||||
|
|||||||
4
nip06.ts
4
nip06.ts
@@ -1,4 +1,4 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {bytesToHex} from '@noble/hashes/utils'
|
||||||
import {wordlist} from '@scure/bip39/wordlists/english.js'
|
import {wordlist} from '@scure/bip39/wordlists/english.js'
|
||||||
import {
|
import {
|
||||||
generateMnemonic,
|
generateMnemonic,
|
||||||
@@ -14,7 +14,7 @@ export function privateKeyFromSeedWords(
|
|||||||
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
|
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
|
||||||
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 bytesToHex(privateKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateSeedWords(): string {
|
export function generateSeedWords(): string {
|
||||||
|
|||||||
353
nip10.test.js
Normal file
353
nip10.test.js
Normal file
@@ -0,0 +1,353 @@
|
|||||||
|
/* eslint-env jest */
|
||||||
|
|
||||||
|
const {nip10} = require('./lib/nostr.cjs')
|
||||||
|
|
||||||
|
describe('parse NIP10-referenced events', () => {
|
||||||
|
test('legacy + a lot of events', () => {
|
||||||
|
let event = {
|
||||||
|
tags: [
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'5e081ebb19153357d7c31e8a10b9ceeef29313f58dc8d701f66727fab02aef64'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'49aff7ae6daeaaa2777931b90f9bb29f6cb01c5a3d7d88c8ba82d890f264afb4'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'567b7c11f0fe582361e3cea6fcc7609a8942dfe196ee1b98d5604c93fbeea976'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'090c037b2e399ee74d9f134758928948dd9154413ca1a1acb37155046e03a051'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'89f220b63465c93542b1a78caa3a952cf4f196e91a50596493c8093c533ebc4d'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(nip10.parse(event)).toEqual({
|
||||||
|
mentions: [
|
||||||
|
{
|
||||||
|
id: 'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '5e081ebb19153357d7c31e8a10b9ceeef29313f58dc8d701f66727fab02aef64',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '49aff7ae6daeaaa2777931b90f9bb29f6cb01c5a3d7d88c8ba82d890f264afb4',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '567b7c11f0fe582361e3cea6fcc7609a8942dfe196ee1b98d5604c93fbeea976',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '090c037b2e399ee74d9f134758928948dd9154413ca1a1acb37155046e03a051',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
reply: {
|
||||||
|
id: '89f220b63465c93542b1a78caa3a952cf4f196e91a50596493c8093c533ebc4d',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
root: {
|
||||||
|
id: 'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('legacy + 3 events', () => {
|
||||||
|
let event = {
|
||||||
|
tags: [
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'5e081ebb19153357d7c31e8a10b9ceeef29313f58dc8d701f66727fab02aef64'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(nip10.parse(event)).toEqual({
|
||||||
|
mentions: [
|
||||||
|
{
|
||||||
|
id: 'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
reply: {
|
||||||
|
id: '5e081ebb19153357d7c31e8a10b9ceeef29313f58dc8d701f66727fab02aef64',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
root: {
|
||||||
|
id: 'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('legacy + 2 events', () => {
|
||||||
|
let event = {
|
||||||
|
tags: [
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(nip10.parse(event)).toEqual({
|
||||||
|
mentions: [],
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'77ce56f89d1228f7ff3743ce1ad1b254857b9008564727ebd5a1f317362f6ca7',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'4ca4f5533e40da5e0508796d409e6bb35a50b26fc304345617ab017183d83ac0',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
reply: {
|
||||||
|
id: 'bbd72f0ae14374aa8fb166b483cfcf99b57d7f4cf1600ccbf17c350040834631',
|
||||||
|
relays: []
|
||||||
|
},
|
||||||
|
root: {
|
||||||
|
id: 'b857504288c18a15950dd05b9e8772c62ca6289d5aac373c0a8ee5b132e94e7c',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('legacy + 1 event', () => {
|
||||||
|
let event = {
|
||||||
|
tags: [
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'9abbfd9b9ac5ecdab45d14b8bf8d746139ea039e931a1b376d19a239f1946590'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(nip10.parse(event)).toEqual({
|
||||||
|
mentions: [],
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'534780e44da7b494485e85cd4cca6af4f6caa1627472432b6f2a4ece0e9e54ec',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
reply: undefined,
|
||||||
|
root: {
|
||||||
|
id: '9abbfd9b9ac5ecdab45d14b8bf8d746139ea039e931a1b376d19a239f1946590',
|
||||||
|
relays: []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.todo('recommended + a lot of events')
|
||||||
|
test.todo('recommended + 3 events')
|
||||||
|
test.todo('recommended + 2 events')
|
||||||
|
|
||||||
|
test('recommended + 1 event', () => {
|
||||||
|
let event = {
|
||||||
|
tags: [
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'a8c21fcd8aa1f4befba14d72fc7a012397732d30d8b3131af912642f3c726f52',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'003d7fd21fd09ff7f6f63a75daf194dd99feefbe6919cc376b7359d5090aa9a6',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'2f6fbe452edd3987d3c67f3b034c03ec5bcf4d054c521c3a954686f89f03212e',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'44c7c74668ff222b0e0b30579c49fc6e22dafcdeaad091036c947f9856590f1e',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'c5cf39149caebda4cdd61771c51f6ba91ef5645919004e5c4998a4ea69f00512',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'094d44bb1e812696c57f57ad1c0c707812dedbe72c07e538b80639032c236a9e',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'p',
|
||||||
|
'a1ba0ac9b6ec098f726a3c11ec654df4a32cbb84b5377e8788395e9c27d9ecda',
|
||||||
|
'wss://relay.mostr.pub'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'e',
|
||||||
|
'f9472913904ab7e9da008dcb2d85fd4af2d2993ada483d00c646d0c4481d031d',
|
||||||
|
'wss://relay.mostr.pub',
|
||||||
|
'reply'
|
||||||
|
],
|
||||||
|
['mostr', 'https://poa.st/objects/dc50684b-6364-4264-ab16-49f4622f05ea']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(nip10.parse(event)).toEqual({
|
||||||
|
mentions: [],
|
||||||
|
profiles: [
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'a8c21fcd8aa1f4befba14d72fc7a012397732d30d8b3131af912642f3c726f52',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'003d7fd21fd09ff7f6f63a75daf194dd99feefbe6919cc376b7359d5090aa9a6',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'2f6fbe452edd3987d3c67f3b034c03ec5bcf4d054c521c3a954686f89f03212e',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'44c7c74668ff222b0e0b30579c49fc6e22dafcdeaad091036c947f9856590f1e',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'c5cf39149caebda4cdd61771c51f6ba91ef5645919004e5c4998a4ea69f00512',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'094d44bb1e812696c57f57ad1c0c707812dedbe72c07e538b80639032c236a9e',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pubkey:
|
||||||
|
'a1ba0ac9b6ec098f726a3c11ec654df4a32cbb84b5377e8788395e9c27d9ecda',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
}
|
||||||
|
],
|
||||||
|
reply: {
|
||||||
|
id: 'f9472913904ab7e9da008dcb2d85fd4af2d2993ada483d00c646d0c4481d031d',
|
||||||
|
relays: ['wss://relay.mostr.pub']
|
||||||
|
},
|
||||||
|
root: undefined
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
96
nip10.ts
Normal file
96
nip10.ts
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import type {Event} from './event'
|
||||||
|
import type {EventPointer, ProfilePointer} from './nip19'
|
||||||
|
|
||||||
|
export type NIP10Result = {
|
||||||
|
/**
|
||||||
|
* Pointer to the root of the thread.
|
||||||
|
*/
|
||||||
|
root: EventPointer | undefined
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pointer to a "parent" event that parsed event replies to (responded to).
|
||||||
|
*/
|
||||||
|
reply: EventPointer | undefined
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pointers to events which may or may not be in the reply chain.
|
||||||
|
*/
|
||||||
|
mentions: EventPointer[]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of pubkeys that are involved in the thread in no particular order.
|
||||||
|
*/
|
||||||
|
profiles: ProfilePointer[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parse(event: Pick<Event, 'tags'>): NIP10Result {
|
||||||
|
const result: NIP10Result = {
|
||||||
|
reply: undefined,
|
||||||
|
root: undefined,
|
||||||
|
mentions: [],
|
||||||
|
profiles: []
|
||||||
|
}
|
||||||
|
|
||||||
|
const eTags: string[][] = []
|
||||||
|
|
||||||
|
for (const tag of event.tags) {
|
||||||
|
if (tag[0] === 'e' && tag[1]) {
|
||||||
|
eTags.push(tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tag[0] === 'p' && tag[1]) {
|
||||||
|
result.profiles.push({
|
||||||
|
pubkey: tag[1],
|
||||||
|
relays: tag[2] ? [tag[2]] : []
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let eTagIndex = 0; eTagIndex < eTags.length; eTagIndex++) {
|
||||||
|
const eTag = eTags[eTagIndex]
|
||||||
|
|
||||||
|
const [_, eTagEventId, eTagRelayUrl, eTagMarker] = eTag as [
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
undefined | string,
|
||||||
|
undefined | string
|
||||||
|
]
|
||||||
|
|
||||||
|
const eventPointer: EventPointer = {
|
||||||
|
id: eTagEventId,
|
||||||
|
relays: eTagRelayUrl ? [eTagRelayUrl] : []
|
||||||
|
}
|
||||||
|
|
||||||
|
const isFirstETag = eTagIndex === 0
|
||||||
|
const isLastETag = eTagIndex === eTags.length - 1
|
||||||
|
|
||||||
|
if (eTagMarker === 'root') {
|
||||||
|
result.root = eventPointer
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eTagMarker === 'reply') {
|
||||||
|
result.reply = eventPointer
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eTagMarker === 'mention') {
|
||||||
|
result.mentions.push(eventPointer)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFirstETag) {
|
||||||
|
result.root = eventPointer
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLastETag) {
|
||||||
|
result.reply = eventPointer
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result.mentions.push(eventPointer)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
8
nip13.test.js
Normal file
8
nip13.test.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/* eslint-env jest */
|
||||||
|
const {nip13} = require('./lib/nostr.cjs')
|
||||||
|
|
||||||
|
test('identifies proof-of-work difficulty', async () => {
|
||||||
|
const id = '000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358'
|
||||||
|
const difficulty = nip13.getPow(id)
|
||||||
|
expect(difficulty).toEqual(21)
|
||||||
|
})
|
||||||
42
nip13.ts
Normal file
42
nip13.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import {hexToBytes} from '@noble/hashes/utils'
|
||||||
|
|
||||||
|
/** Get POW difficulty from a Nostr hex ID. */
|
||||||
|
export function getPow(id: string): number {
|
||||||
|
return getLeadingZeroBits(hexToBytes(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get number of leading 0 bits. Adapted from nostream.
|
||||||
|
* https://github.com/Cameri/nostream/blob/fb6948fd83ca87ce552f39f9b5eb780ea07e272e/src/utils/proof-of-work.ts
|
||||||
|
*/
|
||||||
|
function getLeadingZeroBits(hash: Uint8Array): number {
|
||||||
|
let total: number, i: number, bits: number
|
||||||
|
|
||||||
|
for (i = 0, total = 0; i < hash.length; i++) {
|
||||||
|
bits = msb(hash[i])
|
||||||
|
total += bits
|
||||||
|
if (bits !== 8) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adapted from nostream.
|
||||||
|
* https://github.com/Cameri/nostream/blob/fb6948fd83ca87ce552f39f9b5eb780ea07e272e/src/utils/proof-of-work.ts
|
||||||
|
*/
|
||||||
|
function msb(b: number) {
|
||||||
|
let n = 0
|
||||||
|
|
||||||
|
if (b === 0) {
|
||||||
|
return 8
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-cond-assign
|
||||||
|
while (b >>= 1) {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
return 7 - n
|
||||||
|
}
|
||||||
@@ -100,3 +100,12 @@ test('decode naddr from go-nostr with different TLV ordering', () => {
|
|||||||
expect(data.kind).toEqual(30023)
|
expect(data.kind).toEqual(30023)
|
||||||
expect(data.identifier).toEqual('banana')
|
expect(data.identifier).toEqual('banana')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('encode and decode nrelay', () => {
|
||||||
|
let url = "wss://relay.nostr.example"
|
||||||
|
let nrelay = nip19.nrelayEncode(url)
|
||||||
|
expect(nrelay).toMatch(/nrelay1\w+/)
|
||||||
|
let {type, data} = nip19.decode(nrelay)
|
||||||
|
expect(type).toEqual('nrelay')
|
||||||
|
expect(data).toEqual(url)
|
||||||
|
})
|
||||||
|
|||||||
44
nip19.ts
44
nip19.ts
@@ -1,4 +1,4 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {bytesToHex, concatBytes, hexToBytes} from '@noble/hashes/utils'
|
||||||
import {bech32} from '@scure/base'
|
import {bech32} from '@scure/base'
|
||||||
|
|
||||||
import {utf8Decoder, utf8Encoder} from './utils'
|
import {utf8Decoder, utf8Encoder} from './utils'
|
||||||
@@ -39,7 +39,7 @@ export function decode(nip19: string): {
|
|||||||
return {
|
return {
|
||||||
type: 'nprofile',
|
type: 'nprofile',
|
||||||
data: {
|
data: {
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
pubkey: bytesToHex(tlv[0][0]),
|
||||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,10 +54,10 @@ export function decode(nip19: string): {
|
|||||||
return {
|
return {
|
||||||
type: 'nevent',
|
type: 'nevent',
|
||||||
data: {
|
data: {
|
||||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
id: bytesToHex(tlv[0][0]),
|
||||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
|
||||||
author: tlv[2]?.[0]
|
author: tlv[2]?.[0]
|
||||||
? secp256k1.utils.bytesToHex(tlv[2][0])
|
? bytesToHex(tlv[2][0])
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,17 +75,27 @@ export function decode(nip19: string): {
|
|||||||
type: 'naddr',
|
type: 'naddr',
|
||||||
data: {
|
data: {
|
||||||
identifier: utf8Decoder.decode(tlv[0][0]),
|
identifier: utf8Decoder.decode(tlv[0][0]),
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
pubkey: bytesToHex(tlv[2][0]),
|
||||||
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
|
kind: parseInt(bytesToHex(tlv[3][0]), 16),
|
||||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'nrelay': {
|
||||||
|
let tlv = parseTLV(data)
|
||||||
|
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nrelay')
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'nrelay',
|
||||||
|
data: utf8Decoder.decode(tlv[0][0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case 'nsec':
|
case 'nsec':
|
||||||
case 'npub':
|
case 'npub':
|
||||||
case 'note':
|
case 'note':
|
||||||
return {type: prefix, data: secp256k1.utils.bytesToHex(data)}
|
return {type: prefix, data: bytesToHex(data)}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Error(`unknown prefix ${prefix}`)
|
throw new Error(`unknown prefix ${prefix}`)
|
||||||
@@ -122,14 +132,14 @@ export function noteEncode(hex: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function encodeBytes(prefix: string, hex: string): string {
|
function encodeBytes(prefix: string, hex: string): string {
|
||||||
let data = secp256k1.utils.hexToBytes(hex)
|
let data = hexToBytes(hex)
|
||||||
let words = bech32.toWords(data)
|
let words = bech32.toWords(data)
|
||||||
return bech32.encode(prefix, words, Bech32MaxSize)
|
return bech32.encode(prefix, words, Bech32MaxSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function nprofileEncode(profile: ProfilePointer): string {
|
export function nprofileEncode(profile: ProfilePointer): string {
|
||||||
let data = encodeTLV({
|
let data = encodeTLV({
|
||||||
0: [secp256k1.utils.hexToBytes(profile.pubkey)],
|
0: [hexToBytes(profile.pubkey)],
|
||||||
1: (profile.relays || []).map(url => utf8Encoder.encode(url))
|
1: (profile.relays || []).map(url => utf8Encoder.encode(url))
|
||||||
})
|
})
|
||||||
let words = bech32.toWords(data)
|
let words = bech32.toWords(data)
|
||||||
@@ -138,9 +148,9 @@ export function nprofileEncode(profile: ProfilePointer): string {
|
|||||||
|
|
||||||
export function neventEncode(event: EventPointer): string {
|
export function neventEncode(event: EventPointer): string {
|
||||||
let data = encodeTLV({
|
let data = encodeTLV({
|
||||||
0: [secp256k1.utils.hexToBytes(event.id)],
|
0: [hexToBytes(event.id)],
|
||||||
1: (event.relays || []).map(url => utf8Encoder.encode(url)),
|
1: (event.relays || []).map(url => utf8Encoder.encode(url)),
|
||||||
2: event.author ? [secp256k1.utils.hexToBytes(event.author)] : []
|
2: event.author ? [hexToBytes(event.author)] : []
|
||||||
})
|
})
|
||||||
let words = bech32.toWords(data)
|
let words = bech32.toWords(data)
|
||||||
return bech32.encode('nevent', words, Bech32MaxSize)
|
return bech32.encode('nevent', words, Bech32MaxSize)
|
||||||
@@ -153,13 +163,21 @@ export function naddrEncode(addr: AddressPointer): string {
|
|||||||
let data = encodeTLV({
|
let data = encodeTLV({
|
||||||
0: [utf8Encoder.encode(addr.identifier)],
|
0: [utf8Encoder.encode(addr.identifier)],
|
||||||
1: (addr.relays || []).map(url => utf8Encoder.encode(url)),
|
1: (addr.relays || []).map(url => utf8Encoder.encode(url)),
|
||||||
2: [secp256k1.utils.hexToBytes(addr.pubkey)],
|
2: [hexToBytes(addr.pubkey)],
|
||||||
3: [new Uint8Array(kind)]
|
3: [new Uint8Array(kind)]
|
||||||
})
|
})
|
||||||
let words = bech32.toWords(data)
|
let words = bech32.toWords(data)
|
||||||
return bech32.encode('naddr', words, Bech32MaxSize)
|
return bech32.encode('naddr', words, Bech32MaxSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function nrelayEncode(url: string): string {
|
||||||
|
let data = encodeTLV({
|
||||||
|
0: [utf8Encoder.encode(url)]
|
||||||
|
})
|
||||||
|
let words = bech32.toWords(data)
|
||||||
|
return bech32.encode('nrelay', words, Bech32MaxSize)
|
||||||
|
}
|
||||||
|
|
||||||
function encodeTLV(tlv: TLV): Uint8Array {
|
function encodeTLV(tlv: TLV): Uint8Array {
|
||||||
let entries: Uint8Array[] = []
|
let entries: Uint8Array[] = []
|
||||||
|
|
||||||
@@ -173,5 +191,5 @@ function encodeTLV(tlv: TLV): Uint8Array {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return secp256k1.utils.concatBytes(...entries)
|
return concatBytes(...entries)
|
||||||
}
|
}
|
||||||
|
|||||||
9
nip26.ts
9
nip26.ts
@@ -1,4 +1,5 @@
|
|||||||
import * as secp256k1 from '@noble/secp256k1'
|
import {schnorr} from '@noble/curves/secp256k1'
|
||||||
|
import {bytesToHex} from '@noble/hashes/utils'
|
||||||
import {sha256} from '@noble/hashes/sha256'
|
import {sha256} from '@noble/hashes/sha256'
|
||||||
|
|
||||||
import {Event} from './event'
|
import {Event} from './event'
|
||||||
@@ -36,8 +37,8 @@ export function createDelegation(
|
|||||||
utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`)
|
utf8Encoder.encode(`nostr:delegation:${parameters.pubkey}:${cond}`)
|
||||||
)
|
)
|
||||||
|
|
||||||
let sig = secp256k1.utils.bytesToHex(
|
let sig = bytesToHex(
|
||||||
secp256k1.schnorr.signSync(sighash, privateKey)
|
schnorr.sign(sighash, privateKey)
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -84,7 +85,7 @@ export function getDelegator(event: Event): string | null {
|
|||||||
let sighash = sha256(
|
let sighash = sha256(
|
||||||
utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`)
|
utf8Encoder.encode(`nostr:delegation:${event.pubkey}:${cond}`)
|
||||||
)
|
)
|
||||||
if (!secp256k1.schnorr.verifySync(sig, sighash, pubkey)) return null
|
if (!schnorr.verify(sig, sighash, pubkey)) return null
|
||||||
|
|
||||||
return pubkey
|
return pubkey
|
||||||
}
|
}
|
||||||
|
|||||||
19
package.json
19
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.8.1",
|
"version": "1.9.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -18,12 +18,11 @@
|
|||||||
},
|
},
|
||||||
"license": "Public domain",
|
"license": "Public domain",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "1.0.0",
|
"@noble/curves": "1.0.0",
|
||||||
"@noble/secp256k1": "^1.7.1",
|
"@noble/hashes": "1.3.0",
|
||||||
"@scure/base": "^1.1.1",
|
"@scure/base": "1.1.1",
|
||||||
"@scure/bip32": "^1.1.5",
|
"@scure/bip32": "1.3.0",
|
||||||
"@scure/bip39": "^1.1.1",
|
"@scure/bip39": "1.2.0"
|
||||||
"prettier": "^2.8.4"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"decentralization",
|
"decentralization",
|
||||||
@@ -32,6 +31,11 @@
|
|||||||
"client",
|
"client",
|
||||||
"nostr"
|
"nostr"
|
||||||
],
|
],
|
||||||
|
"scripts": {
|
||||||
|
"build": "node build",
|
||||||
|
"format": "prettier --plugin-search-dir . --write .",
|
||||||
|
"test": "node build && jest"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.13.0",
|
"@types/node": "^18.13.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
||||||
@@ -44,6 +48,7 @@
|
|||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
"jest": "^29.4.2",
|
"jest": "^29.4.2",
|
||||||
"node-fetch": "^2.6.9",
|
"node-fetch": "^2.6.9",
|
||||||
|
"prettier": "^2.8.4",
|
||||||
"ts-jest": "^29.0.5",
|
"ts-jest": "^29.0.5",
|
||||||
"tsd": "^0.22.0",
|
"tsd": "^0.22.0",
|
||||||
"typescript": "^4.9.5",
|
"typescript": "^4.9.5",
|
||||||
|
|||||||
48
pool.ts
48
pool.ts
@@ -26,22 +26,16 @@ export class SimplePool {
|
|||||||
|
|
||||||
async ensureRelay(url: string): Promise<Relay> {
|
async ensureRelay(url: string): Promise<Relay> {
|
||||||
const nm = normalizeURL(url)
|
const nm = normalizeURL(url)
|
||||||
const existing = this._conn[nm]
|
|
||||||
if (existing && existing.status === 1) return existing
|
|
||||||
|
|
||||||
if (existing) {
|
if (!this._conn[nm]) {
|
||||||
await existing.connect()
|
this._conn[nm] = relayInit(nm, {
|
||||||
return existing
|
getTimeout: this.getTimeout * 0.9,
|
||||||
|
listTimeout: this.getTimeout * 0.9
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const relay = relayInit(nm, {
|
const relay = this._conn[nm]
|
||||||
getTimeout: this.getTimeout * 0.9,
|
|
||||||
listTimeout: this.getTimeout * 0.9
|
|
||||||
})
|
|
||||||
this._conn[nm] = relay
|
|
||||||
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,22 +159,36 @@ export class SimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Pub {
|
publish(relays: string[], event: Event): Pub {
|
||||||
const pubs: Pub[] = []
|
const pubPromises: Promise<Pub>[] = relays.map(async relay => {
|
||||||
relays.forEach(async relay => {
|
|
||||||
let r
|
let r
|
||||||
try {
|
try {
|
||||||
r = await this.ensureRelay(relay)
|
r = await this.ensureRelay(relay)
|
||||||
pubs.push(r.publish(event))
|
return r.publish(event)
|
||||||
} catch (_) {}
|
} catch (_) {
|
||||||
|
return {on() {}, off() {}}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const callbackMap = new Map()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
on(type, cb) {
|
on(type, cb) {
|
||||||
pubs.forEach((pub, i) => {
|
relays.forEach(async (relay, i) => {
|
||||||
pub.on(type, () => cb(relays[i]))
|
let pub = await pubPromises[i]
|
||||||
|
let callback = () => cb(relay)
|
||||||
|
callbackMap.set(cb, callback)
|
||||||
|
pub.on(type, callback)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
off() {
|
|
||||||
// do nothing here, FIXME
|
off(type, cb) {
|
||||||
|
relays.forEach(async (_, i) => {
|
||||||
|
let callback = callbackMap.get(cb)
|
||||||
|
if (callback) {
|
||||||
|
let pub = await pubPromises[i]
|
||||||
|
pub.off(type, callback)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ export function parseReferences(evt: Event): Reference[] {
|
|||||||
}
|
}
|
||||||
case 'a': {
|
case 'a': {
|
||||||
try {
|
try {
|
||||||
let [kind, pubkey, identifier] = ref[1].split(':')
|
let [kind, pubkey, identifier] = tag[1].split(':')
|
||||||
references.push({
|
references.push({
|
||||||
text: ref[0],
|
text: ref[0],
|
||||||
address: {
|
address: {
|
||||||
|
|||||||
8
relay.ts
8
relay.ts
@@ -83,8 +83,10 @@ export function relayInit(
|
|||||||
}
|
}
|
||||||
} = {}
|
} = {}
|
||||||
|
|
||||||
|
var connectionPromise: Promise<void> | undefined
|
||||||
async function connectRelay(): Promise<void> {
|
async function connectRelay(): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
if (connectionPromise) return connectionPromise
|
||||||
|
connectionPromise = new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
ws = new WebSocket(url)
|
ws = new WebSocket(url)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -96,10 +98,12 @@ export function relayInit(
|
|||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
ws.onerror = () => {
|
ws.onerror = () => {
|
||||||
|
connectionPromise = undefined
|
||||||
listeners.error.forEach(cb => cb())
|
listeners.error.forEach(cb => cb())
|
||||||
reject()
|
reject()
|
||||||
}
|
}
|
||||||
ws.onclose = async () => {
|
ws.onclose = async () => {
|
||||||
|
connectionPromise = undefined
|
||||||
listeners.disconnect.forEach(cb => cb())
|
listeners.disconnect.forEach(cb => cb())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,6 +189,8 @@ export function relayInit(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return connectionPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
function connected() {
|
function connected() {
|
||||||
|
|||||||
Reference in New Issue
Block a user