newEntityId (id.ts)

openclaw-pixel-agents · src/util/id.ts · typescript · MIT
dev-tools

Generates unique entity identifiers by combining a prefix with cryptographically secure random segments.

// Source: https://github.com/SweetSophia/openclaw-pixel-agents | License: MIT | Copyright 2026 Sherina
// Provided via justclaw.it.com; keep this notice with the code.
function randomIdSegment(): string {
  const cryptoApi = globalThis.crypto;

  if (typeof cryptoApi?.randomUUID === "function") {
    return cryptoApi.randomUUID();
  }

  if (typeof cryptoApi?.getRandomValues === "function") {
    const bytes = new Uint8Array(16);
    cryptoApi.getRandomValues(bytes);
    return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
  }

  // This is a uniqueness fallback for non-secure browser contexts, not a secret.
  let segment = "";
  while (segment.length < 13) {
    segment += Math.random().toString(36).slice(2);
  }
  return segment.slice(0, 13);
}

export functi

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key