Customize the WebSocket client
Overview
The CloudClient class is a protocol-aware WebSocket client designed specifically for MPC (Multi-Party Computation) operations. It serves as a thin wrapper around the WebSocketClient which implements WebSocketClientInterface from silent-shard-sdk.
1. CloudClient
- Role: Protocol-aware MPC client
- Responsibility:
- MPC-specific URL construction
- Operation routing (keygen/sign/refresh)
- Customization Use Cases:
- Add authentication headers
2. WebSocketClient implements WebSocketClientInterface
- 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
main.dart
import 'package:silent_shard_sdk/silent_shard_sdk.dart' as sdk;
// To add custom header with each connection
class CloudSocketClient extends sdk.CloudClient {
final String userToken;
CloudSocketClient(
{required super.baseUri,
required this.userToken,
required super.isSecure,
super.webSocketClient})
: super();
Future<void> connect(String uri, [String protocol = 'duo-instance']) {
return webSocketClient.connect(uri: uri, protocol: protocol, headers: {
'Authorization': 'Bearer $userToken',
});
}
}
Future<void> setup() async {
final customCloudClient = CloudSocketClient(
baseUri: 'wss://cloud.silent-shard.com',
isSecure: true,
userToken: 'your-token',
);
final session = await sdk.createDuoEcdsaSession(
cloudClient: customCloudClient,
cloudVerifyingKeyHex: 'SERVER_PUBLIC_KEY_HEX',
storageClient: sdk.SimpleStorageClient(),
);
final keyshare = await session.keygen();
print('Keyshare: $keyshare');
}