英文:
Uncaught TypeError: Cannot read properties of undefined (reading 'utils')
问题
当我尝试运行我的应用程序时,在我的Chrome浏览器控制台中出现了此错误 - **未捕获的TypeError: 无法读取未定义的属性(读取 'utils')
这是错误的详情
错误
**
以下是我的代码
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('私钥:', toHex(privateKey));
const publicKey = secp.getPublicKey(privateKey);
console.log('公钥:', toHex(publicKey));
const address = (keccak256(publicKey.slice(1)).slice(-20));
console.log('以太坊公钥:', toHex(address));
function GenerateKey() {
return (
<div>
<p>私钥: {privateKey}</p>
<p>公钥: {publicKey}</p>
<p>地址: {address}</p>
</div>
)
}
export default GenerateKey;
请问我应该如何修复这个问题
我正在尝试生成随机的加密密钥
英文:
When I try to run my app, it results to this error in my chrome browser console - **Uncaught TypeError: Cannot read properties of undefined (reading 'utils')
Here is the error
Error
**
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 do I fix this
I am trying to generate random cryptographic keys
答案1
得分: 2
尝试像这样使用导入:
import {
getPublicKey,
utils,
} from 'ethereum-cryptography/secp256k1'
然后生成你的密钥:
const privateKey = utils.randomPrivateKey()
const publicKey = getPublicKey(privateKey)
你会遇到一个错误,因为 ethereum-cryptography/secp256k1
路径没有默认导出。
另一种方法是使用 * as
构造:
import * as secp from 'ethereum-cryptography/secp256k1'
const privateKey = secp.utils.randomPrivateKey()
const publicKey = secp.getPublicKey(privateKey)
英文:
Try to use imports like this:
import {
getPublicKey,
utils,
} from 'ethereum-cryptography/secp256k1'
And then generate your keys:
const privateKey = utils.randomPrivateKey()
const publicKey = getPublicKey(privateKey)
You get an error because ethereum-cryptography/secp256k1
path doesn't have a default export.
Another approach is to use * as
construction:
import * as secp from 'ethereum-cryptography/secp256k1'
const privateKey = secp.utils.randomPrivateKey()
const publicKey = secp.getPublicKey(privateKey)
答案2
得分: 0
尝试按照以下方式导入secp作为secp256k1:
将第1行更改为:
import { secp256k1 } from 'ethereum-cryptography/secp256k1';
将第5行更改为:
const privateKey = secp256k1.utils.randomPrivateKey();
将第8行更改为:
const publicKey = secp256k1.getPublicKey(privateKey);
这是因为对secp256k1模块进行了更新。之前,它使用的是noble-secp256k1 1.7,现在它使用更安全的noble-curve。请参考curves README中的升级部分。
希望这对您有所帮助。
英文:
Try importing the secp as secp256k1 as shown below:
Change line 1 to:
import { secp256k1 } from 'ethereum-cryptography/secp256k1';
Change line 5 to:
const privateKey = secp256k1.utils.randomPrivateKey();
Change line 8 to:
const publicKey = secp256k1.getPublicKey(privateKey);
This is because of the updates that were made on the secp256k1 module. Before, it was using the noble-secp256k1 1.7 and now it is using the safer noble-curve. Please refer to the upgrading section from curves README.
I hope this was helpful.
答案3
得分: 0
尝试将
import secp from "ethereum-cryptography/secp256k1";
替换为
const { secp256k1 } = require("ethereum-cryptography/secp256k1.js");
以及将
secp.utils
替换为
secp256k1.utils
英文:
Try replacing
import secp from "ethereum-cryptography/secp256k1";
by
const { secp256k1 } = require("ethereum-cryptography/secp256k1.js");
and instead of
secp.utils ... use secp256k1.utils
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论