230 lines
4.8 KiB
Markdown
230 lines
4.8 KiB
Markdown
# Dragonchain Node.js SDK
|
|
|
|
Official Node.js and TypeScript SDK for interacting with Dragonchain blockchain nodes.
|
|
|
|
## Features
|
|
|
|
- 🔒 **HMAC-SHA256 Authentication** - Secure request signing
|
|
- 📦 **Full TypeScript Support** - Complete type definitions included
|
|
- 🌐 **Dual Package** - Works with both ESM and CommonJS
|
|
- ✅ **Comprehensive Testing** - Thoroughly tested with Jest
|
|
- 🎯 **Modern Best Practices** - Built with latest Node.js standards
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install @dragonchain-inc/prime-sdk
|
|
```
|
|
|
|
Or with yarn:
|
|
|
|
```bash
|
|
yarn add @dragonchain-inc/prime-sdk
|
|
```
|
|
|
|
## Quick Start
|
|
|
|
```typescript
|
|
import { DragonchainSDK } from '@dragonchain-inc/prime-sdk';
|
|
|
|
// Initialize the SDK
|
|
const sdk = new DragonchainSDK(
|
|
'your-public-id',
|
|
'your-auth-key-id',
|
|
'your-auth-key',
|
|
'https://your-dragonchain-endpoint.com'
|
|
);
|
|
|
|
// Check system health
|
|
await sdk.system.health();
|
|
|
|
// Get system status
|
|
const status = await sdk.system.status();
|
|
console.log(`Chain ID: ${status.id}, Level: ${status.level}`);
|
|
|
|
// Create a transaction
|
|
const txnResponse = await sdk.transaction.create({
|
|
txn_type: 'my-transaction-type',
|
|
payload: JSON.stringify({ message: 'Hello Dragonchain' }),
|
|
tag: 'example-tag',
|
|
});
|
|
console.log(`Created transaction: ${txnResponse.transaction_id}`);
|
|
```
|
|
|
|
## Using Configuration Files
|
|
|
|
The SDK supports loading credentials from YAML configuration files:
|
|
|
|
```yaml
|
|
default: my-chain-id
|
|
chains:
|
|
- name: my-chain
|
|
publicId: my-chain-id
|
|
authKeyId: my-auth-key-id
|
|
authKey: my-auth-key
|
|
endpoint: https://mychain.dragonchain.com
|
|
```
|
|
|
|
```typescript
|
|
import { DragonchainSDK } from '@dragonchain-inc/prime-sdk';
|
|
import { loadConfig, getDefaultChain } from '@dragonchain-inc/prime-sdk';
|
|
|
|
// Load config from file
|
|
const config = loadConfig('~/.dragonchain/credentials.yaml');
|
|
const chain = getDefaultChain(config);
|
|
|
|
if (chain) {
|
|
const sdk = new DragonchainSDK(
|
|
chain.publicId,
|
|
chain.authKeyId,
|
|
chain.authKey,
|
|
chain.endpoint
|
|
);
|
|
}
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### System
|
|
|
|
```typescript
|
|
// Check system health
|
|
await sdk.system.health();
|
|
|
|
// Get system status
|
|
const status = await sdk.system.status();
|
|
```
|
|
|
|
### Transactions
|
|
|
|
```typescript
|
|
// Create a transaction
|
|
const response = await sdk.transaction.create({
|
|
txn_type: 'my-type',
|
|
payload: JSON.stringify({ data: 'example' }),
|
|
tag: 'optional-tag',
|
|
});
|
|
|
|
// Create multiple transactions
|
|
const bulkResponse = await sdk.transaction.createBulk({
|
|
transactions: [
|
|
{ txn_type: 'type1', payload: 'data1' },
|
|
{ txn_type: 'type2', payload: 'data2' },
|
|
],
|
|
});
|
|
|
|
// Get a transaction by ID
|
|
const txn = await sdk.transaction.get('transaction-id');
|
|
|
|
// List all transactions
|
|
const txns = await sdk.transaction.list();
|
|
```
|
|
|
|
### Transaction Types
|
|
|
|
```typescript
|
|
// Create a transaction type
|
|
await sdk.transactionType.create({
|
|
txn_type: 'my-new-type',
|
|
});
|
|
|
|
// Get a transaction type
|
|
const txnType = await sdk.transactionType.get('my-type');
|
|
|
|
// List all transaction types
|
|
const types = await sdk.transactionType.list();
|
|
|
|
// Delete a transaction type
|
|
await sdk.transactionType.delete('my-type');
|
|
```
|
|
|
|
### Blocks
|
|
|
|
```typescript
|
|
// Get a block by ID
|
|
const block = await sdk.block.get('block-id');
|
|
```
|
|
|
|
## TypeScript Support
|
|
|
|
This SDK is written in TypeScript and includes complete type definitions:
|
|
|
|
```typescript
|
|
import {
|
|
DragonchainSDK,
|
|
TransactionCreateRequest,
|
|
TransactionCreateResponse,
|
|
SystemStatus,
|
|
} from '@dragonchain-inc/prime-sdk';
|
|
|
|
const sdk = new DragonchainSDK(
|
|
publicId,
|
|
authKeyId,
|
|
authKey,
|
|
baseURL
|
|
);
|
|
|
|
// Full type safety
|
|
const request: TransactionCreateRequest = {
|
|
txn_type: 'my-type',
|
|
payload: JSON.stringify({ foo: 'bar' }),
|
|
};
|
|
|
|
const response: TransactionCreateResponse = await sdk.transaction.create(request);
|
|
```
|
|
|
|
## Authentication
|
|
|
|
The SDK uses HMAC-SHA256 authentication with the following components:
|
|
|
|
1. **Public ID** - Your Dragonchain public identifier
|
|
2. **Auth Key ID** - Your authentication key identifier
|
|
3. **Auth Key** - Your secret authentication key
|
|
4. **Endpoint** - The base URL of your Dragonchain node
|
|
|
|
Each request is signed with:
|
|
- `Authorization` header: `DC1-HMAC-SHA256 {authKeyId}:{signature}`
|
|
- `Dragonchain` header: Your public ID
|
|
- `Timestamp` header: Unix timestamp of the request
|
|
|
|
## Development
|
|
|
|
```bash
|
|
# Install dependencies
|
|
npm install
|
|
|
|
# Run tests
|
|
npm test
|
|
|
|
# Run tests with coverage
|
|
npm run test:coverage
|
|
|
|
# Build the package
|
|
npm run build
|
|
|
|
# Lint the code
|
|
npm run lint
|
|
|
|
# Format the code
|
|
npm run format
|
|
```
|
|
|
|
## Requirements
|
|
|
|
- Node.js >= 16.0.0
|
|
- TypeScript >= 5.0.0 (for TypeScript projects)
|
|
|
|
## License
|
|
|
|
Apache-2.0
|
|
|
|
## Support
|
|
|
|
For issues and questions, please visit:
|
|
- Repository: https://git.dragonchain.com/dragonchain/dragonchain-prime-node-sdk
|
|
- Documentation: https://dragonchain-core-docs.dragonchain.com
|
|
|
|
## Contributing
|
|
|
|
Contributions are welcome! Please feel free to submit a Pull Request.
|