LIT-Protocol / Issues-and-Reports

For bug reports, feature requests, and implementation questions related to Lit Protocol and use of the Lit SDK.
0 stars 0 forks source link

[Discord] Decryption Support #53

Open spacesailor24 opened 1 month ago

spacesailor24 commented 1 month ago

Discord thread

Description of the issue

Hello, I upgraded the Lit SDK to version 5.1.0. I'm trying to encrypt and decrypt files. Encrypting is going fine, but when I try to decrypt, I get a generic error that doesn't help me understand what may be wrong. It just says, "Failed to decrypt." and the below is the code

For encrypt:

  const encryptFile = async (
    file: File,
    accessControlConditions: any,
    uploadOption: Storage
  ) => {
    if (!litNodeClient) {
      await connect();
    }
    let authSig = JSON.parse(
      localStorage.getItem(LIT_LOCAL_STORAGE_KEYS.AUTH_SIGNATURE) || "{}"
    );
    if (!authSig) {
      disconnect();
    }

    switch (uploadOption) {
      case Storage.PINATA:
        const { ciphertext, dataToEncryptHash } = await LitJsSdk.encryptFile(
          {
            file,
            chain: chain,
            accessControlConditions,
            authSig,
          },
          litNodeClient
        );

        return {
          ciphertext,
          dataToEncryptHash,
        };

      default:
        throw new Error("Invalid upload to storage option");
    }
  };

For decrypt:

  const decryptFile = async (
    accessControlConditions: any,
    uploadOption: Storage,
    dataToEncryptHash: string,
    ciphertext: any
  ) => {
    try {
      console.log("accessControlConditions", accessControlConditions);
      console.log("uploadOption", uploadOption);
      console.log("dataToEncryptHash", dataToEncryptHash);
      console.log("ciphertext", ciphertext);

      // Connect to litNodeClient if not already connected
      if (!litNodeClient) {
        await connect();
      }

      // Retrieve authSig from local storage
      let authSig = JSON.parse(
        localStorage.getItem("lit-auth-signature") || "{}"
      );

      // If authSig is missing, disconnect and potentially reauthenticate
      if (!authSig) {
        disconnect();
      }

      switch (uploadOption) {
        case Storage.PINATA:
          const decryptedFile = await LitJsSdk.decryptToFile(
            {
              accessControlConditions,
              chain,
              ciphertext,
              dataToEncryptHash,
              authSig,
            },
            litNodeClient
          );
        default:
          throw new Error("Invalid upload to storage option");
      }
    } catch (error: any) {
      console.error("Error occurred during decryption:", error);
      toast.error(
        error.message ? error.message : "Error occurred during decryption"
      );
    }

  };

Additional context

I used to use Cayenne Network when I changed to Habanero or Manzano, I got error

"message: "Rate limit exceeded. Try again later."

more context here is how i mange uploading the files:

  const pinataStore = async (allFilesEncrypted: File[]) => {
    setAuthToken(true);
    const fileToUplod = await PreparingTheFileForUpload(allFilesEncrypted);

    let res;
    const fd = new FormData();
    fileToUplod.forEach(([file, meta], index) => {
      const encryptedBlob = new Blob([file], { type: "text/plain" });
      const encryptedFile = new File([encryptedBlob], meta.name);
      fd.append("file", encryptedFile, encryptedFile.name);
    });

    try {
      res = await progressAxiosInstance.post("/upload-to-pinata", fd, {
        onUploadProgress: (progressEvent) => {
          const { loaded, total } = progressEvent;
          let percent = Math.round((loaded * 100) / (total ? total : 100));
          setProgress(percent);
        },
      });
    } catch (error) {
      toast.error(
        "An error occurred during the upload process. Please try again in a few minutes."
      );
      console.log(error);
    }
    return res?.data;
  };
spacesailor24 commented 1 month ago

I used to use Cayenne Network when I changed to Habanero or Manzano, I got error "message: "Rate limit exceeded. Try again later."

This is because payment is required for network usage on the Habanero and Manzano networks.See these docs to learn more about minting and using Capacity Credits to pay for usage

spacesailor24 commented 1 month ago

Here is a workin code example of encrypting and decrypting a file using the datil-test network, please use it as a reference.

To further debug your issue, can you share the accessControlConditions you use for encryption, as well as the code you use to produce the authSig for decryption? Lastly, if you can share the Lit request ID for the failing decryption request, we can look at the Lit node logs to see what the cause was. You can obtain the Lit request ID if you enable the debug property when instantiating a LitNodeClient like so:

const litNodeClient = new LitNodeClient({
  litNetwork: LitNetwork.DatilDev,
  debug: true,
});
await litNodeClient.connect();