@devgrid/bitcoin-core
Version:
Bitcoin Core API client for Node.js
500 lines • 16.4 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitcoinCore = void 0;
const axios_1 = __importDefault(require("axios"));
class BitcoinCore {
constructor(config) {
this.config = config;
this.url = this.config.url;
this.cache = new Map();
this.pendingRequests = [];
}
async getBestBlockhash() {
return this.callMethod('getbestblockhash');
}
async getBlock(params) {
return this.callMethod('getblock', params);
}
async getBlockchainInfo() {
return this.callMethod('getblockchaininfo');
}
async getBlockCount() {
return this.callMethod('getblockcount');
}
async getBlockFilter(params) {
return this.callMethod('getblockfilter', params);
}
async getBlockHash(params) {
return this.callMethod('getblockhash', params);
}
async getBlockHeader(params) {
return this.callMethod('getblockheader', params);
}
async getBlockStats(params) {
return this.callMethod('getblockstats', params);
}
async getChainTips() {
return this.callMethod('getchaintips');
}
async getChainTxStats(params) {
return this.callMethod('getchaintxstats', params);
}
async getDifficulty() {
return this.callMethod('getdifficulty');
}
async getMempoolAncestors(params) {
return this.callMethod('getmempoolancestors', params);
}
async getMempoolDescendants(params) {
return this.callMethod('getmempooldescendants', params);
}
async getMempoolEntry(params) {
return this.callMethod('getmempoolentry', params);
}
async getMempoolInfo() {
return this.callMethod('getmempoolinfo');
}
async getRawMempool(params) {
return this.callMethod('getrawmempool', params);
}
async getTxOut(params) {
return this.callMethod('gettxout', params);
}
async getTxOutProof(params) {
return this.callMethod('gettxoutproof', params);
}
async getTxOutSetInfo(params) {
return this.callMethod('gettxoutsetinfo', params);
}
async preciousBlock(params) {
return this.callMethod('preciousblock', params);
}
async pruneBlockChain(params) {
return this.callMethod('pruneblockchain', params);
}
async saveMempool() {
return this.callMethod('savemempool');
}
async scanTxOutSet(params) {
return this.callMethod('scantxoutset', params);
}
async verifyChain(params) {
return this.callMethod('verifychain', params);
}
async verifyTxOutProof(params) {
return this.callMethod('verifytxoutproof', params);
}
async getMemoryInfo(params) {
return this.callMethod('getmemoryinfo', params);
}
async getRpcInfo() {
return this.callMethod('getrpcinfo');
}
async help(params) {
return this.callMethod('help', params);
}
async logging(params) {
return this.callMethod('logging', params);
}
async stop() {
return this.callMethod('stop');
}
async uptime() {
return this.callMethod('uptime');
}
async generateBlock(params) {
return this.callMethod('generateblock', params);
}
async generateToAddress(params) {
return this.callMethod('generatetoaddress', params);
}
async generateToDescriptor(params) {
return this.callMethod('generatetodescriptor', params);
}
async getBlockTemplate(params) {
return this.callMethod('getblocktemplate', params);
}
async getMiningInfo() {
return this.callMethod('getmininginfo');
}
async getNetworkHashPs(params) {
return this.callMethod('getnetworkhashps', params);
}
async prioritizeTransaction(params) {
return this.callMethod('prioritisetransaction', params);
}
async submitBlock(params) {
return this.callMethod('submitblock', params);
}
async submitHeader(params) {
return this.callMethod('submitheader', params);
}
async addNode(params) {
return this.callMethod('addnode', params);
}
async clearBanned() {
return this.callMethod('clearbanned');
}
async disconnectNode(params) {
return this.callMethod('disconnectnode', params);
}
async getAddedNodeInfo(params) {
return this.callMethod('getaddednodeinfo', params);
}
async getConnectionCount() {
return this.callMethod('getconnectioncount');
}
async getNetTotals() {
return this.callMethod('getnettotals');
}
async getNetworkInfo() {
return this.callMethod('getnetworkinfo');
}
async getNodeAddresses(params) {
return this.callMethod('getnodeaddresses', params);
}
async getPeerInfo() {
return this.callMethod('getpeerinfo');
}
async listBanned() {
return this.callMethod('listbanned');
}
async ping() {
return this.callMethod('ping');
}
async setBan(params) {
return this.callMethod('setban', params);
}
async setNetworkActive(params) {
return this.callMethod('setnetworkactive', params);
}
async analyzePsbt(params) {
return this.callMethod('analyzepsbt', params);
}
async combinePsbt(params) {
return this.callMethod('combinepsbt', params);
}
async combineRawTransaction(params) {
return this.callMethod('combinerawtransaction', params);
}
async convertToPsbt(params) {
return this.callMethod('converttopsbt', params);
}
async createPsbt(params) {
return this.callMethod('createpsbt', params);
}
async createRawTransaction(params) {
return this.callMethod('createrawtransaction', params);
}
async decodePsbt(params) {
return this.callMethod('decodepsbt', params);
}
async decodeRawTransaction(params) {
return this.callMethod('decoderawtransaction', params);
}
async decodeScript(params) {
return this.callMethod('decodescript', params);
}
async finalizePsbt(params) {
return this.callMethod('finalizepsbt', params);
}
async fundRawTransaction(params) {
return this.callMethod('fundrawtransaction', params);
}
async getRawTransaction(params) {
return this.callMethod('getrawtransaction', params);
}
async joinPsbts(params) {
return this.callMethod('joinpsbts', params);
}
async sendRawTransaction(params) {
return this.callMethod('sendrawtransaction', params);
}
async signRawTransactionWithKey(params) {
return this.callMethod('signrawtransactionwithkey', params);
}
async testMempoolAccept(params) {
return this.callMethod('testmempoolaccept', params);
}
async utxoUpdatePsbt(params) {
return this.callMethod('utxoupdatepsbt', params);
}
async createMultisig(params) {
return this.callMethod('createmultisig', params);
}
async deriveAddresses(params) {
return this.callMethod('deriveaddresses', params);
}
async estimateSmartFee(params) {
return this.callMethod('estimatesmartfee', params);
}
async getDescriptorInfo(params) {
return this.callMethod('getdescriptorinfo', params);
}
async getIndexInfo(params) {
return this.callMethod('getindexinfo', params);
}
async signMessageWithPrivKey(params) {
return this.callMethod('signmessagewithprivkey', params);
}
async validateAddress(params) {
return this.callMethod('validateaddress', params);
}
async verifyMessage(params) {
return this.callMethod('verifymessage', params);
}
async abandonTransaction(params) {
return this.callMethod('abandontransaction', params);
}
async abortRescan() {
return this.callMethod('abortrescan');
}
async addMultiSigAddress(params) {
return this.callMethod('addmultisigaddress', params);
}
async backupWallet(params) {
return this.callMethod('backupwallet', params);
}
async bumpFee(params) {
return this.callMethod('bumpfee', params);
}
async createWallet(params) {
return this.callMethod('createwallet', params);
}
async dumpPrivKey(params) {
return this.callMethod('dumpprivkey', params);
}
async dumpWallet(params) {
return this.callMethod('dumpwallet', params);
}
async encryptWallet(params) {
return this.callMethod('encryptwallet', params);
}
async getAddressesByLabel(params) {
return this.callMethod('getaddressesbylabel', params);
}
async getAddressInfo(params) {
return this.callMethod('getaddressinfo', params);
}
async getBalance(params) {
return this.callMethod('getbalance', params);
}
async getBalances() {
return this.callMethod('getbalances');
}
async getNewAddress(params, options) {
return this.callMethod('getnewaddress', params, options);
}
async getRawChangeAddress(params) {
return this.callMethod('getrawchangeaddress', params);
}
async getReceivedByAddress(params) {
return this.callMethod('getreceivedbyaddress', params);
}
async getReceivedByLabel(params) {
return this.callMethod('getreceivedbylabel', params);
}
async getTransaction(params) {
return this.callMethod('gettransaction', params);
}
async getUnconfirmedBalance() {
return this.callMethod('getunconfirmedbalance');
}
async getWalletInfo() {
return this.callMethod('getwalletinfo');
}
async importAddress(params) {
return this.callMethod('importaddress', params);
}
async importDescriptors(params) {
return this.callMethod('importdescriptors', params);
}
async importMulti(params) {
return this.callMethod('importmulti', params);
}
async importPrivKey(params) {
return this.callMethod('importprivkey', params);
}
async importPrunedFunds(params) {
return this.callMethod('importprunedfunds', params);
}
async importPubKey(params) {
return this.callMethod('importpubkey', params);
}
async importWallet(params) {
return this.callMethod('importwallet', params);
}
async keyPoolRefill(params) {
return this.callMethod('keypoolrefill', params);
}
async listAddressGroupings() {
return this.callMethod('listaddressgroupings');
}
async listLabels(params) {
return this.callMethod('listlabels', params);
}
async listLockUnspent() {
return this.callMethod('listlockunspent');
}
async listReceivedByAddress(params) {
return this.callMethod('listreceivedbyaddress', params);
}
async listReceivedByLabel(params) {
return this.callMethod('listreceivedbylabel', params);
}
async listSinceBlock(params) {
return this.callMethod('listsinceblock', params);
}
async listTransactions(params) {
return this.callMethod('listtransactions', params);
}
async listUnspent(params) {
return this.callMethod('listunspent', params);
}
async listWalletDir() {
return this.callMethod('listwalletdir');
}
async listWallets() {
return this.callMethod('listwallets');
}
async loadWallet(params) {
return this.callMethod('loadwallet', params);
}
async lockUnspent(params) {
return this.callMethod('lockunspent', params);
}
async psbtBumpFee(params) {
return this.callMethod('psbtbumpfee', params);
}
async removePrunedFunds(params) {
return this.callMethod('removeprunedfunds', params);
}
async rescanBlockchain(params) {
return this.callMethod('rescanblockchain', params);
}
async send(params) {
return this.callMethod('send', params);
}
async sendMany(params) {
return this.callMethod('sendmany', params);
}
async sendToAddress(params) {
return this.callMethod('sendtoaddress', params);
}
async setHdSeed(params) {
return this.callMethod('sethdseed', params);
}
async setLabel(params) {
return this.callMethod('setlabel', params);
}
async setTxFee(params) {
return this.callMethod('settxfee', params);
}
async setWalletFlag(params) {
return this.callMethod('setwalletflag', params);
}
async signMessage(params) {
return this.callMethod('signmessage', params);
}
async signRawTransactionWithWallet(params) {
return this.callMethod('signrawtransactionwithwallet', params);
}
async unloadWallet(params) {
return this.callMethod('unloadwallet', params);
}
async upgradeWallet(params) {
return this.callMethod('upgradewallet', params);
}
async walletCreateFundedPsbt(params) {
return this.callMethod('walletcreatefundedpsbt', params);
}
async walletLock() {
return this.callMethod('walletlock');
}
async walletPassphrase(params) {
return this.callMethod('walletpassphrase', params);
}
async walletPassphraseChange(params) {
return this.callMethod('walletpassphrasechange', params);
}
async walletProcessPsbt(params) {
return this.callMethod('walletprocesspsbt', params);
}
async callMethod(method, params, options) {
const cacheKey = JSON.stringify({ method, params, options });
if (this.cache.has(cacheKey)) {
return this.cache.get(cacheKey);
}
const makeRequest = async () => {
try {
const response = await this.makeRPCCall(method, params, options);
const result = response.data.result;
this.cache.set(cacheKey, result);
return result;
}
catch (error) {
if (error.response) {
throw new Error(`BitcoinCore RPC Error: ${error.response.data.error.message} (Code: ${error.response.data.error.code})`);
}
else if (error.request) {
throw new Error(`BitcoinCore Connection Error: No response from server`);
}
else {
throw new Error(`BitcoinCore Request Error: ${error.message}`);
}
}
};
try {
return await makeRequest();
}
catch (error) {
if (error.message.includes('BitcoinCore Connection Error')) {
this.pendingRequests.push({ method, params, options });
await this.waitForNode();
return this.callMethod(method, params, options);
}
else {
throw error;
}
}
}
async waitForNode() {
while (true) {
try {
console.log('Attempting to connect to the node...');
await this.makeRPCCall("ping");
console.log('Connection successful.');
break;
}
catch {
console.log('Failed to connect to the node. Retrying in 5 seconds...');
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
while (this.pendingRequests.length > 0) {
const request = this.pendingRequests.shift();
if (request) {
await this.callMethod(request.method, request.params, request.options);
}
}
}
makeRPCCall(method, params, options) {
const auth = {
username: this.config.username,
password: this.config.password,
};
const wallet = options?.wallet ?? this.config.wallet;
const url = wallet ? `${this.url}/wallet/${wallet}` : this.url;
return axios_1.default.post(url, {
jsonrpc: '1.0',
id: `${Date.now()}`,
method,
params: params ?? [],
}, { auth });
}
}
exports.BitcoinCore = BitcoinCore;
//# sourceMappingURL=bitcoin-core.js.map