denoland / deno

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

PEM Base64 Error while using mineflayer NPM Package #26634

Open Nexus-00 opened 3 weeks ago

Nexus-00 commented 3 weeks ago

Version: Deno 2.0.3 Hello.

I am interested in using Deno to build a Minecraft player bot. I used the minelayer package and the Deno runtime. I wrote simple code that should log in to the player using Microsoft authentication. I am using Deno running on aarch64 Linux. I am also running mineflayer version 4.23.0.

I am currently attempting to run this code:

import mineflayer from "mineflayer";

console.log("Hello, world!");
const bot = mineflayer.createBot({
    host: "[remote ip]",
    port: 25565,
    username: "[username]",
    auth: "microsoft",
    version: "1.20.4",
});

bot.on("login", () => {
    bot.chat("Hello, world!");
});

After authenticating with a Microsoft account. I get this strange error:

error: Uncaught (in promise) Error: ASN.1 error: PEM error: PEM Base64 error: invalid Base64 encoding
    at Object.publicEncrypt (ext:deno_node/internal/crypto/cipher.ts:221:10)
    at sendEncryptionKeyResponse (file:///home/[username]/.cache/deno/npm/registry.npmjs.org/minecraft-protocol/1.50.0/src/client/encrypt.js:49:52)
    at onJoinServerResponse (file:///home/[username]/.cache/deno/npm/registry.npmjs.org/minecraft-protocol/1.50.0/src/client/encrypt.js:36:11)
    at file:///home/[username]/.cache/deno/npm/registry.npmjs.org/yggdrasil/1.7.0/src/utils.js:73:15
    at Object.runMicrotasks (ext:core/01_core.js:672:26)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:57:10)
    at runNextTicks (ext:deno_node/_next_tick.ts:74:3)
    at eventLoopTick (ext:core/01_core.js:182:21)

This code works as intended using the latest version of node js, which is 23.0.0-1. Steps to reproduce:

  1. Install mineflayer package. deno add npm:mineflayer
  2. Write above code.
  3. Run script and authenticate. deno run main.ts
  4. Observe error.
interrrp commented 3 weeks ago

See https://github.com/denoland/deno/issues/20397 and the related PR on Prismarine's side: https://github.com/PrismarineJS/node-minecraft-protocol/pull/1292

I believe this is not a problem on Deno's side (unless they intend to double down on the security side of things for 100% Node compatibility); node-minecraft-protocol isn't properly following the related RFC

sp0oOk commented 3 weeks ago

Also facing the same issue, tried changing Deno versions, Mineflayer versions. Unsure on how some people apparently managed to get this working.

import { BaseModule } from "../ModuleTypes.ts";
import { Bot, BotEvents, BotOptions, createBot } from "npm:mineflayer";
import { config } from "../main.ts";
import * as log from "@std/log";

type BotEventListeners = {
  [K in keyof BotEvents]?: (...args: Parameters<BotEvents[K]>) => void;
};

export interface MinecraftModule extends BaseModule, BotEventListeners {
  getBot(): Bot | undefined;
}

let options: Partial<BotOptions>,
  instance: Bot | undefined;

export const MinecraftModule: MinecraftModule = {
  name: "Minecraft Module",
  init: () => {
    options = config.get("mineflayer");
    instance = createBot(options as BotOptions);
  },
  getBot: () => {
    return instance;
  },
  login: () => {
    log.info(
      `Logged onto ${options.host || "unknown"} as ${
        instance?.username || "N/A"
      }!`,
    );
  },
};

Error:

error: Uncaught (in promise) Error: ASN.1 error: PEM error: PEM Base64 error: invalid Base64 encoding
    at Object.publicEncrypt (ext:deno_node/internal/crypto/cipher.ts:221:10)
    at sendEncryptionKeyResponse (file:///C:/Users/spook/AppData/Local/deno/npm/registry.npmjs.org/minecraft-protocol/1.50.0/src/client/encrypt.js:49:52)
    at onJoinServerResponse (file:///C:/Users/spook/AppData/Local/deno/npm/registry.npmjs.org/minecraft-protocol/1.50.0/src/client/encrypt.js:36:11)
    at file:///C:/Users/spook/AppData/Local/deno/npm/registry.npmjs.org/yggdrasil/1.7.0/src/utils.js:73:15
    at Object.runMicrotasks (ext:core/01_core.js:672:26)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:59:10)
    at runNextTicks (ext:deno_node/_next_tick.ts:76:3)
    at eventLoopTick (ext:core/01_core.js:182:21)