forked from
smokesignal.events/atproto-plc
Rust and WASM did-method-plc tools and structures
1/**
2 * JavaScript wrapper for atproto-plc WASM module
3 *
4 * This module provides a friendly JavaScript API for working with did:plc
5 * identifiers in web browsers and Node.js.
6 */
7
8import init, {
9 WasmDid,
10 WasmDidBuilder,
11 WasmDidDocument,
12 WasmOperation,
13 WasmSigningKey,
14 WasmServiceEndpoint
15} from '../pkg/atproto_plc.js';
16
17// Initialize WASM module
18let initialized = false;
19
20async function ensureInitialized() {
21 if (!initialized) {
22 await init();
23 initialized = true;
24 }
25}
26
27/**
28 * Parse and validate a DID string
29 *
30 * @param {string} didString - The DID to parse (e.g., "did:plc:...")
31 * @returns {Promise<WasmDid>} The parsed DID
32 * @throws {Error} If the DID is invalid
33 */
34export async function parseDid(didString) {
35 await ensureInitialized();
36 return new WasmDid(didString);
37}
38
39/**
40 * Create a new DID builder
41 *
42 * @returns {Promise<WasmDidBuilder>} A new DID builder
43 */
44export async function createDidBuilder() {
45 await ensureInitialized();
46 return new WasmDidBuilder();
47}
48
49/**
50 * Generate a new P-256 signing key
51 *
52 * @returns {Promise<WasmSigningKey>} A new signing key
53 */
54export async function generateP256Key() {
55 await ensureInitialized();
56 return WasmSigningKey.generateP256();
57}
58
59/**
60 * Generate a new secp256k1 signing key
61 *
62 * @returns {Promise<WasmSigningKey>} A new signing key
63 */
64export async function generateK256Key() {
65 await ensureInitialized();
66 return WasmSigningKey.generateK256();
67}
68
69/**
70 * Create a new service endpoint
71 *
72 * @param {string} serviceType - The service type
73 * @param {string} endpoint - The endpoint URL
74 * @returns {Promise<WasmServiceEndpoint>} A new service endpoint
75 */
76export async function createServiceEndpoint(serviceType, endpoint) {
77 await ensureInitialized();
78 return new WasmServiceEndpoint(serviceType, endpoint);
79}
80
81/**
82 * Parse a DID document from JSON
83 *
84 * @param {string} json - The JSON string
85 * @returns {Promise<WasmDidDocument>} The parsed DID document
86 */
87export async function parseDidDocument(json) {
88 await ensureInitialized();
89 return WasmDidDocument.fromJson(json);
90}
91
92/**
93 * Parse an operation from JSON
94 *
95 * @param {string} json - The JSON string
96 * @returns {Promise<WasmOperation>} The parsed operation
97 */
98export async function parseOperation(json) {
99 await ensureInitialized();
100 return WasmOperation.fromJson(json);
101}
102
103// Re-export WASM types
104export {
105 WasmDid as Did,
106 WasmDidBuilder as DidBuilder,
107 WasmDidDocument as DidDocument,
108 WasmOperation as Operation,
109 WasmSigningKey as SigningKey,
110 WasmServiceEndpoint as ServiceEndpoint
111};
112
113// Export initialization function
114export { init };