mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 17:18:51 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6f5ee8223 | ||
|
|
a05506468d | ||
|
|
674ff66b6f | ||
|
|
731705047a | ||
|
|
94b382a49f |
7
event.ts
7
event.ts
@@ -13,6 +13,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 +24,8 @@ export enum Kind {
|
|||||||
Zap = 9735,
|
Zap = 9735,
|
||||||
RelayList = 10002,
|
RelayList = 10002,
|
||||||
ClientAuth = 22242,
|
ClientAuth = 22242,
|
||||||
|
BadgeDefinition = 30008,
|
||||||
|
ProfileBadge = 30009,
|
||||||
Article = 30023
|
Article = 30023
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +81,10 @@ export function getEventHash(event: UnsignedEvent): string {
|
|||||||
return secp256k1.utils.bytesToHex(eventHash)
|
return secp256k1.utils.bytesToHex(eventHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isRecord = (obj: unknown): obj is Record<string, unknown> => obj instanceof Object
|
||||||
|
|
||||||
export function validateEvent<T>(event: T): event is T & UnsignedEvent {
|
export function validateEvent<T>(event: T): event is T & UnsignedEvent {
|
||||||
if (typeof event !== 'object') return false
|
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
|
||||||
|
|||||||
1
index.ts
1
index.ts
@@ -9,6 +9,7 @@ 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 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'
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
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 * as secp256k1 from '@noble/secp256k1'
|
||||||
|
|
||||||
|
/** Get POW difficulty from a Nostr hex ID. */
|
||||||
|
export function getPow(id: string): number {
|
||||||
|
return getLeadingZeroBits(secp256k1.utils.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
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.8.3",
|
"version": "1.8.4",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user