itns-sidechain/lib/wallet/node.js

140 lines
3.1 KiB
JavaScript
Raw Normal View History

/*!
* server.js - wallet server for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
2017-11-01 12:57:11 -07:00
const assert = require('assert');
2017-10-26 11:58:50 -07:00
const Node = require('../node/node');
2017-06-29 20:54:07 -07:00
const WalletDB = require('./walletdb');
2017-10-26 12:31:08 -07:00
const HTTP = require('./http');
2017-06-29 20:54:07 -07:00
const Client = require('./client');
2017-10-26 11:58:50 -07:00
const RPC = require('./rpc');
2018-01-02 20:24:56 -08:00
const pkg = require('../pkg');
/**
2017-10-26 11:58:50 -07:00
* Wallet Node
* @extends Node
*/
2017-11-16 20:11:17 -08:00
class WalletNode extends Node {
/**
* Create a wallet node.
* @constructor
* @param {Object?} options
*/
constructor(options) {
2018-01-02 20:24:56 -08:00
super(pkg.name, 'wallet.conf', 'wallet.log', options);
2017-11-16 20:11:17 -08:00
this.opened = false;
this.client = new Client({
network: this.network,
url: this.config.str('node-url'),
host: this.config.str('node-host'),
port: this.config.str('node-port', this.network.rpcPort),
ssl: this.config.str('node-ssl'),
apiKey: this.config.str('node-api-key')
});
this.wdb = new WalletDB({
network: this.network,
logger: this.logger,
workers: this.workers,
client: this.client,
prefix: this.config.prefix,
2017-12-06 17:05:00 -08:00
memory: this.config.bool('memory'),
2017-11-16 20:11:17 -08:00
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
witness: this.config.bool('witness'),
checkpoints: this.config.bool('checkpoints'),
wipeNoReally: this.config.bool('wipe-no-really'),
spv: this.config.bool('spv')
});
this.rpc = new RPC(this);
this.http = new HTTP({
network: this.network,
logger: this.logger,
node: this,
prefix: this.config.prefix,
ssl: this.config.bool('ssl'),
keyFile: this.config.path('ssl-key'),
certFile: this.config.path('ssl-cert'),
host: this.config.str('http-host'),
port: this.config.uint('http-port'),
apiKey: this.config.str('api-key'),
walletAuth: this.config.bool('wallet-auth'),
2017-12-28 11:20:13 -08:00
noAuth: this.config.bool('no-auth'),
adminToken: this.config.str('admin-token')
2017-11-16 20:11:17 -08:00
});
this.init();
}
/**
* Initialize the node.
* @private
*/
init() {
this.wdb.on('error', err => this.error(err));
this.http.on('error', err => this.error(err));
this.loadPlugins();
}
/**
* Open the node and all its child objects,
* wait for the database to load.
* @returns {Promise}
*/
async open() {
assert(!this.opened, 'WalletNode is already open.');
this.opened = true;
await this.handlePreopen();
await this.wdb.open();
this.rpc.wallet = this.wdb.primary;
await this.openPlugins();
await this.http.open();
await this.handleOpen();
this.logger.info('Wallet node is loaded.');
}
/**
* Close the node, wait for the database to close.
* @returns {Promise}
*/
async close() {
assert(this.opened, 'WalletNode is not open.');
this.opened = false;
await this.handlePreclose();
await this.http.close();
await this.closePlugins();
this.rpc.wallet = null;
await this.wdb.close();
await this.handleClose();
}
2017-10-26 11:58:50 -07:00
}
/*
* Expose
*/
2017-10-26 11:58:50 -07:00
module.exports = WalletNode;