UNPKG

btcrpc

Version:
52 lines (51 loc) 2.09 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import fetch from 'node-fetch'; class BTCRPC { constructor(config) { if (!config.host) config.host = 'localhost'; if (!config.port) config.port = 8332; this.config = config; } exec(methodName, params) { return __awaiter(this, void 0, void 0, function* () { if (!params) params = []; const url = 'http://' + this.config.host + ':' + this.config.port + '/'; const data = { jsonrpc: '1.0', id: 'btcrpc', method: methodName, params: params, }; const dataStr = JSON.stringify(data); const req = yield fetch(url, { method: 'POST', headers: { Host: this.config.host, Authorization: Buffer.from(this.config.user + ':' + this.config.pass).toString('base64'), 'Content-Length': dataStr.length, }, body: dataStr, }); const res = yield req.text(); return res; }); } getBlockCount() { return __awaiter(this, void 0, void 0, function* () { return yield this.exec('getblockcount', []); }); } } export default BTCRPC; module.exports = BTCRPC;