bring back @noble/secp256k1 along with micro-bip32.

This commit is contained in:
fiatjaf
2021-12-31 22:47:43 -03:00
parent 16536340e5
commit 01dd5b7a3c
5 changed files with 13 additions and 33 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": [

View File

@@ -118,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."