btcd-client
Version:
A bitcoin core json rpc client
70 lines (69 loc) • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitcoinCoreClient = void 0;
const axios_1 = __importDefault(require("axios"));
class BitcoinCoreClient {
constructor(credentials) {
this.client = axios_1.default.create({
baseURL: credentials.url,
auth: {
username: credentials.username,
password: credentials.password,
},
});
this.id = 0;
}
async makeRequest(method, params = [], walletName = '') {
this.id++;
const data = {
jsonrpc: '1.0',
id: this.id,
method,
params,
};
try {
let url = '';
if (walletName !== '')
url = `/wallet/${walletName}`;
const response = await this.client.post(url, data);
return response.data.result;
}
catch (error) {
throw error.response.data.error;
}
}
async getBlockchainInfo() {
return await this.makeRequest('getblockchaininfo');
}
async getWalletInfo(walletName) {
return await this.makeRequest('getwalletinfo', [], walletName);
}
async getNewAddress(walletName) {
return await this.makeRequest('getnewaddress', [], walletName);
}
async getTransaction(walletName, params) {
return await this.makeRequest('gettransaction', params, walletName);
}
async listTransactions(walletName) {
return await this.makeRequest('listtransactions', [], walletName);
}
async listWallets() {
return await this.makeRequest('listwallets');
}
async restoreWallet(walletName, params) {
return await this.makeRequest('restorewallet', params, walletName);
}
async sendToAddress(walletName, params) {
return await this.makeRequest('sendtoaddress', params, walletName);
}
async unloadWallet(walletName, params) {
return await this.makeRequest('unloadwallet', params, walletName);
}
async walletPassphrase(walletName, params) {
return await this.makeRequest('walletpassphrase', params, walletName);
}
}
exports.BitcoinCoreClient = BitcoinCoreClient;