/** * JavaScript wrapper for atproto-plc WASM module * * This module provides a friendly JavaScript API for working with did:plc * identifiers in web browsers and Node.js. */ import init, { WasmDid, WasmDidBuilder, WasmDidDocument, WasmOperation, WasmSigningKey, WasmServiceEndpoint } from '../pkg/atproto_plc.js'; // Initialize WASM module let initialized = false; async function ensureInitialized() { if (!initialized) { await init(); initialized = true; } } /** * Parse and validate a DID string * * @param {string} didString - The DID to parse (e.g., "did:plc:...") * @returns {Promise} The parsed DID * @throws {Error} If the DID is invalid */ export async function parseDid(didString) { await ensureInitialized(); return new WasmDid(didString); } /** * Create a new DID builder * * @returns {Promise} A new DID builder */ export async function createDidBuilder() { await ensureInitialized(); return new WasmDidBuilder(); } /** * Generate a new P-256 signing key * * @returns {Promise} A new signing key */ export async function generateP256Key() { await ensureInitialized(); return WasmSigningKey.generateP256(); } /** * Generate a new secp256k1 signing key * * @returns {Promise} A new signing key */ export async function generateK256Key() { await ensureInitialized(); return WasmSigningKey.generateK256(); } /** * Create a new service endpoint * * @param {string} serviceType - The service type * @param {string} endpoint - The endpoint URL * @returns {Promise} A new service endpoint */ export async function createServiceEndpoint(serviceType, endpoint) { await ensureInitialized(); return new WasmServiceEndpoint(serviceType, endpoint); } /** * Parse a DID document from JSON * * @param {string} json - The JSON string * @returns {Promise} The parsed DID document */ export async function parseDidDocument(json) { await ensureInitialized(); return WasmDidDocument.fromJson(json); } /** * Parse an operation from JSON * * @param {string} json - The JSON string * @returns {Promise} The parsed operation */ export async function parseOperation(json) { await ensureInitialized(); return WasmOperation.fromJson(json); } // Re-export WASM types export { WasmDid as Did, WasmDidBuilder as DidBuilder, WasmDidDocument as DidDocument, WasmOperation as Operation, WasmSigningKey as SigningKey, WasmServiceEndpoint as ServiceEndpoint }; // Export initialization function export { init };