Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 209 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,124 +1,256 @@
# RGB API SDK
# RGB API JavaScript SDK

This is a JavaScript/TypeScript SDK for interacting with the RGB Lightning Node API.
A JavaScript/TypeScript SDK for interacting with RGB Lightning Node APIs. This SDK provides a simple and intuitive interface for managing RGB assets, lightning network operations, and on-chain transactions.

## Architecture Overview
## Features

The SDK follows a modular design with the following main structure:

- `src/index.js`: The entry point of the SDK, exporting `RgbApiClient`.
- `src/client.js`: Contains the core `RgbApiClient` class, responsible for handling API requests and responses.
- `src/modules/`: Contains sub-modules organized by functionality (e.g., `node.js`, `onchain.js`, `rgb.js`, `lightning.js`, `swaps.js`), with each module exposing a set of related API methods.
- `src/types.d.ts`: Provides comprehensive TypeScript type definitions for use in TypeScript projects.
- **Modular Architecture**: Organized into logical modules (node, onchain, RGB, lightning, swaps)
- **Full TypeScript Support**: Complete type definitions for all APIs
- **Promise-based**: Modern async/await API
- **Error Handling**: Comprehensive error handling with meaningful messages
- **Flexible Configuration**: Support for custom headers, base URLs, and axios configurations

## Installation

You can install this SDK using npm or yarn:

```bash
npm install your-package-name # Replace with your package name
# or
yarn add your-package-name # Replace with your package name
npm install @lnfi-network/rgb-api-js-sdk
```

## Usage
or

```bash
yarn add @lnfi-network/rgb-api-js-sdk
```

First, import and instantiate `RgbApiClient`:
## Quick Start

```javascript
import { RgbApiClient } from 'your-package-name'; // Replace with your package name
import { RgbApiClient } from '@lnfi-network/rgb-api-js-sdk';

// Initialize the client
const client = new RgbApiClient({
baseUrl: 'http://localhost:3001', // Modify according to your node address
// If an API key is required, you can add headers here
// headers: { 'Authorization': 'Bearer your_api_key' }
baseUrl: 'http://localhost:3001'
});

// Check node status
const nodeInfo = await client.node.getNodeInfo();
console.log('Node Info:', nodeInfo);

// List RGB assets
const assets = await client.rgb.listAssets();
console.log('Assets:', assets);
```

Then, you can access methods of various modules through the client instance:
## Configuration

### Basic Configuration

```javascript
const client = new RgbApiClient({
baseUrl: 'http://localhost:3001'
});
```

### Node Methods (NodeMethods)
### Advanced Configuration

```javascript
async function getNodeStatus() {
try {
const nodeInfo = await client.node.getNodeInfo();
console.log('Node Info:', nodeInfo);

const nodeState = await client.node.getNodeState();
console.log('Node State:', nodeState); // 0: NON_EXISTING, 1: LOCKED, 4: SERVER_ACTIVE

const networkInfo = await client.node.getNetworkInfo();
console.log('Network Info:', networkInfo);

// ... Other node methods
await client.node.checkIndexerUrl({ indexer_url: 'electrs:50001' });
console.log('Indexer URL check successful.');

await client.node.checkProxyEndpoint({ proxy_endpoint: 'rpc://34.84.66.29:5000/json-rpc' });
console.log('Proxy endpoint check successful.');

const signedMessage = await client.node.signMessage({ message: 'Hello RGB' });
console.log('Signed Message:', signedMessage);

// Note: Calling lockNode will lock the node and affect subsequent calls
// await client.node.lockNode();
// console.log('Node locked.');

} catch (error) {
console.error('Error interacting with node:', error);
const client = new RgbApiClient({
baseUrl: 'https://your-rgb-node.com',
headers: {
'Authorization': 'Bearer your-api-key',
'Custom-Header': 'value'
},
axiosConfig: {
timeout: 10000,
proxy: false
}
}
});
```

## API Modules

getNodeStatus();
### Node Operations

```javascript
// Get node information
const nodeInfo = await client.node.getNodeInfo();

// Check node state (0: NON_EXISTING, 1: LOCKED, 4: SERVER_ACTIVE)
const state = await client.node.getNodeState();

// Get network information
const networkInfo = await client.node.getNetworkInfo();

// Validate indexer URL
await client.node.checkIndexerUrl({
indexer_url: 'electrs:50001'
});

// Sign a message
const signature = await client.node.signMessage({
message: 'Hello RGB'
});
```

### Other Modules
### RGB Asset Operations

You can similarly access methods of other modules:
```javascript
// List all assets
const assets = await client.rgb.listAssets();

// Get asset balance
const balance = await client.rgb.getAssetBalance({
asset_id: 'rgb1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nqcg'
});

// Get asset metadata
const metadata = await client.rgb.getAssetMetadata({
asset_id: 'rgb1qyfe883hey6jrgj2xvk5g3dfmfqfzm7a4wez4pd2krf7ltsxffd6u6nqcg'
});

// Send assets
await client.rgb.sendAssets({
recipient_map: {
'asset_id': [{
recipient: 'recipient_address',
amount: 1000
}]
}
});
```

### Lightning Network Operations

```javascript
// On-chain Methods
// await client.onchain.getAddress();
// List channels
const channels = await client.lightning.listChannels();

// RGB Methods
// await client.rgb.listAssets();
// Create invoice
const invoice = await client.lightning.createInvoice({
amount_msat: 10000,
description: 'Payment for services'
});

// Lightning Methods
// await client.lightning.listChannels();
// Decode RGB Lightning invoice
const decoded = await client.lightning.decodeRGBLNInvoice(
'lnbcrt30u1p5f9szd...'
);

// Swaps Methods
// await client.swaps.listSwaps();
// Pay invoice
await client.lightning.payInvoice({
invoice: 'lnbcrt30u1p5f9szd...'
});
```

(Please refer to the files in the `src/modules/` directory and `src/types.d.ts` for detailed information and parameters of available methods in each module.)
### On-chain Operations

```javascript
// Get new address
const address = await client.onchain.getAddress();

// List transactions
const transactions = await client.onchain.listTransactions();

// Send Bitcoin
await client.onchain.sendBtc({
address: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
amount: 100000 // satoshis
});
```

### Swap Operations

```javascript
// List swaps
const swaps = await client.swaps.listSwaps();

// Create swap
await client.swaps.createSwap({
// swap parameters
});
```

## TypeScript Support

The SDK provides comprehensive TypeScript type definitions (`types.d.ts`), allowing you to benefit from type checking and auto-completion in your TypeScript projects.
The SDK includes comprehensive TypeScript definitions:

```typescript
import { RgbApiClient } from 'your-package-name'; // Replace with your package name
import { NodeState } from 'your-package-name/types'; // Import types, adjust path as needed
import { RgbApiClient, NodeState, AssetSchema } from '@lnfi-network/rgb-api-js-sdk';

const client = new RgbApiClient({
baseUrl: 'http://localhost:3001'
});

// Type-safe operations
const state: number = await client.node.getNodeState();
if (state === 4) { // SERVER_ACTIVE
console.log('Node is active and ready');
}

// Filter assets by schema
const assets = await client.rgb.listAssets({
filter_asset_schemas: [AssetSchema.NIA, AssetSchema.CFA]
});
```

## Error Handling

const client = new RgbApiClient({ baseUrl: 'http://localhost:3001' });
The SDK provides structured error handling:

async function checkState() {
const state: NodeState = await client.node.getNodeState();
if (state === NodeState.SERVER_ACTIVE) {
console.log('Node is active.');
```javascript
try {
const nodeInfo = await client.node.getNodeInfo();
} catch (error) {
if (error.message.includes('Network Error')) {
console.error('Unable to connect to RGB node');
} else if (error.message.includes('API Error')) {
console.error('API returned an error:', error.message);
} else {
console.error('Unexpected error:', error.message);
}
}
```

## Development

### Building

```bash
npm run build
```

### Testing

```bash
# Run all tests
npm test

checkState();
# Run specific test file
npm test test/rgb.integration.test.js
```

## Development and Testing
### Development Mode

```bash
npm run dev
```

## Examples

Check the `examples/` directory for complete usage examples:

- [Node.js Example](./examples/nodejs/index.js)

## API Reference

For detailed API documentation, refer to:
- Type definitions: [`src/types.d.ts`](./src/types.d.ts)
- Module implementations: [`src/modules/`](./src/modules/)

## License

(Add instructions on how to build, run unit tests, and integration tests here)
MIT

---
example:
npm test test/onchain.test.js
## Contributing

Please further refine this README file based on your actual package name and specific API endpoints, request/response structures.
Contributions are welcome! Please feel free to submit a Pull Request.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lnfi-network/rgb-api-js-sdk",
"version": "1.0.1",
"version": "1.0.2",
"description": "RGB API SDK for RGB Lightning Node",
"main": "dist/rgb-api-sdk.cjs",
"module": "dist/rgb-api-sdk.mjs",
Expand Down
Loading