I am trying to generate random cryptographic keys, but when I try to run my app, it results to this error in my browser console Uncaught TypeError: Cannot read properties of undefined (reading 'utils').
Here is my code:
import secp from "ethereum-cryptography/secp256k1";
import { keccak256 } from "ethereum-cryptography/keccak";
import { toHex } from "ethereum-cryptography/utils";
const privateKey = secp.utils.randomPrivateKey();
console.log('Private key:', toHex(privateKey));
const publicKey = secp.getPublicKey(privateKey);
console.log('Public key:', toHex(publicKey));
const address = (keccak256(publicKey.slice(1)).slice(-20));
console.log('Ethereum public key:', toHex(address));
function GenerateKey() {
return (
<div>
<p>Private key: {privateKey}</p>
<p>Public key: {publicKey}</p>
<p>Address: {address}</p>
</div>
)
}
export default GenerateKey;
Please how can I fix this?
Try to use imports like this:
And then generate your keys:
You get an error because
ethereum-cryptography/secp256k1path doesn't have a default export.Another approach is to use
* asconstruction: