coauthored with @MichaelTaylor3d
Package for managing connections to Chia RPC services. Intended to encapsulate finding CHIA_ROOT, implement environment chia conventions, and provide a simple interface for describing the connection with the service. This simplifies finding the cert files, in a reusable manner, decoupled from aby specific communication library or pattern.
createChiaConnection
and createChiaConnectionFromConfig
are the main entry points for the package. They return a ChiaConnection
object that can be used with a https
or wss
library to make RPC calls to the service. Unless specified bt the caller, all methods respect the CHIA_ROOT
environment variable or default to ~/.chia/mainnet
.
This creates a connection to the wallet service using the default connection parameters, localhost
and ~/.chia/mainnet/
.
const connector = require('chia-service-connector');
const connection = connector.createChiaConnection("wallet");
const configConnection = connector.createChiaConnectionFromConfig("wallet");
Create a connection to the chia daemon on another machine, using the default ~/.chia/mainnet/
location for certs.
const connector = require('chia-service-connector');
const connection = connector.createChiaConnection("daemon", "192.168.1.155");
const configConnection = connector.createChiaConnectionFromConfig("daemon", "~/path/to/some/config.yaml");
const connector = require('chia-service-connector');
const connection = connector.createChiaConnection(
"full_node", // service name
"192.168.1.155", // host (name or ip)
connector.getChiaRoot(), // getChiaRoot will try to find the current CHIA_ROOT
60, // timeout in seconds
{ "full_node": 8765 } // map of service names to ports - defaults to the standard ports
);
The ChiaConnection
object can be created directly, but it is recommended to use create methods instead since they accommodate the most environment and conventional values.
const connector = require('chia-service-connector');
const connection = new connector.ChiaConnection(
"full_node", // service name
"192.168.1.155", // host (name or ip)
9812, // port
"~/.chia/mainnet/config/ssl/wallet/private_wallet.key", // full path to the cert file
"~/.chia/mainnet/config/ssl/wallet/private_wallet.crt", // full path to the key file
60, // timeout in seconds
);
service
- the service namehost
- the host name or ip addressport
- the port numbertimeout_seconds
- the timeout in seconds for the connectioncert_path
- the full path to the cert filekey_path
- the full path to the key filecert
- the contents of the cert file as aBuffer
key
- the contents of the key file as aBuffer
serviceAddress
- the formatted address to the service, including protocol, host, and portcreateClientOptions()
- a function that returns an object that can be used with thehttps
orwss
libraries orhttps agent
The ChiaConnection
keeps the details that can be used with your favorite https
library.
const connector = require('chia-service-connector');
const https = require('https');
const axiosLib = require('axios');
const superagent = require('superagent');
const connection = connector.createChiaConnection("wallet");
const axios = axiosLib.create({
baseURL: connection.serviceAddress,
timeout: connection.timeout_seconds * 1000,
headers: {
accepts: "application/json",
"content-type": "application/json",
},
httpsAgent: new https.Agent(
connection.createClientOptions()
),
});
const response = await superagent
.post(`${connection.serviceAddress}/get_sync_status`)
.send({})
.timeout(timeout)
.agent(new https.Agent(connection.createClientOptions()));
Or web-sockets
const connector = require('chia-service-connector');
const ws = require("ws");
const connection = connector.createChiaConnection("daemon");
const socket = new ws.WebSocket(
connection.serviceAddress,
connection.createClientOptions()
);
Or the chia-daemon package
import { createChiaConnection, createChiaConnectionFromConfig } from 'chia-service-connector';
import { ChiaDaemon, ChiaHttps } from 'chia-daemon';
const daemonConnection = connector.createChiaConnection("daemon");
const daemon = new ChiaDaemon(daemonConnection);
await demon.connect();
console.log(await daemon.services.full_node.get_blockchain_state())
const walletConnection = connector.createChiaConnectionFromConfig("wallet");
const wallet = new ChiaHttps(walletConnection);
console.log(await wallet.get_wallet_balance({ wallet_id: 1}))
Or whatever other chia library you like.