nostr-tools
Advanced tools
Comparing version 1.9.0 to 1.10.0
@@ -15,4 +15,5 @@ export * from './keys'; | ||
export * as nip39 from './nip39'; | ||
export * as nip42 from './nip42'; | ||
export * as nip57 from './nip57'; | ||
export * as fj from './fakejson'; | ||
export * as utils from './utils'; |
@@ -16,6 +16,25 @@ export type ProfilePointer = { | ||
}; | ||
export declare function decode(nip19: string): { | ||
type: string; | ||
data: ProfilePointer | EventPointer | AddressPointer | string; | ||
export type DecodeResult = { | ||
type: 'nprofile'; | ||
data: ProfilePointer; | ||
} | { | ||
type: 'nrelay'; | ||
data: string; | ||
} | { | ||
type: 'nevent'; | ||
data: EventPointer; | ||
} | { | ||
type: 'naddr'; | ||
data: AddressPointer; | ||
} | { | ||
type: 'nsec'; | ||
data: string; | ||
} | { | ||
type: 'npub'; | ||
data: string; | ||
} | { | ||
type: 'note'; | ||
data: string; | ||
}; | ||
export declare function decode(nip19: string): DecodeResult; | ||
export declare function nsecEncode(hex: string): string; | ||
@@ -22,0 +41,0 @@ export declare function npubEncode(hex: string): string; |
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
@@ -18,2 +20,6 @@ var __export = (target, all) => { | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
@@ -42,2 +48,3 @@ | ||
nip39: () => nip39_exports, | ||
nip42: () => nip42_exports, | ||
nip57: () => nip57_exports, | ||
@@ -55,15 +62,13 @@ parseReferences: () => parseReferences, | ||
// keys.ts | ||
var import_secp256k1 = require("@noble/curves/secp256k1"); | ||
var import_utils = require("@noble/hashes/utils"); | ||
var secp256k1 = __toESM(require("@noble/secp256k1")); | ||
function generatePrivateKey() { | ||
return (0, import_utils.bytesToHex)(import_secp256k1.schnorr.utils.randomPrivateKey()); | ||
return secp256k1.utils.bytesToHex(secp256k1.utils.randomPrivateKey()); | ||
} | ||
function getPublicKey(privateKey) { | ||
return (0, import_utils.bytesToHex)(import_secp256k1.schnorr.getPublicKey(privateKey)); | ||
return secp256k1.utils.bytesToHex(secp256k1.schnorr.getPublicKey(privateKey)); | ||
} | ||
// event.ts | ||
var import_secp256k12 = require("@noble/curves/secp256k1"); | ||
var secp256k12 = __toESM(require("@noble/secp256k1")); | ||
var import_sha256 = require("@noble/hashes/sha256"); | ||
var import_utils2 = require("@noble/hashes/utils"); | ||
@@ -219,3 +224,3 @@ // utils.ts | ||
let eventHash = (0, import_sha256.sha256)(utf8Encoder.encode(serializeEvent(event))); | ||
return (0, import_utils2.bytesToHex)(eventHash); | ||
return secp256k12.utils.bytesToHex(eventHash); | ||
} | ||
@@ -250,3 +255,3 @@ var isRecord = (obj) => obj instanceof Object; | ||
function verifySignature(event) { | ||
return import_secp256k12.schnorr.verify( | ||
return secp256k12.schnorr.verifySync( | ||
event.sig, | ||
@@ -258,4 +263,4 @@ getEventHash(event), | ||
function signEvent(event, key) { | ||
return (0, import_utils2.bytesToHex)( | ||
import_secp256k12.schnorr.sign(getEventHash(event), key) | ||
return secp256k12.utils.bytesToHex( | ||
secp256k12.schnorr.signSync(getEventHash(event), key) | ||
); | ||
@@ -350,12 +355,14 @@ } | ||
// relay.ts | ||
var newListeners = () => ({ | ||
connect: [], | ||
disconnect: [], | ||
error: [], | ||
notice: [], | ||
auth: [] | ||
}); | ||
function relayInit(url, options = {}) { | ||
let { listTimeout = 3e3, getTimeout = 3e3 } = options; | ||
let { listTimeout = 3e3, getTimeout = 3e3, countTimeout = 3e3 } = options; | ||
var ws; | ||
var openSubs = {}; | ||
var listeners = { | ||
connect: [], | ||
disconnect: [], | ||
error: [], | ||
notice: [] | ||
}; | ||
var listeners = newListeners(); | ||
var subListeners = {}; | ||
@@ -413,10 +420,19 @@ var pubListeners = {}; | ||
switch (data[0]) { | ||
case "EVENT": | ||
let id = data[1]; | ||
case "EVENT": { | ||
let id2 = data[1]; | ||
let event = data[2]; | ||
if (validateEvent(event) && openSubs[id] && (openSubs[id].skipVerification || verifySignature(event)) && matchFilters(openSubs[id].filters, event)) { | ||
openSubs[id]; | ||
(subListeners[id]?.event || []).forEach((cb) => cb(event)); | ||
if (validateEvent(event) && openSubs[id2] && (openSubs[id2].skipVerification || verifySignature(event)) && matchFilters(openSubs[id2].filters, event)) { | ||
openSubs[id2]; | ||
(subListeners[id2]?.event || []).forEach((cb) => cb(event)); | ||
} | ||
return; | ||
} | ||
case "COUNT": | ||
let id = data[1]; | ||
let payload = data[2]; | ||
if (openSubs[id]) { | ||
; | ||
(subListeners[id]?.count || []).forEach((cb) => cb(payload)); | ||
} | ||
return; | ||
case "EOSE": { | ||
@@ -448,2 +464,7 @@ let id2 = data[1]; | ||
return; | ||
case "AUTH": { | ||
let challenge = data[1]; | ||
listeners.auth?.forEach((cb) => cb(challenge)); | ||
return; | ||
} | ||
} | ||
@@ -480,2 +501,3 @@ } catch (err) { | ||
const sub = (filters, { | ||
verb = "REQ", | ||
skipVerification = false, | ||
@@ -492,3 +514,3 @@ alreadyHaveEvent = null, | ||
}; | ||
trySend(["REQ", subid, ...filters]); | ||
trySend([verb, subid, ...filters]); | ||
return { | ||
@@ -508,2 +530,3 @@ sub: (newFilters, newOpts = {}) => sub(newFilters || filters, { | ||
event: [], | ||
count: [], | ||
eose: [] | ||
@@ -521,2 +544,25 @@ }; | ||
}; | ||
function _publishEvent(event, type) { | ||
if (!event.id) | ||
throw new Error(`event ${event} has no id`); | ||
let id = event.id; | ||
trySend([type, event]); | ||
return { | ||
on: (type2, cb) => { | ||
pubListeners[id] = pubListeners[id] || { | ||
ok: [], | ||
failed: [] | ||
}; | ||
pubListeners[id][type2].push(cb); | ||
}, | ||
off: (type2, cb) => { | ||
let listeners2 = pubListeners[id]; | ||
if (!listeners2) | ||
return; | ||
let idx = listeners2[type2].indexOf(cb); | ||
if (idx >= 0) | ||
listeners2[type2].splice(idx, 1); | ||
} | ||
}; | ||
} | ||
return { | ||
@@ -565,28 +611,23 @@ url, | ||
}), | ||
count: (filters) => new Promise((resolve) => { | ||
let s = sub(filters, { ...sub, verb: "COUNT" }); | ||
let timeout = setTimeout(() => { | ||
s.unsub(); | ||
resolve(null); | ||
}, countTimeout); | ||
s.on("count", (event) => { | ||
s.unsub(); | ||
clearTimeout(timeout); | ||
resolve(event); | ||
}); | ||
}), | ||
publish(event) { | ||
if (!event.id) | ||
throw new Error(`event ${event} has no id`); | ||
let id = event.id; | ||
trySend(["EVENT", event]); | ||
return { | ||
on: (type, cb) => { | ||
pubListeners[id] = pubListeners[id] || { | ||
ok: [], | ||
failed: [] | ||
}; | ||
pubListeners[id][type].push(cb); | ||
}, | ||
off: (type, cb) => { | ||
let listeners2 = pubListeners[id]; | ||
if (!listeners2) | ||
return; | ||
let idx = listeners2[type].indexOf(cb); | ||
if (idx >= 0) | ||
listeners2[type].splice(idx, 1); | ||
} | ||
}; | ||
return _publishEvent(event, "EVENT"); | ||
}, | ||
auth(event) { | ||
return _publishEvent(event, "AUTH"); | ||
}, | ||
connect, | ||
close() { | ||
listeners = { connect: [], disconnect: [], error: [], notice: [] }; | ||
listeners = newListeners(); | ||
subListeners = {}; | ||
@@ -788,3 +829,3 @@ pubListeners = {}; | ||
}); | ||
var import_utils5 = require("@noble/hashes/utils"); | ||
var secp256k13 = __toESM(require("@noble/secp256k1")); | ||
var import_base = require("@scure/base"); | ||
@@ -805,3 +846,3 @@ var Bech32MaxSize = 5e3; | ||
data: { | ||
pubkey: (0, import_utils5.bytesToHex)(tlv[0][0]), | ||
pubkey: secp256k13.utils.bytesToHex(tlv[0][0]), | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [] | ||
@@ -822,5 +863,5 @@ } | ||
data: { | ||
id: (0, import_utils5.bytesToHex)(tlv[0][0]), | ||
id: secp256k13.utils.bytesToHex(tlv[0][0]), | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [], | ||
author: tlv[2]?.[0] ? (0, import_utils5.bytesToHex)(tlv[2][0]) : void 0 | ||
author: tlv[2]?.[0] ? secp256k13.utils.bytesToHex(tlv[2][0]) : void 0 | ||
} | ||
@@ -845,4 +886,4 @@ }; | ||
identifier: utf8Decoder.decode(tlv[0][0]), | ||
pubkey: (0, import_utils5.bytesToHex)(tlv[2][0]), | ||
kind: parseInt((0, import_utils5.bytesToHex)(tlv[3][0]), 16), | ||
pubkey: secp256k13.utils.bytesToHex(tlv[2][0]), | ||
kind: parseInt(secp256k13.utils.bytesToHex(tlv[3][0]), 16), | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [] | ||
@@ -864,3 +905,3 @@ } | ||
case "note": | ||
return { type: prefix, data: (0, import_utils5.bytesToHex)(data) }; | ||
return { type: prefix, data: secp256k13.utils.bytesToHex(data) }; | ||
default: | ||
@@ -895,3 +936,3 @@ throw new Error(`unknown prefix ${prefix}`); | ||
function encodeBytes(prefix, hex) { | ||
let data = (0, import_utils5.hexToBytes)(hex); | ||
let data = secp256k13.utils.hexToBytes(hex); | ||
let words = import_base.bech32.toWords(data); | ||
@@ -902,3 +943,3 @@ return import_base.bech32.encode(prefix, words, Bech32MaxSize); | ||
let data = encodeTLV({ | ||
0: [(0, import_utils5.hexToBytes)(profile.pubkey)], | ||
0: [secp256k13.utils.hexToBytes(profile.pubkey)], | ||
1: (profile.relays || []).map((url) => utf8Encoder.encode(url)) | ||
@@ -911,5 +952,5 @@ }); | ||
let data = encodeTLV({ | ||
0: [(0, import_utils5.hexToBytes)(event.id)], | ||
0: [secp256k13.utils.hexToBytes(event.id)], | ||
1: (event.relays || []).map((url) => utf8Encoder.encode(url)), | ||
2: event.author ? [(0, import_utils5.hexToBytes)(event.author)] : [] | ||
2: event.author ? [secp256k13.utils.hexToBytes(event.author)] : [] | ||
}); | ||
@@ -925,3 +966,3 @@ let words = import_base.bech32.toWords(data); | ||
1: (addr.relays || []).map((url) => utf8Encoder.encode(url)), | ||
2: [(0, import_utils5.hexToBytes)(addr.pubkey)], | ||
2: [secp256k13.utils.hexToBytes(addr.pubkey)], | ||
3: [new Uint8Array(kind)] | ||
@@ -950,3 +991,3 @@ }); | ||
}); | ||
return (0, import_utils5.concatBytes)(...entries); | ||
return secp256k13.utils.concatBytes(...entries); | ||
} | ||
@@ -1049,9 +1090,9 @@ | ||
}); | ||
var import_utils7 = require("@noble/hashes/utils"); | ||
var import_secp256k13 = require("@noble/curves/secp256k1"); | ||
var import_utils4 = require("@noble/hashes/utils"); | ||
var secp256k14 = __toESM(require("@noble/secp256k1")); | ||
var import_base2 = require("@scure/base"); | ||
async function encrypt(privkey, pubkey, text) { | ||
const key = import_secp256k13.secp256k1.getSharedSecret(privkey, "02" + pubkey); | ||
const key = secp256k14.getSharedSecret(privkey, "02" + pubkey); | ||
const normalizedKey = getNormalizedX(key); | ||
let iv = Uint8Array.from((0, import_utils7.randomBytes)(16)); | ||
let iv = Uint8Array.from((0, import_utils4.randomBytes)(16)); | ||
let plaintext = utf8Encoder.encode(text); | ||
@@ -1076,3 +1117,3 @@ let cryptoKey = await crypto.subtle.importKey( | ||
let [ctb64, ivb64] = data.split("?iv="); | ||
let key = import_secp256k13.secp256k1.getSharedSecret(privkey, "02" + pubkey); | ||
let key = secp256k14.getSharedSecret(privkey, "02" + pubkey); | ||
let normalizedKey = getNormalizedX(key); | ||
@@ -1156,3 +1197,3 @@ let cryptoKey = await crypto.subtle.importKey( | ||
}); | ||
var import_utils9 = require("@noble/hashes/utils"); | ||
var secp256k15 = __toESM(require("@noble/secp256k1")); | ||
var import_english = require("@scure/bip39/wordlists/english.js"); | ||
@@ -1166,3 +1207,3 @@ var import_bip39 = require("@scure/bip39"); | ||
throw new Error("could not derive private key"); | ||
return (0, import_utils9.bytesToHex)(privateKey); | ||
return secp256k15.utils.bytesToHex(privateKey); | ||
} | ||
@@ -1239,5 +1280,5 @@ function generateSeedWords() { | ||
}); | ||
var import_utils10 = require("@noble/hashes/utils"); | ||
var secp256k16 = __toESM(require("@noble/secp256k1")); | ||
function getPow(id) { | ||
return getLeadingZeroBits((0, import_utils10.hexToBytes)(id)); | ||
return getLeadingZeroBits(secp256k16.utils.hexToBytes(id)); | ||
} | ||
@@ -1272,4 +1313,3 @@ function getLeadingZeroBits(hash) { | ||
}); | ||
var import_secp256k14 = require("@noble/curves/secp256k1"); | ||
var import_utils11 = require("@noble/hashes/utils"); | ||
var secp256k17 = __toESM(require("@noble/secp256k1")); | ||
var import_sha2562 = require("@noble/hashes/sha256"); | ||
@@ -1290,4 +1330,4 @@ function createDelegation(privateKey, parameters) { | ||
); | ||
let sig = (0, import_utils11.bytesToHex)( | ||
import_secp256k14.schnorr.sign(sighash, privateKey) | ||
let sig = secp256k17.utils.bytesToHex( | ||
secp256k17.schnorr.signSync(sighash, privateKey) | ||
); | ||
@@ -1323,3 +1363,3 @@ return { | ||
); | ||
if (!import_secp256k14.schnorr.verify(sig, sighash, pubkey)) | ||
if (!secp256k17.schnorr.verifySync(sig, sighash, pubkey)) | ||
return null; | ||
@@ -1352,2 +1392,34 @@ return pubkey; | ||
// nip42.ts | ||
var nip42_exports = {}; | ||
__export(nip42_exports, { | ||
authenticate: () => authenticate | ||
}); | ||
var authenticate = async ({ | ||
challenge, | ||
relay, | ||
sign | ||
}) => { | ||
const e = { | ||
kind: 22242 /* ClientAuth */, | ||
created_at: Math.floor(Date.now() / 1e3), | ||
tags: [ | ||
["relay", relay.url], | ||
["challenge", challenge] | ||
], | ||
content: "" | ||
}; | ||
const pub = relay.auth(await sign(e)); | ||
return new Promise((resolve, reject) => { | ||
pub.on("ok", function ok() { | ||
pub.off("ok", ok); | ||
resolve(); | ||
}); | ||
pub.on("failed", function fail(reason) { | ||
pub.off("failed", fail); | ||
reject(reason); | ||
}); | ||
}); | ||
}; | ||
// nip57.ts | ||
@@ -1469,1 +1541,8 @@ var nip57_exports = {}; | ||
} | ||
// index.ts | ||
var secp256k18 = __toESM(require("@noble/secp256k1")); | ||
var import_hmac = require("@noble/hashes/hmac"); | ||
var import_sha2563 = require("@noble/hashes/sha256"); | ||
secp256k18.utils.hmacSha256Sync = (key, ...msgs) => (0, import_hmac.hmac)(import_sha2563.sha256, key, secp256k18.utils.concatBytes(...msgs)); | ||
secp256k18.utils.sha256Sync = (...msgs) => (0, import_sha2563.sha256)(secp256k18.utils.concatBytes(...msgs)); |
@@ -8,5 +8,10 @@ import { Event } from './event'; | ||
notice: (msg: string) => void | Promise<void>; | ||
auth: (challenge: string) => void | Promise<void>; | ||
}; | ||
export type CountPayload = { | ||
count: number; | ||
}; | ||
type SubEvent = { | ||
event: (event: Event) => void | Promise<void>; | ||
count: (payload: CountPayload) => void | Promise<void>; | ||
eose: () => void | Promise<void>; | ||
@@ -22,3 +27,5 @@ }; | ||
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>; | ||
count: (filters: Filter[], opts?: SubscriptionOptions) => Promise<CountPayload | null>; | ||
publish: (event: Event) => Pub; | ||
auth: (event: Event) => Pub; | ||
off: <T extends keyof RelayEvent, U extends RelayEvent[T]>(event: T, listener: U) => void; | ||
@@ -39,2 +46,3 @@ on: <T extends keyof RelayEvent, U extends RelayEvent[T]>(event: T, listener: U) => void; | ||
id?: string; | ||
verb?: 'REQ' | 'COUNT'; | ||
skipVerification?: boolean; | ||
@@ -46,3 +54,4 @@ alreadyHaveEvent?: null | ((id: string, relay: string) => boolean); | ||
listTimeout?: number; | ||
countTimeout?: number; | ||
}): Relay; | ||
export {}; |
{ | ||
"name": "nostr-tools", | ||
"version": "1.9.0", | ||
"version": "1.10.0", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -21,7 +21,7 @@ "repository": { | ||
"dependencies": { | ||
"@noble/curves": "1.0.0", | ||
"@noble/hashes": "1.3.0", | ||
"@noble/hashes": "1.2.0", | ||
"@noble/secp256k1": "1.7.1", | ||
"@scure/base": "1.1.1", | ||
"@scure/bip32": "1.3.0", | ||
"@scure/bip39": "1.2.0" | ||
"@scure/bip32": "1.1.4", | ||
"@scure/bip39": "1.1.1" | ||
}, | ||
@@ -28,0 +28,0 @@ "keywords": [ |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
28
0
852332
11031
+ Added@noble/secp256k1@1.7.1
+ Added@noble/hashes@1.2.0(transitive)
+ Added@noble/secp256k1@1.7.1(transitive)
+ Added@scure/bip32@1.1.4(transitive)
+ Added@scure/bip39@1.1.1(transitive)
- Removed@noble/curves@1.0.0
- Removed@noble/curves@1.0.0(transitive)
- Removed@noble/hashes@1.3.0(transitive)
- Removed@scure/bip32@1.3.0(transitive)
- Removed@scure/bip39@1.2.0(transitive)
Updated@noble/hashes@1.2.0
Updated@scure/bip32@1.1.4
Updated@scure/bip39@1.1.1