xud
Version:
Exchange Union Daemon
133 lines • 5.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 });
/* tslint:disable no-null-keyword */
const grpc_1 = require("grpc");
const xudrpc = __importStar(require("../proto/xudrpc_pb"));
const getGrpcError_1 = __importDefault(require("./getGrpcError"));
class GrpcInitService {
constructor() {
this.disabled = false;
this.setInitService = (initService) => {
this.initService = initService;
};
/** Disables the grpc initialization service once xud has been intialized. */
this.disable = () => {
this.disabled = true;
this.initService = undefined;
};
/**
* Checks whether this service is ready to handle calls and sends an error to the client
* caller if not ready.
* @returns `true` if the service is ready, otherwise `false`
*/
this.isReady = (initService, callback) => {
if (!initService) {
const err = this.disabled ?
{ code: grpc_1.status.UNIMPLEMENTED, message: 'xud init service is disabled', name: 'DisabledError' } :
{ code: grpc_1.status.UNAVAILABLE, message: 'xud is starting', name: 'NotReadyError' };
callback(err, null);
return false;
}
return true;
};
/**
* See [[InitService.createNode]]
*/
this.createNode = (call, callback) => __awaiter(this, void 0, void 0, function* () {
if (!this.isReady(this.initService, callback)) {
return;
}
try {
const { mnemonic, initializedLndWallets, initializedConnext } = yield this.initService.createNode(call.request.toObject());
const response = new xudrpc.CreateNodeResponse();
if (mnemonic) {
response.setSeedMnemonicList(mnemonic);
}
response.setInitializedConnext(initializedConnext);
response.setInitializedLndsList(initializedLndWallets);
callback(null, response);
}
catch (err) {
callback(getGrpcError_1.default(err), null);
}
});
/**
* See [[InitService.unlockNode]]
*/
this.unlockNode = (call, callback) => __awaiter(this, void 0, void 0, function* () {
if (!this.isReady(this.initService, callback)) {
return;
}
try {
const unlockNodeResult = yield this.initService.unlockNode(call.request.toObject());
const response = new xudrpc.UnlockNodeResponse();
response.setUnlockedLndsList(unlockNodeResult.unlockedLndClients);
response.setLockedLndsList(unlockNodeResult.lockedLndClients);
callback(null, response);
}
catch (err) {
callback(getGrpcError_1.default(err), null);
}
});
/**
* See [[InitService.restoreNode]]
*/
this.restoreNode = (call, callback) => __awaiter(this, void 0, void 0, function* () {
if (!this.isReady(this.initService, callback)) {
return;
}
try {
const password = call.request.getPassword();
const lndBackupsMap = call.request.getLndBackupsMap();
const seedMnemonicList = call.request.getSeedMnemonicList();
const xudDatabase = call.request.getXudDatabase_asU8();
const { initializedConnext, initializedLndWallets } = yield this.initService.restoreNode({
password,
seedMnemonicList,
xudDatabase,
lndBackupsMap,
});
const response = new xudrpc.RestoreNodeResponse();
response.setRestoredConnext(initializedConnext);
response.setRestoredLndsList(initializedLndWallets);
callback(null, response);
}
catch (err) {
callback(getGrpcError_1.default(err), null);
}
});
}
}
exports.default = GrpcInitService;
//# sourceMappingURL=GrpcInitService.js.map