Compare commits

...

4 Commits

Author SHA1 Message Date
fiatjaf
01dd5b7a3c bring back @noble/secp256k1 along with micro-bip32. 2021-12-31 22:47:45 -03:00
fiatjaf
16536340e5 small fix on pool.removeRelay() 2021-12-31 22:25:33 -03:00
fiatjaf
1037eee335 trim relay url on normalize. 2021-12-31 22:03:02 -03:00
fiatjaf
5ce1b4c9f7 only initiate subscriptions for new relays added with read:true 2021-12-31 20:50:02 -03:00
6 changed files with 24 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
import {Buffer} from 'buffer'
import createHash from 'create-hash'
import {signSchnorr, verifySchnorr} from 'tiny-secp256k1'
import * as secp256k1 from '@noble/secp256k1'
export function getBlankEvent() {
return {
@@ -32,15 +32,9 @@ export function getEventHash(event) {
export function verifySignature(event) {
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
View File

@@ -1,19 +1,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)
}

View File

@@ -4,14 +4,11 @@ import {
mnemonicToSeedSync,
validateMnemonic
} from 'micro-bip39'
import BIP32Factory from 'bip32'
import * as ecc from 'tiny-secp256k1'
const bip32 = BIP32Factory(ecc)
import {HDKey} from 'micro-bip32'
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')
}
export function seedFromWords(mnemonic) {

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.13.0",
"version": "0.14.0",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
@@ -8,14 +8,13 @@
},
"dependencies": {
"@noble/secp256k1": "^1.3.0",
"bip32": "^3.0.1",
"browserify-cipher": ">=1",
"buffer": ">=5",
"create-hash": "^1.2.0",
"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"
},
"keywords": [

16
pool.js
View File

@@ -84,16 +84,20 @@ export function relayPool() {
})
relays[relayURL] = {relay, policy}
Object.values(activeSubscriptions).forEach(subscription =>
subscription.addRelay(relay)
)
if (policy.read) {
Object.values(activeSubscriptions).forEach(subscription =>
subscription.addRelay(relay)
)
}
return relay
},
removeRelay(url) {
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 =>
subscription.removeRelay(relay)
)
@@ -114,7 +118,7 @@ export function relayPool() {
event.tags = event.tags || []
if (globalPrivateKey) {
event.sig = signEvent(event, globalPrivateKey)
event.sig = await signEvent(event, globalPrivateKey)
} else {
throw new Error(
"can't publish unsigned event. either sign this event beforehand or pass a private key while initializing this relay pool so it can be signed automatically."

View File

@@ -6,7 +6,7 @@ import {verifySignature} from './event'
import {matchFilters} from './filter'
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)
if (host.slice(0, 2) !== 'ws') host = 'wss://' + host
if (host.length && host[host.length - 1] === '/') host = host.slice(0, -1)
@@ -46,7 +46,7 @@ export function relayConnect(url, onNotice = () => {}, onError = () => {}) {
}
}
}
ws.onerror = (err) => {
ws.onerror = err => {
console.log('error connecting to relay', url)
onError(err)
}