Skip to main content

Customize the WebSocket client

Overview

The CloudWebSocketClient class is a protocol-aware WebSocket client designed specifically for MPC (Multi-Party Computation) operations. It serves as a thin wrapper around the SilentShardWebSocketClient which implements IWebSocketClient from @silencelaboratories/silent-shard-sdk.

1. CloudWebSocketClient

  • Role: Protocol-aware MPC client
  • Responsibility:
    • MPC-specific URL construction
    • Operation routing (keygen/sign/refresh)
  • Customization Use Cases:
    • Add authentication headers

2. SilentShardWebSocketClient implements IWebSocketClient

  • Role: Low-level WebSocket implementation
  • Responsibility:
    • Raw WebSocket connection management
    • Message queueing and asynchronous I/O
    • Binary/text data transmission
  • Customization Use Cases:
    • Replace WebSocket library (e.g., Socket.IO, different WebSocket impl)
    • Modify low-level message queueing logic

Example adding authentication headers to WebSocket connection

App.tsx
import {
CloudWebSocketClient,
createEcdsaDuoSession,
InMemoryStorageProvider,
} from '@silencelaboratories/silent-shard-sdk';
import type { WebSocketAction } from '@/silent-shard-sdk/src/network/WebSocketAction';

class CustomCloudClient extends CloudWebSocketClient {
connect(opt: WebSocketAction): Promise<void> {
// You can read your client ID and access token from environment variables or a secure store.
const token = 'client-access-token-here';
const headers = {
'client-id': 'your-client-id-here',
'access-token': token,
};
return super.connect(opt, { headers });
}
}

// Then you can use the CustomCloudClient in Session objects
const customClient = new CustomCloudClient('CLOUD_NODE_URL', true); // true for WSS, false for WS
const storage = new InMemoryStorageProvider();

createEcdsaDuoSession({
client: customClient,
storage: storage,
cloudVerifyingKey: '',
messageSigner: {
publicKeyB64: '',
sign: async (message: string) => message,
keyType: 'ED25519',
},
});