Is there a way to generate truly random bitcoin private key without relying on 3rd party libraries like ECPair nor tiny-secp256k1?
As a recommended approach to generate secure random key is to use something like this:
import ECPairFactory from 'ecpair'
import * as ecc from 'tiny-secp256k1'
const ECPair = ECPairFactory(ecc)
export function generatePrivateKey() {
const keyPair = ECPair.makeRandom()
return keyPair.privateKey.toString('hex')
}
Can the same be done by just using browser native Web Crypto API window.crypto.getRandomValues only?
function generatePrivateKey() {
const privateKeyBytes = new Uint8Array(32); // 256 bits for a Bitcoin private key
window.crypto.getRandomValues(privateKeyBytes)
const privateKeyHex =
Array.from(privateKeyBytes)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('')
return privateKeyHex
}