All files / lib plugin.js

0% Statements 0/39
0% Branches 0/2
0% Functions 0/8
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
/*!
 * bmultisig.js - Multsig plugin for bwallet
 * Copyright (c) 2018, The Bcoin Developers (MIT License).
 * https://github.com/bcoin-org/bcoin
 */
 
'use strict';
 
const assert = require('bsert');
const EventEmitter = require('events');
const Network = require('bcoin').Network;
const WalletNodeClient = require('./walletclient');
const MultisigDB = require('./multisigdb');
const HTTP = require('./http');
const pkg = require('./pkg');
 
/**
 * @exports multisig/plugin
 */
 
const plugin = exports;
 
/**
 * Plugin
 * @extends EventEmitter
 * @property {bcfg.Config} config
 * @property {Object} options
 * @property {blgr.Logger} logger
 * @property {WalletNodeClient} client
 * @property {MultisigDB} msdb
 * @property {MultisigHTTP} http
 */
class Plugin extends EventEmitter {
  constructor(options) {
    super();
 
    assert(options, 'MultisigWallet requires options.');
    assert(options.node, 'MultisigWallet requires node.');
 
    const node = options.node;
    const network = Network.get(node.network.type);
 
    this.config = node.config.filter('multisig');
 
    this.options = options;
    this.node = node;
    this.logger = node.logger.context('multisig');
 
    this.client = new WalletNodeClient(this.node);
 
    this.msdb = new MultisigDB({
      network: network,
      logger: this.logger,
      client: this.client,
 
      prefix: this.config.prefix,
      memory: this.config.bool('memory', node.memory)
    });
 
    const httpOptions = node.http.options;
 
    this.http = new HTTP({
      msdb: this.msdb,
      logger: this.logger,
      version: pkg.version,
      whttp: this.node.http,
 
      // from Wallet.http
      apiKey: httpOptions.apiKey,
      walletAuth: httpOptions.walletAuth,
      noAuth: httpOptions.noAuth,
      adminToken: httpOptions.adminToken
    });
 
    this.init();
  }
 
  logError(err) {
    this.logger.debug(err.message);
    this.logger.debug(err.stack);
  }
 
  init() {
    this.msdb.on('error', (err) => {
      this.logError(err);
      this.emit('error', err);
    });
 
    this.http.on('error', (err) => {
      this.logError(err);
      this.emit('error', err);
    });
 
    this.msdb.init();
  }
 
  async open() {
    if (this.node.http)
      this.http.attach('/multisig', this.node.http);
 
    await this.msdb.open();
  }
 
  async close() {
    await this.msdb.close();
  }
}
 
/**
 * Plugin name.
 * @const {String}
 */
 
plugin.id = 'multisig';
 
/**
 * Plugin initialization.
 * @param {bcoin.WalletNode}
 * @returns {Plugin}
 */
 
plugin.init = function init(node) {
  return new Plugin({ node });
};
 
/*
 * Expose
 */
module.exports = plugin;