nostr-tools
Advanced tools
Comparing version 0.11.0 to 0.12.0
26
event.js
import {Buffer} from 'buffer' | ||
import * as secp256k1 from '@noble/secp256k1' | ||
import createHash from 'create-hash' | ||
import {signSchnorr, verifySchnorr} from 'tiny-secp256k1' | ||
import {sha256} from './utils' | ||
export function getBlankEvent() { | ||
@@ -28,17 +27,20 @@ return { | ||
export async function getEventHash(event) { | ||
let eventHash = await sha256(Buffer.from(serializeEvent(event))) | ||
let eventHash = createHash('sha256') | ||
.update(Buffer.from(serializeEvent(event))) | ||
.digest() | ||
return Buffer.from(eventHash).toString('hex') | ||
} | ||
export async function verifySignature(event) { | ||
return await secp256k1.schnorr.verify( | ||
event.sig, | ||
await getEventHash(event), | ||
event.pubkey | ||
export function verifySignature(event) { | ||
if (event.id !== getEventHash(event)) return false | ||
return verifySchnorr( | ||
Buffer.from(event.sig, 'hex'), | ||
Buffer.from(event.id, 'hex'), | ||
Buffer.from(event.pubkey, 'hex') | ||
) | ||
} | ||
export async function signEvent(event, key) { | ||
let eventHash = await getEventHash(event) | ||
return await secp256k1.schnorr.sign(eventHash, key) | ||
export function signEvent(event, key) { | ||
let eventHash = getEventHash(event) | ||
return signSchnorr(Buffer.from(eventHash, 'hex'), Buffer.from(key, 'hex')) | ||
} |
@@ -0,1 +1,2 @@ | ||
import {generatePrivateKey, getPublicKey} from './keys' | ||
import {relayConnect} from './relay' | ||
@@ -11,3 +12,2 @@ import {relayPool} from './pool' | ||
import {matchFilter, matchFilters} from './filter' | ||
import {makeRandom32, sha256, getPublicKey} from './utils' | ||
@@ -21,4 +21,2 @@ export { | ||
getEventHash, | ||
makeRandom32, | ||
sha256, | ||
getPublicKey, | ||
@@ -25,0 +23,0 @@ getBlankEvent, |
25
nip06.js
import createHmac from 'create-hmac' | ||
import randomBytes from 'randombytes' | ||
import * as bip39 from 'bip39' | ||
import {wordlist} from 'micro-bip39/wordlists/english' | ||
import { | ||
generateMnemonic, | ||
mnemonicToSeedSync, | ||
validateMnemonic | ||
} from 'micro-bip39' | ||
import BIP32Factory from 'bip32' | ||
import * as ecc from 'tiny-secp256k1' | ||
const bip32 = BIP32Factory(ecc) | ||
export function privateKeyFromSeed(seed) { | ||
let hmac = createHmac('sha512', Buffer.from('Nostr seed', 'utf8')) | ||
hmac.update(seed) | ||
return hmac.digest().slice(0, 32).toString('hex') | ||
let root = bip32.fromSeed(seed) | ||
return root.derivePath(`m/44'/1237'/0'/0'`).privateKey.toString('hex') | ||
} | ||
export function seedFromWords(mnemonic) { | ||
return bip39.mnemonicToSeedSync(mnemonic) | ||
return mnemonicToSeedSync(mnemonic, wordlist) | ||
} | ||
export function generateSeedWords() { | ||
return bip39.entropyToMnemonic(randomBytes(16).toString('hex')) | ||
return generateMnemonic(wordlist) | ||
} | ||
export function validateWords(words) { | ||
return validateMnemonic(words, wordlist) | ||
} |
{ | ||
"name": "nostr-tools", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -11,8 +11,11 @@ "repository": { | ||
"@noble/secp256k1": "^1.3.0", | ||
"bip39": "^3.0.4", | ||
"bip32": "^3.0.1", | ||
"browserify-cipher": ">=1", | ||
"buffer": ">=5", | ||
"create-hash": "^1.2.0", | ||
"create-hmac": ">=1", | ||
"dns-packet": "^5.2.4", | ||
"micro-bip39": "^0.1.3", | ||
"randombytes": ">=2", | ||
"tiny-secp256k1": "^2.1.2", | ||
"websocket-polyfill": "^0.0.3" | ||
@@ -19,0 +22,0 @@ }, |
@@ -98,2 +98,4 @@ import {getEventHash, signEvent} from './event' | ||
async publish(event, statusCallback = (status, relayURL) => {}) { | ||
event.id = await getEventHash(event) | ||
if (!event.sig) { | ||
@@ -103,3 +105,2 @@ event.tags = event.tags || [] | ||
if (globalPrivateKey) { | ||
event.id = await getEventHash(event) | ||
event.sig = await signEvent(event, globalPrivateKey) | ||
@@ -106,0 +107,0 @@ } else { |
20285
600
11
+ Addedbip32@^3.0.1
+ Addedcreate-hash@^1.2.0
+ Addedmicro-bip39@^0.1.3
+ Addedtiny-secp256k1@^2.1.2
+ Added@noble/hashes@0.5.9(transitive)
+ Addedbase-x@3.0.10(transitive)
+ Addedbip32@3.1.0(transitive)
+ Addedbs58@4.0.1(transitive)
+ Addedbs58check@2.1.2(transitive)
+ Addedmicro-base@0.10.2(transitive)
+ Addedmicro-bip39@0.1.3(transitive)
+ Addedtiny-secp256k1@2.2.3(transitive)
+ Addedtypeforce@1.18.0(transitive)
+ Addeduint8array-tools@0.0.7(transitive)
+ Addedwif@2.0.6(transitive)
- Removedbip39@^3.0.4
- Removed@noble/hashes@1.5.0(transitive)
- Removedbip39@3.1.0(transitive)