Skip to content

API Documentation

SecureBitChat edited this page Aug 18, 2025 · 1 revision

API Documentation

πŸ”Œ Core API

Connection Management

createConnection(config)

Creates a new P2P connection.

Parameters:

  • config (Object): Connection configuration
    • iceServers (Array): STUN/TURN servers
    • timeout (Number): Connection timeout in ms

Returns: Promise

Example:


#### `generateKeyPair()`
Generates a new ECDH key pair.

**Returns:** Promise<CryptoKeyPair>

#### `encryptMessage(message, key)`
Encrypts a message using AES-GCM.

**Parameters:**
- `message` (String): Message to encrypt
- `key` (CryptoKey): Encryption key

**Returns:** Promise<Object> - { encrypted, iv }

#### `decryptMessage(encrypted, key, iv)`
Decrypts a message using AES-GCM.

**Parameters:**
- `encrypted` (ArrayBuffer): Encrypted data
- `key` (CryptoKey): Decryption key
- `iv` (Uint8Array): Initialization vector

**Returns:** Promise<String>

### Payment API

#### `createSession(type)`
Creates a new payment session.

**Parameters:**
- `type` (String): 'demo', 'basic', or 'premium'

**Returns:** Promise<Session>

#### `processPayment(invoice)`
Processes Lightning Network payment.

**Parameters:**
- `invoice` (String): Lightning invoice

**Returns:** Promise<PaymentResult>

## πŸ”§ Configuration API

### `configure(options)`
Configures SecureBit.chat instance.

**Parameters:**
- `options` (Object): Configuration options
  - `lightningNode` (String): Lightning node URL
  - `stunServers` (Array): STUN server URLs
  - `turnServers` (Array): TURN server URLs
  - `sessionTimeout` (Number): Session timeout in ms

**Example:**
```javascript
configure({
  lightningNode: 'https://your-node.com',
  stunServers: ['stun:stun.l.google.com:19302'],
  sessionTimeout: 3600000
});

πŸ“‘ Events API

Event Listeners

on('connection', callback)

Fired when connection is established.

on('message', callback)

Fired when message is received.

on('payment', callback)

Fired when payment is processed.

on('error', callback)

Fired when error occurs.

Example:

on('connection', (connection) => {
  console.log('Connected:', connection.id);
});

on('message', (message) => {
  console.log('Received:', message.text);
});

πŸ› οΈ Error Handling

Error Types

  • ConnectionError: Connection-related errors
  • CryptoError: Cryptographic operation errors
  • PaymentError: Payment processing errors
  • SessionError: Session management errors

Error Handling Example

try {
  const connection = await createConnection(config);
} catch (error) {
  if (error instanceof ConnectionError) {
    console.error('Connection failed:', error.message);
  } else if (error instanceof CryptoError) {
    console.error('Crypto error:', error.message);
  }
}
Clone this wiki locally