// src/signing.ts
import { ml_dsa65 } from "@noble/post-quantum/ml-dsa";
import { randomBytes } from "@noble/hashes/utils";
var DILITHIUM3_SEED_BYTES = 32;
function generateKeypair(seed) {
  const actualSeed = seed ?? randomBytes(DILITHIUM3_SEED_BYTES);
  if (actualSeed.length !== DILITHIUM3_SEED_BYTES) {
    throw new Error(
      `Dilithium3 seed must be ${DILITHIUM3_SEED_BYTES} bytes, got ${actualSeed.length}`
    );
  }
  const keys = ml_dsa65.keygen(actualSeed);
  return {
    publicKey: keys.publicKey,
    secretKey: keys.secretKey
  };
}
function sign(message, secretKey) {
  return ml_dsa65.sign(secretKey, message);
}
function verify(message, signature, publicKey) {
  try {
    return ml_dsa65.verify(publicKey, message, signature);
  } catch {
    return false;
  }
}

// src/hashing.ts
import { blake3 as nobleBlake3 } from "@noble/hashes/blake3";
import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
function blake3(data, dkLen = 32) {
  return nobleBlake3(data, { dkLen });
}
function sha256(data) {
  return nobleSha256(data);
}

// src/address.ts
var ADDRESS_BYTE_LENGTH = 20;
var ADDRESS_HEX_LENGTH = 40;
function addressBytesFromPublicKey(publicKey) {
  return blake3(publicKey).slice(0, ADDRESS_BYTE_LENGTH);
}
function addressFromPublicKey(publicKey) {
  const bytes = addressBytesFromPublicKey(publicKey);
  return toChecksumAddress(bytesToHex(bytes));
}
function toChecksumAddress(address) {
  const stripped = address.toLowerCase().replace(/^0x/, "");
  if (stripped.length !== ADDRESS_HEX_LENGTH) {
    throw new Error(
      `Invalid address length: expected ${ADDRESS_HEX_LENGTH} hex chars, got ${stripped.length}`
    );
  }
  if (!/^[0-9a-f]+$/.test(stripped)) {
    throw new Error("Invalid address: non-hex characters present");
  }
  const hashBytes = blake3(new TextEncoder().encode(stripped));
  const hashHex = bytesToHex(hashBytes);
  let checksum = "";
  for (let i = 0; i < stripped.length; i++) {
    const char = stripped.charAt(i);
    const hashChar = hashHex.charAt(i);
    const nibble = parseInt(hashChar, 16);
    checksum += nibble >= 8 ? char.toUpperCase() : char;
  }
  return `0x${checksum}`;
}
function isValidAddress(address) {
  if (typeof address !== "string") return false;
  if (!address.startsWith("0x")) return false;
  const stripped = address.slice(2);
  if (stripped.length !== ADDRESS_HEX_LENGTH) return false;
  return /^[0-9a-fA-F]+$/.test(stripped);
}
function bytesToHex(bytes) {
  let hex = "";
  for (let i = 0; i < bytes.length; i++) {
    hex += (bytes[i] ?? 0).toString(16).padStart(2, "0");
  }
  return hex;
}
export {
  addressBytesFromPublicKey,
  addressFromPublicKey,
  blake3,
  generateKeypair,
  isValidAddress,
  sha256,
  sign,
  toChecksumAddress,
  verify
};
//# sourceMappingURL=index.js.map