Is it possible to create a truly random bitcoin private key without depending on third-party libraries like ECPair or tiny-secp256k1?
An alternative method for generating a secure random key is as follows:
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 task be achieved solely using the Web Crypto API window.crypto.getRandomValues
in the browser?
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
}