Customize the Storage Provider
Overview
The IStorageProvider interface is designed to be implemented by platform-specific storage solutions, such as SQLite or FileSystem. This interface allows the Mobile SDK to interact with the storage layer without being tied to a specific implementation.
The SDK provides a default implementation InMemoryStorageClient. This is useful for testing or development purposes, but it is not suitable for production use. But we can create a custom storage provider to persist data.
Implementing a Custom Storage Provider with AsyncStorage
App.tsx
import {
CloudWebSocketClient,
createEcdsaDuoSession,
type IStorageProvider,
type StorageDomain,
} from '@silencelaboratories/silent-shard-sdk';
import AsyncStorage from '@react-native-async-storage/async-storage';
class AsyncStorageProvider implements IStorageProvider {
async create(keyId: string, domain: StorageDomain, item: string): Promise<void> {
const storageKey = this.getStorageKey(keyId, domain);
// Check if item already exists
const existing = await this.exists(keyId, domain);
if (existing) {
throw new Error(`Item already exists for key: ${storageKey}`);
}
await AsyncStorage.setItem(storageKey, item);
}
async read(keyId: string, domain: StorageDomain): Promise<string | null> {
const storageKey = this.getStorageKey(keyId, domain);
return AsyncStorage.getItem(storageKey);
}
async update(keyId: string, domain: StorageDomain, item: string): Promise<void> {
const storageKey = this.getStorageKey(keyId, domain);
// Verify existence before update
const existing = await this.exists(keyId, domain);
if (!existing) {
throw new Error(`Item not found for update: ${storageKey}`);
}
await AsyncStorage.setItem(storageKey, item);
}
async delete(keyId: string, domain: StorageDomain): Promise<void> {
const storageKey = this.getStorageKey(keyId, domain);
await AsyncStorage.removeItem(storageKey);
}
async exists(keyId: string, domain: StorageDomain): Promise<boolean> {
const storageKey = this.getStorageKey(keyId, domain);
const value = await AsyncStorage.getItem(storageKey);
return value !== null;
}
// Helper: Generate storage key from keyId and domain
private getStorageKey(keyId: string, domain: StorageDomain): string {
return `mpc-${domain}-${keyId}`;
}
}
// Then you can use the CustomCloudClient in Session objects
const customStorageProvider = new AsyncStorageProvider();
const client = new CloudWebSocketClient('CLOUD_NODE_URL', true); // true for WSS, false for WS
export const initSession = async () => {
return createEcdsaDuoSession({
client: client,
storage: customStorageProvider,
cloudVerifyingKey: 'YOUR_CLOUD_VERIFICATION_KEY', // Replace with your actual key,
messageSigner: {
// This is a mock signer, replace with your actual implementation
publicKeyB64: '',
sign: async (message: string) => message,
keyType: 'ED25519',
},
});
};