nostr-tools
Advanced tools
Comparing version 1.15.0 to 1.16.0
@@ -25,2 +25,3 @@ /** Designates a verified event signature. */ | ||
ClientAuth = 22242, | ||
NwcRequest = 23194, | ||
HttpAuth = 27235, | ||
@@ -27,0 +28,0 @@ ProfileBadge = 30008, |
@@ -22,2 +22,3 @@ export * from './keys.ts'; | ||
export * as nip44 from './nip44.ts'; | ||
export * as nip47 from './nip47.ts'; | ||
export * as nip57 from './nip57.ts'; | ||
@@ -24,0 +25,0 @@ export * as nip98 from './nip98.ts'; |
@@ -0,2 +1,10 @@ | ||
import { type UnsignedEvent, type Event } from './event.ts'; | ||
/** Get POW difficulty from a Nostr hex ID. */ | ||
export declare function getPow(hex: string): number; | ||
/** | ||
* Mine an event with the desired POW. This function mutates the event. | ||
* Note that this operation is synchronous and should be run in a worker context to avoid blocking the main thread. | ||
* | ||
* Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36 | ||
*/ | ||
export declare function minePow<K extends number>(unsigned: UnsignedEvent<K>, difficulty: number): Omit<Event<K>, 'sig'>; |
@@ -14,2 +14,3 @@ /** | ||
author?: string; | ||
kind?: number; | ||
}; | ||
@@ -16,0 +17,0 @@ export type AddressPointer = { |
@@ -51,2 +51,3 @@ "use strict"; | ||
nip44: () => nip44_exports, | ||
nip47: () => nip47_exports, | ||
nip57: () => nip57_exports, | ||
@@ -261,2 +262,3 @@ nip98: () => nip98_exports, | ||
Kind3[Kind3["ClientAuth"] = 22242] = "ClientAuth"; | ||
Kind3[Kind3["NwcRequest"] = 23194] = "NwcRequest"; | ||
Kind3[Kind3["HttpAuth"] = 27235] = "HttpAuth"; | ||
@@ -918,3 +920,3 @@ Kind3[Kind3["ProfileBadge"] = 30008] = "ProfileBadge"; | ||
}); | ||
const sub = this.sub(relays2, filters2); | ||
const sub = this.sub(relays2, [mergeFilters(...filters2)]); | ||
sub.on("event", (event) => { | ||
@@ -968,2 +970,10 @@ batchedRequests.forEach((br) => matchFilters(br.filters, event) && br.events.push(event)); | ||
var BECH32_REGEX = /[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/; | ||
function integerToUint8Array(number) { | ||
const uint8Array = new Uint8Array(4); | ||
uint8Array[0] = number >> 24 & 255; | ||
uint8Array[1] = number >> 16 & 255; | ||
uint8Array[2] = number >> 8 & 255; | ||
uint8Array[3] = number & 255; | ||
return uint8Array; | ||
} | ||
function decode(nip19) { | ||
@@ -995,2 +1005,4 @@ let { prefix, words } = import_base.bech32.decode(nip19, Bech32MaxSize); | ||
throw new Error("TLV 2 should be 32 bytes"); | ||
if (tlv[3] && tlv[3][0].length !== 4) | ||
throw new Error("TLV 3 should be 4 bytes"); | ||
return { | ||
@@ -1001,3 +1013,4 @@ type: "nevent", | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [], | ||
author: tlv[2]?.[0] ? (0, import_utils6.bytesToHex)(tlv[2][0]) : void 0 | ||
author: tlv[2]?.[0] ? (0, import_utils6.bytesToHex)(tlv[2][0]) : void 0, | ||
kind: tlv[3]?.[0] ? parseInt((0, import_utils6.bytesToHex)(tlv[3][0]), 16) : void 0 | ||
} | ||
@@ -1087,6 +1100,11 @@ }; | ||
function neventEncode(event) { | ||
let kindArray; | ||
if (event.kind != void 0) { | ||
kindArray = integerToUint8Array(event.kind); | ||
} | ||
let data = encodeTLV({ | ||
0: [(0, import_utils6.hexToBytes)(event.id)], | ||
1: (event.relays || []).map((url) => utf8Encoder.encode(url)), | ||
2: event.author ? [(0, import_utils6.hexToBytes)(event.author)] : [] | ||
2: event.author ? [(0, import_utils6.hexToBytes)(event.author)] : [], | ||
3: kindArray ? [new Uint8Array(kindArray)] : [] | ||
}); | ||
@@ -1398,3 +1416,4 @@ return encodeBech32("nevent", data); | ||
__export(nip13_exports, { | ||
getPow: () => getPow | ||
getPow: () => getPow, | ||
minePow: () => minePow | ||
}); | ||
@@ -1414,2 +1433,21 @@ function getPow(hex) { | ||
} | ||
function minePow(unsigned, difficulty) { | ||
let count = 0; | ||
const event = unsigned; | ||
const tag = ["nonce", count.toString(), difficulty.toString()]; | ||
event.tags.push(tag); | ||
while (true) { | ||
const now = Math.floor(new Date().getTime() / 1e3); | ||
if (now !== event.created_at) { | ||
count = 0; | ||
event.created_at = now; | ||
} | ||
tag[1] = (++count).toString(); | ||
event.id = getEventHash(event); | ||
if (getPow(event.id) >= difficulty) { | ||
break; | ||
} | ||
} | ||
return event; | ||
} | ||
@@ -1816,2 +1854,39 @@ // nip18.ts | ||
// nip47.ts | ||
var nip47_exports = {}; | ||
__export(nip47_exports, { | ||
makeNwcRequestEvent: () => makeNwcRequestEvent, | ||
parseConnectionString: () => parseConnectionString | ||
}); | ||
function parseConnectionString(connectionString) { | ||
const { pathname, searchParams } = new URL(connectionString); | ||
const pubkey = pathname; | ||
const relay = searchParams.get("relay"); | ||
const secret = searchParams.get("secret"); | ||
if (!pubkey || !relay || !secret) { | ||
throw new Error("invalid connection string"); | ||
} | ||
return { pubkey, relay, secret }; | ||
} | ||
async function makeNwcRequestEvent({ | ||
pubkey, | ||
secret, | ||
invoice | ||
}) { | ||
const content = { | ||
method: "pay_invoice", | ||
params: { | ||
invoice | ||
} | ||
}; | ||
const encryptedContent = await encrypt(secret, pubkey, JSON.stringify(content)); | ||
const eventTemplate = { | ||
kind: 23194 /* NwcRequest */, | ||
created_at: Math.round(Date.now() / 1e3), | ||
content: encryptedContent, | ||
tags: [["p", pubkey]] | ||
}; | ||
return finishEvent(eventTemplate, secret); | ||
} | ||
// nip57.ts | ||
@@ -1818,0 +1893,0 @@ var nip57_exports = {}; |
{ | ||
"name": "nostr-tools", | ||
"version": "1.15.0", | ||
"version": "1.16.0", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -93,3 +93,2 @@ # nostr-tools | ||
kind: 1, | ||
pubkey: pk, | ||
created_at: Math.floor(Date.now() / 1000), | ||
@@ -100,3 +99,3 @@ tags: [], | ||
// this calculates the event id and signs the event in a single step | ||
// this assigns the pubkey, calculates the event id and signs the event in a single step | ||
const signedEvent = finishEvent(event, sk) | ||
@@ -150,2 +149,7 @@ await relay.publish(signedEvent) | ||
let batchedEvents = await pool.batchedList('notes', relays, [{ kinds: [1] }]) | ||
// `batchedList` will wait for other function calls with the same `batchKey` | ||
// (e.g. 'notes', 'authors', etc) within a fixed amount of time (default: `100ms`) before sending | ||
// next ws request, and batch all requests with similar `batchKey`s together in a single request. | ||
let relaysForEvent = pool.seenOn('44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245') | ||
@@ -157,2 +161,4 @@ // relaysForEvent will be an array of URLs from relays a given event was seen on | ||
read more details about `batchedList` on this pr: [https://github.com/nbd-wtf/nostr-tools/pull/279](https://github.com/nbd-wtf/nostr-tools/pull/279#issue-1859315757) | ||
### Parsing references (mentions) from a content using NIP-10 and NIP-27 | ||
@@ -159,0 +165,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
997409
66
12348
313