Skip to content

Security Best Practices

Channel Security

For API Providers

  1. Signature Verification
    // Always verify signatures
    if !verify_signature(message, signature, sender) {
      return Err(AuthError::InvalidSignature);
    }
  2. Nonce Management
    // Check nonce ordering
    if new_nonce <= current_nonce {
      return Err(AuthError::InvalidNonce);
    }
  3. Balance Verification
    // Verify sufficient balance
    if channel.balance < required_amount {
      return Err(AuthError::InsufficientBalance);
    }

For API Consumers

  1. Private Key Security
    // Never expose private keys in code
    const privateKey = process.env.PRIVATE_KEY;
    if (!privateKey) {
      throw new Error("Missing private key");
    }
  2. Channel Monitoring
    // Regular balance checks
    const monitor = setInterval(() => {
      const state = pipeGate.getChannelState(channelId);
      if (state.balance < minimumBalance) {
        // Handle low balance
      }
    }, 60000);

Rate Limiting

use tokio::time::{Duration, Instant};
 
// Implement rate limiting
let mut last_request = Instant::now();
if last_request.elapsed() < Duration::from_secs(1) {
    return Err(AuthError::RateLimited);
}

Error Handling

try {
  await pipeGate.createPaymentChannel(params);
} catch (error) {
  if (error instanceof AuthError) {
    // Handle authentication errors
  } else if (error instanceof PaymentError) {
    // Handle payment errors
  } else {
    // Handle other errors
  }
}