如何使用元掩码在 IPFS 上存储(上传)之前加密任何文档?

问题描述 投票:0回答:0

我想在上传到 IPFS 之前加密文档并允许多个用户解密它并注意用户数量可能随时增加或减少

我在 metamask 上使用帐户 1 中的以下代码来加密某些文本

let encryptionPublicKey;
await window.ethereum.request({
  method: 'eth_getEncryptionPublicKey',
  params: [account1], // you must have access to the specified account
})
.then((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);
  }
});

  const ethUtil = require('ethereumjs-util');
  const sigUtil = require('@metamask/eth-sig-util');
  const encryptedMessage = ethUtil.bufferToHex(
    Buffer.from(
      JSON.stringify(
        sigUtil.encrypt({
          publicKey: encryptionPublicKey,
          data: 'hello world!',
          version: 'x25519-xsalsa20-poly1305',
        })
      ),
      'utf8'
    )
  );

然后我尝试用账号1用账号2解密上面的密文

    const encryptedMessage = '0x7b2276657273 .....';
    account1 = '0x....';
    await window.ethereum.request({
        method: 'eth_decrypt',
        params: [encryptedMessage,account1],
      })
      .then((decryptedMessage) =>
        console.log('The decrypted message is:', decryptedMessage)
      )
      .catch((error) => console.log(error.message));
  }

我在控制台上收到此错误消息:“MetaMask - RPC 错误:用户未授权请求的帐户和/或方法。”

encryption cryptography web3js metamask ipfs
© www.soinside.com 2019 - 2024. All rights reserved.