mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
20 lines
514 B
JavaScript
20 lines
514 B
JavaScript
import randomBytes from 'randombytes'
|
|
import {isPrivate, pointFromScalar} from 'tiny-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'
|
|
)
|
|
}
|
|
|
|
export function getPublicKey(privateKey) {
|
|
return Buffer.from(pointFromScalar(Buffer.from(privateKey, 'hex'), true))
|
|
.toString('hex')
|
|
.slice(2)
|
|
}
|