'Sign and verify JWS (json web signature) with Ed25519 KeyPair

I want to sign a JWS (json web signature) with a private key generated through Ed25519 on a clients device. Then send this signature to my backend and verify it with the public key. To get familiar with the procedure I want to try to sign and verify a JWS in node js.
Both my private and public key are already generated and are available in base58. This is my current attempt at signing a JWT with an Ed25519 privateKey:

const { SignJWT } = require("jose/jwt/sign");
const bs50 = require("bs58");

async function main() {
  const publicBase58 = "A77GCUCZ7FAuXVMKtwwXyFhMa158XsaoGKHYNnJ1q3pv";
  const privateKeyBase58 = "BE1VM7rTRJReLsTLLG4JMNX5ozcp7qpmMuRht9zB1UjU";

  const publicKeyBuffer = bs50.decode(publicBase58);
  const privateKeyBuffer = bs50.decode(privateKeyBase58);

  const publicKey = new Uint8Array(publicKeyBuffer);
  const privateKey = new Uint8Array(privateKeyBuffer);

  const jwt = await new SignJWT({
    subject: "uuid",
  })
    .setProtectedHeader({ alg: "EdDSA" })
    .setExpirationTime("2h")
    .sign(privateKey);

  console.log(jwt);
}

Error: TypeError: Key must be one of type KeyObject or CryptoKey. Received an instance of Uint8Array

When trying to use the sign() function I get the error above because my privateKey is of type Uint8Array, the only accepted types are KeyObject or CryptoKey but I don't know how I can convert my Uint8Arrays into KeyObjects or CryptoKeys.

I got some code snippets from this answer



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source