Uncaught TypeError: 无法读取未定义的属性 (读取 ‘utils’)

huangapple go评论60阅读模式
英文:

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 &quot;ethereum-cryptography/secp256k1&quot;;
import { keccak256 } from &quot;ethereum-cryptography/keccak&quot;;
import { toHex } from &quot;ethereum-cryptography/utils&quot;;

const privateKey = secp.utils.randomPrivateKey();
console.log(&#39;Private key:&#39;, toHex(privateKey));

const publicKey = secp.getPublicKey(privateKey);
console.log(&#39;Public key:&#39;, toHex(publicKey));

const address = (keccak256(publicKey.slice(1)).slice(-20));
console.log(&#39;Ethereum public key:&#39;, toHex(address));

function GenerateKey() {
    return (
        &lt;div&gt;
            &lt;p&gt;Private key: {privateKey}&lt;/p&gt;
            &lt;p&gt;Public key: {publicKey}&lt;/p&gt;
            &lt;p&gt;Address: {address}&lt;/p&gt;
        &lt;/div&gt;
    )
}

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 &#39;ethereum-cryptography/secp256k1&#39;

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 &#39;ethereum-cryptography/secp256k1&#39;

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 &#39;ethereum-cryptography/secp256k1&#39;;

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

huangapple
  • 本文由 发表于 2023年2月16日 19:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定