denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.04k stars 5.39k forks source link

Npm OpenPgp is not working on server side in Fresh.js #26099

Open usmanh333 opened 1 month ago

usmanh333 commented 1 month ago

Version: Deno 1.46.3 npm:openpgp its not working on server side in freshJs for data encryption getting this error : invalid utf-8 sequence of 1 bytes from index 1 whenever createMessage method call after reading the publicKey.

marvinhagemeister commented 1 month ago

Thanks for opening an issue. What is the full code snippet to reproduce this error? This way it's a lot easier for us to inspect what's going on if we can see the error on our end.

usmanh333 commented 1 month ago

In handler (api folder) i have an endpoint to create orders. here full code snippet to reproduce this error. Hope this will help.

server side Error: invalid utf-8 sequence of 1 bytes from index 1

### helper.ts

export async function encryptData(key: string, string: string) {
    const publicKeyArmored = key;
    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored }); // console is working here not below
    const encrypted = await openpgp.encrypt({
        message: await openpgp.createMessage({ text: string }),
        encryptionKeys: publicKey,
    });
    return encrypted as string;
}

### API

import { encryptData } from "../../utils/helper.ts";
import { fetchPublicKey } from "../keys/operations.ts";
import * as openpgp from "npm:openpgp";

export async function CreateOrders(params: Orders[], siteLink: any) {
  try {
    const key = await fetchPublicKey();
    const publicKey = key.publicKey;
    const {
      userDetails,
      products,
      orderAmount,
      btcAmount,
      address,
      userDeviceDetails,
      shippingOption,
    }: any = params;

    const encUserDetails = {
      fname: await encryptData(publicKey, userDetails.fname),
      lname: await encryptData(publicKey, userDetails.lname),
      email: await encryptData(publicKey, userDetails.email),
      zip: await encryptData(publicKey, userDetails.zip),
      city: await encryptData(publicKey, userDetails.city),
      address: await encryptData(publicKey, userDetails.address),
      co: userDetails.co !== ""
        ? await encryptData(publicKey, userDetails.address)
        : "",
    };
    const encUserDeviceDetails = {
      browser: await encryptData(publicKey, userDeviceDetails.browser),
      device: await encryptData(publicKey, userDeviceDetails.device),
      ip: await encryptData(publicKey, userDeviceDetails.ip),
      os: await encryptData(publicKey, userDeviceDetails.os),
      resolution: await encryptData(publicKey, userDeviceDetails.resolution),
    };

    return true;
  } catch (error) {
    throw new Error(error);
  }
}

/api/orders/create.ts

import { Handlers } from "$fresh/server.ts";
import { Status } from "https://deno.land/std@0.200.0/http/http_status.ts";
import { CreateOrders } from "../../../database/orders/operations.ts";

export const handler: Handlers = {
  async POST(req, _) {
    const headers = new Headers(req.headers)
    const siteLink = headers.get("origin")
    try {
      const params = await req.json();
      await CreateOrders(params, siteLink);
      return new Response(null, {
        status: Status.Created,
        headers: {
          Location: "/",
        },
      });
    } catch (error) {
      return new Response(error, {
        status: Status.InternalServerError,
      });
    }
  },
};