使用公钥的链链加密

发布于 2025-02-05 18:24:06 字数 1649 浏览 1 评论 0原文

我正在尝试在将用户的公共密钥上传到区块链之前使用用户的公钥签名并加密元数据,并使用用户的私钥离链解密数据。我遵循 this 但是它要求在MetAmask中使用当前连接的用户的私钥。

因此,我尝试在metAmask文档中关注教程 [1] 。这是代码:

await window.ethereum.enable();
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });

let encryptionPublicKey;

ethereum
.request({
    method: 'eth_getEncryptionPublicKey',
    params: [accounts[0]], // you must have access to the specified account
})
.then((result) => {
    console.log(result);
    encryptionPublicKey = result;
})
.catch((error) => {
    if (error.code === 4001) {
    // EIP-1193 userRejectedRequest error
    console.log("We can't encrypt anything without the key.");
    } else {
    console.error(error);
    }
});

console.log(encryptionPublicKey);
const encryptedMessage = ethUtil.bufferToHex(
    Buffer.from(
      JSON.stringify(
        sigUtil.encrypt({
          publicKey: encryptionPublicKey,
          data: 'hello world!',
          version: 'x25519-xsalsa20-poly1305',
        })
      ),
      'utf8'
    )
);

我要获得一个空的结果,因此是一个空的EncryptionPublicKey。因此,我遇到了这个错误:

Uncaught (in promise) Error: Missing publicKey parameter

我该如何解决?任何帮助将不胜感激。

关于使用公钥加密数据的类似问题,但没有明确的答案: [1]

I am trying to sign and encrypt a metadata using a user's public key before uploading it to the blockchain, and decrypt the data with the user's private key off-chain. I have followed this and it worked perfectly but it is asking for the user's private key of the currently connected user in Metamask.

So I tried following the tutorial in Metamask's documentation [1]. And this is the code:

await window.ethereum.enable();
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });

let encryptionPublicKey;

ethereum
.request({
    method: 'eth_getEncryptionPublicKey',
    params: [accounts[0]], // you must have access to the specified account
})
.then((result) => {
    console.log(result);
    encryptionPublicKey = result;
})
.catch((error) => {
    if (error.code === 4001) {
    // EIP-1193 userRejectedRequest error
    console.log("We can't encrypt anything without the key.");
    } else {
    console.error(error);
    }
});

console.log(encryptionPublicKey);
const encryptedMessage = ethUtil.bufferToHex(
    Buffer.from(
      JSON.stringify(
        sigUtil.encrypt({
          publicKey: encryptionPublicKey,
          data: 'hello world!',
          version: 'x25519-xsalsa20-poly1305',
        })
      ),
      'utf8'
    )
);

I'm getting an empty result, thus an empty encryptionPublicKey. Hence, I am getting this error:

Uncaught (in promise) Error: Missing publicKey parameter

How can I fix this? Any help is greatly appreciated.

Similar Question on encrypting data using public key but there is no clear answer:
[1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-02-12 18:24:06

我设法通过在以ethereum.request之前将其运行。

其他问题:这种加密和解密方法在创建和检索区块链中的交易中是否有效?

I managed to get it working by putting await before ethereum.request.

Additional Question: Does this encryption and decryption method works in creating and retrieving transactions in blockchain?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文