btcrpc
Version:
Bitcoin RPC Wrapper
58 lines (57 loc) • 2.42 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_fetch_1 = __importDefault(require("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 (0, node_fetch_1.default)(url, {
method: 'POST',
headers: {
Host: this.config.host,
Authorization: Buffer.from(this.config.user + ':' + this.config.pass).toString('base64'),
'Content-Length': dataStr.length,
'Content-Type': 'application/json',
},
body: dataStr,
});
const res = yield req.text();
return res;
});
}
getBlockCount() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.exec('getblockcount', []);
});
}
}
exports.default = BTCRPC;
module.exports = BTCRPC;