nostr-tools
Advanced tools
Comparing version 0.13.0 to 0.14.0
14
event.js
import {Buffer} from 'buffer' | ||
import createHash from 'create-hash' | ||
import {signSchnorr, verifySchnorr} from 'tiny-secp256k1' | ||
import * as secp256k1 from '@noble/secp256k1' | ||
@@ -35,13 +35,7 @@ export function getBlankEvent() { | ||
if (event.id !== getEventHash(event)) return false | ||
return verifySchnorr( | ||
Buffer.from(event.id, 'hex'), | ||
Buffer.from(event.pubkey, 'hex'), | ||
Buffer.from(event.sig, 'hex') | ||
) | ||
return secp256k1.schnorr.verify(event.id, event.pubkey, event.sig) | ||
} | ||
export function signEvent(event, key) { | ||
let eventHash = Buffer.from(getEventHash(event), 'hex') | ||
let keyB = Buffer.from(key, 'hex') | ||
return Buffer.from(signSchnorr(eventHash, keyB)).toString('hex') | ||
export async function signEvent(event, key) { | ||
return secp256k1.schnorr.sign(getEventHash(event), key) | ||
} |
16
keys.js
@@ -1,19 +0,9 @@ | ||
import randomBytes from 'randombytes' | ||
import {isPrivate, pointFromScalar} from 'tiny-secp256k1' | ||
import * as secp256k1 from '@noble/secp256k1' | ||
export function generatePrivateKey() { | ||
let i = 8 | ||
while (i--) { | ||
let r32 = Buffer.from(randomBytes(32)) | ||
if (isPrivate(r32)) return r32.toString('hex') | ||
} | ||
throw new Error( | ||
'Valid private key was not found in 8 iterations. PRNG is broken' | ||
) | ||
return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex') | ||
} | ||
export function getPublicKey(privateKey) { | ||
return Buffer.from(pointFromScalar(Buffer.from(privateKey, 'hex'), true)) | ||
.toString('hex') | ||
.slice(2) | ||
return secp256k1.getPublicKey(privateKey) | ||
} |
@@ -7,10 +7,7 @@ import {wordlist} from 'micro-bip39/wordlists/english' | ||
} from 'micro-bip39' | ||
import BIP32Factory from 'bip32' | ||
import * as ecc from 'tiny-secp256k1' | ||
import {HDKey} from 'micro-bip32' | ||
const bip32 = BIP32Factory(ecc) | ||
export function privateKeyFromSeed(seed) { | ||
let root = bip32.fromSeed(Buffer.from(seed, 'hex')) | ||
return root.derivePath(`m/44'/1237'/0'/0'`).privateKey.toString('hex') | ||
let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex')) | ||
return root.derive(`m/44'/1237'/0'/0'`).privateKey.toString('hex') | ||
} | ||
@@ -17,0 +14,0 @@ |
{ | ||
"name": "nostr-tools", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -11,3 +11,2 @@ "repository": { | ||
"@noble/secp256k1": "^1.3.0", | ||
"bip32": "^3.0.1", | ||
"browserify-cipher": ">=1", | ||
@@ -17,5 +16,5 @@ "buffer": ">=5", | ||
"dns-packet": "^5.2.4", | ||
"micro-bip32": "^0.1.0", | ||
"micro-bip39": "^0.1.3", | ||
"randombytes": ">=2", | ||
"tiny-secp256k1": "^2.1.2", | ||
"websocket-polyfill": "^0.0.3" | ||
@@ -22,0 +21,0 @@ }, |
16
pool.js
@@ -87,5 +87,7 @@ import {getEventHash, signEvent} from './event' | ||
Object.values(activeSubscriptions).forEach(subscription => | ||
subscription.addRelay(relay) | ||
) | ||
if (policy.read) { | ||
Object.values(activeSubscriptions).forEach(subscription => | ||
subscription.addRelay(relay) | ||
) | ||
} | ||
@@ -96,4 +98,6 @@ return relay | ||
let relayURL = normalizeRelayURL(url) | ||
let {relay} = relays[relayURL] | ||
if (!relay) return | ||
let data = relays[relayURL] | ||
if (!data) return | ||
let {relay} = data | ||
Object.values(activeSubscriptions).forEach(subscription => | ||
@@ -119,3 +123,3 @@ subscription.removeRelay(relay) | ||
if (globalPrivateKey) { | ||
event.sig = signEvent(event, globalPrivateKey) | ||
event.sig = await signEvent(event, globalPrivateKey) | ||
} else { | ||
@@ -122,0 +126,0 @@ throw new Error( |
@@ -9,3 +9,3 @@ /* global WebSocket */ | ||
export function normalizeRelayURL(url) { | ||
let [host, ...qs] = url.split('?') | ||
let [host, ...qs] = url.trim().split('?') | ||
if (host.slice(0, 4) === 'http') host = 'ws' + host.slice(4) | ||
@@ -50,3 +50,3 @@ if (host.slice(0, 2) !== 'ws') host = 'wss://' + host | ||
} | ||
ws.onerror = (err) => { | ||
ws.onerror = err => { | ||
console.log('error connecting to relay', url) | ||
@@ -53,0 +53,0 @@ onError(err) |
9
21106
624
+ Addedmicro-bip32@^0.1.0
+ Addedmicro-bip32@0.1.0(transitive)
- Removedbip32@^3.0.1
- Removedtiny-secp256k1@^2.1.2
- Removedbase-x@3.0.10(transitive)
- Removedbip32@3.1.0(transitive)
- Removedbs58@4.0.1(transitive)
- Removedbs58check@2.1.2(transitive)
- Removedcreate-hmac@1.1.7(transitive)
- Removedtiny-secp256k1@2.2.3(transitive)
- Removedtypeforce@1.18.0(transitive)
- Removeduint8array-tools@0.0.7(transitive)
- Removedwif@2.0.6(transitive)