使用公钥的链链加密
我正在尝试在将用户的公共密钥上传到区块链之前使用用户的公钥签名并加密元数据,并使用用户的私钥离链解密数据。我遵循 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法通过在
以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?