mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
remove buffer usage everywhere.
This commit is contained in:
12
event.ts
12
event.ts
@@ -1,8 +1,8 @@
|
||||
import {Buffer} from 'buffer'
|
||||
// @ts-ignore
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
import {sha256} from '@noble/hashes/sha256'
|
||||
|
||||
import {utf8Encoder} from './utils'
|
||||
|
||||
export type Event = {
|
||||
id?: string
|
||||
sig?: string
|
||||
@@ -35,8 +35,8 @@ export function serializeEvent(evt: Event): string {
|
||||
}
|
||||
|
||||
export function getEventHash(event: Event): string {
|
||||
let eventHash = sha256(Buffer.from(serializeEvent(event)))
|
||||
return Buffer.from(eventHash).toString('hex')
|
||||
let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))
|
||||
return secp256k1.utils.bytesToHex(eventHash)
|
||||
}
|
||||
|
||||
export function validateEvent(event: Event): boolean {
|
||||
@@ -63,7 +63,7 @@ export function verifySignature(
|
||||
}
|
||||
|
||||
export async function signEvent(event: Event, key: string): Promise<string> {
|
||||
return Buffer.from(
|
||||
return secp256k1.utils.bytesToHex(
|
||||
await secp256k1.schnorr.sign(event.id || getEventHash(event), key)
|
||||
).toString('hex')
|
||||
)
|
||||
}
|
||||
|
||||
5
keys.ts
5
keys.ts
@@ -1,10 +1,9 @@
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
import {Buffer} from 'buffer'
|
||||
|
||||
export function generatePrivateKey(): string {
|
||||
return Buffer.from(secp256k1.utils.randomPrivateKey()).toString('hex')
|
||||
return secp256k1.utils.bytesToHex(secp256k1.utils.randomPrivateKey())
|
||||
}
|
||||
|
||||
export function getPublicKey(privateKey: string): string {
|
||||
return Buffer.from(secp256k1.schnorr.getPublicKey(privateKey)).toString('hex')
|
||||
return secp256k1.utils.bytesToHex(secp256k1.schnorr.getPublicKey(privateKey))
|
||||
}
|
||||
|
||||
7
nip06.ts
7
nip06.ts
@@ -1,3 +1,4 @@
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
import {wordlist} from '@scure/bip39/wordlists/english.js'
|
||||
import {
|
||||
generateMnemonic,
|
||||
@@ -7,14 +8,14 @@ import {
|
||||
import {HDKey} from '@scure/bip32'
|
||||
|
||||
export function privateKeyFromSeed(seed: string): string {
|
||||
let root = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
|
||||
let root = HDKey.fromMasterSeed(secp256k1.utils.hexToBytes(seed))
|
||||
let privateKey = root.derive(`m/44'/1237'/0'/0/0`).privateKey
|
||||
if (!privateKey) throw new Error('could not derive private key')
|
||||
return Buffer.from(privateKey).toString('hex')
|
||||
return secp256k1.utils.bytesToHex(privateKey)
|
||||
}
|
||||
|
||||
export function seedFromWords(mnemonic: string): string {
|
||||
return Buffer.from(mnemonicToSeedSync(mnemonic)).toString('hex')
|
||||
return secp256k1.utils.bytesToHex(mnemonicToSeedSync(mnemonic))
|
||||
}
|
||||
|
||||
export function generateSeedWords(): string {
|
||||
|
||||
Reference in New Issue
Block a user