Account Management
The SDK handles various types of accounts that interact with the OpenMSCP protocol. Understanding these accounts is crucial for effective development.
Account Types
// Profile Account
interface ProfileAccount {
publicKey: PublicKey;
username: string;
bio: string;
profilePicture: string;
createdAt: number;
updatedAt: number;
}
// Post Account
interface PostAccount {
publicKey: PublicKey;
author: PublicKey;
content: string;
createdAt: number;
likes: number;
comments: number;
}
// Message Account
interface MessageAccount {
publicKey: PublicKey;
sender: PublicKey;
recipient: PublicKey;
content: string;
encrypted: boolean;
createdAt: number;
}
Account Creation and Management
// Create a new account
const account = await client.createAccount({
type: 'profile',
data: {
username: 'alice',
bio: 'Blockchain enthusiast'
}
});
// Update an account
await client.updateAccount(account.publicKey, {
bio: 'Updated bio'
});
// Delete an account
await client.deleteAccount(account.publicKey);
Transaction Handling
The SDK provides robust transaction handling capabilities.
Transaction Types
// Basic transaction
const tx = await client.createTransaction({
type: 'profile_update',
data: {
bio: 'New bio'
}
});
// Batch transaction
const batchTx = await client.createBatchTransaction([
{
type: 'profile_update',
data: { bio: 'New bio' }
},
{
type: 'post_create',
data: { content: 'New post' }
}
]);
Transaction Signing and Sending
// Sign and send a transaction
const signedTx = await client.signTransaction(tx);
const signature = await client.sendTransaction(signedTx);
// Confirm transaction
const confirmation = await client.confirmTransaction(signature);
Error Handling
The SDK implements a comprehensive error handling system.
Error Types
// Network errors
class NetworkError extends Error {
constructor(message: string) {
super(message);
this.name = 'NetworkError';
}
}
// Account errors
class AccountError extends Error {
constructor(message: string) {
super(message);
this.name = 'AccountError';
}
}
// Transaction errors
class TransactionError extends Error {
constructor(message: string) {
super(message);
this.name = 'TransactionError';
}
}
Error Handling Patterns
try {
const result = await client.someOperation();
} catch (error) {
if (error instanceof NetworkError) {
// Handle network issues
} else if (error instanceof AccountError) {
// Handle account-related issues
} else if (error instanceof TransactionError) {
// Handle transaction issues
}
}
Network Configuration
The SDK provides flexible network configuration options.
Connection Options
const connectionOptions = {
commitment: 'confirmed',
confirmTransactionInitialTimeout: 60000,
wsEndpoint: 'wss://api.devnet.solana.com',
httpHeaders: {
'x-custom-header': 'value'
}
};
const client = new OpenMSCPClient(connection, wallet, connectionOptions);
Network Switching
// Switch to devnet
await client.switchNetwork('devnet');
// Switch to mainnet
await client.switchNetwork('mainnet');
// Switch to custom endpoint
await client.switchNetwork('https://custom-rpc-endpoint.com');
Security Considerations
The SDK implements several security features to protect user data and transactions.
Encryption
// Encrypt data
const encryptedData = await client.encrypt(data, recipientPublicKey);
// Decrypt data
const decryptedData = await client.decrypt(encryptedData, senderPublicKey);
Security Best Practices
Always verify transaction signatures
Implement proper error handling
Use the latest SDK version
Security Configuration
const securityConfig = {
requireSignatures: true,
validateTransactions: true,
encryptionEnabled: true,
timeout: 30000
};
const client = new OpenMSCPClient(connection, wallet, { security: securityConfig });