@devgrid/bitcoin-core
Version:
Bitcoin Core API client for Node.js
168 lines (126 loc) • 3.11 kB
Markdown
A strongly-typed Bitcoin Core RPC client for Node.js with full TypeScript support.
- 🚀 Full TypeScript support with comprehensive type definitions
- 💪 Strong type checking for all RPC methods and responses
- 🔒 Secure connection handling with SSL/TLS support
- ⚡ Promise-based API
- 🔄 Automatic request batching
- 🎯 Detailed error handling
- 📝 Extensive documentation
```bash
npm install @devgrid/bitcoin-core
yarn add @devgrid/bitcoin-core
```
```typescript
import { BitcoinCore } from '@devgrid/bitcoin-core';
const client = new BitcoinCore({
host: 'localhost',
port: 8332,
username: 'your-username',
password: 'your-password',
ssl: true
});
// Get blockchain info
const info = await client.getBlockchainInfo();
console.log(info);
// Get transaction by ID
const tx = await client.getTransaction('txid');
console.log(tx);
```
```typescript
interface BitcoinConfig {
host: string;
port: number;
username: string;
password: string;
ssl?: boolean;
timeout?: number;
headers?: Record<string, string>;
}
```
- `getBlockchainInfo()`: Get blockchain info
- `getBlock(hash: string)`: Get block by hash
- `getBlockHash(height: number)`: Get block hash by height
- `getBlockCount()`: Get current block count
- `getTransaction(txid: string)`: Get transaction details
- `sendTransaction(hex: string)`: Send raw transaction
- `decodeTransaction(hex: string)`: Decode raw transaction
- `getBalance()`: Get wallet balance
- `listUnspent()`: List unspent transactions
- `sendToAddress(address: string, amount: number)`: Send to address
```typescript
try {
await client.getTransaction('invalid-txid');
} catch (error) {
if (error.code === RPCErrorCode.INVALID_PARAMS) {
console.error('Invalid transaction ID');
}
}
```
```typescript
const batch = client.batch();
batch.getBlockCount();
batch.getBlockchainInfo();
const [count, info] = await batch.execute();
```
```typescript
const client = new BitcoinCore({
// ... other config
headers: {
'User-Agent': 'MyApp/1.0.0'
}
});
```
```typescript
const client = new BitcoinCore({
// ... other config
ssl: true,
sslOptions: {
ca: fs.readFileSync('ca.pem'),
cert: fs.readFileSync('cert.pem'),
key: fs.readFileSync('key.pem')
}
});
```
```typescript
const client = new BitcoinCore({
// ... other config
timeout: 30000 // 30 seconds
});
```
| Code | Description |
|------|-------------|
| -32600 | Invalid Request |
| -32601 | Method not found |
| -32602 | Invalid params |
| -32603 | Internal error |
| -32700 | Parse error |
Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) first.
```bash
npm test
yarn test
```
MIT
Built with TypeScript and Node.js.