legacyMultibase

tcmts.cli.test.e2e.examples.legacyMultibase · #zyyykyrd · term · runs on the server

uses node:crypto

A secp256k1 public key (compressed, as exportPublicKey("raw") gives it) as a legacy DID document writes it: uncompressed, in base58btc multibase (z…), for an EcdsaSecp256k1VerificationKey2019 method.

import { ECDH } from "node:crypto";

export function legacyMultibase(compressed: Uint8Array): string {
  const bytes = ECDH.convertKey(
    compressed,
    "secp256k1",
    undefined,
    undefined,
    "uncompressed",
  ) as Buffer;
  const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  let n = BigInt(`0x${bytes.toString("hex")}`);
  let out = "";
  while (n > 0n) {
    out = ALPHABET[Number(n % 58n)]! + out;
    n /= 58n;
  }
  for (const b of bytes) {
    if (b !== 0) break;
    out = `1${out}`;
  }
  return `z${out}`;
}

used by