#!/usr/bin/env node

'use strict';

process.title = 'bcoin';

var assert = require('assert');
var bcoin = require('../');
var plugin = require('../lib/wallet/plugin');
var util = bcoin.util;
var co = bcoin.co;
var node;

node = bcoin.spvnode({
  config: true,
  argv: true,
  env: true,
  logFile: true,
  logConsole: true,
  logLevel: 'debug',
  db: 'leveldb',
  persistent: true,
  listen: true,
  loader: require
});

// Temporary hack
if (!node.has('walletdb'))
  node.use(plugin);

node.on('error', function(err) {
  ;
});

co.spawn(function *() {
  yield node.ensure();
  yield node.open();
  yield node.connect();

  if (node.config.bool('test')) {
    node.pool.watchAddress('1VayNert3x1KzbpzMGt2qdqrAThiRovi8');
    node.pool.watchOutpoint(new bcoin.outpoint());
    node.on('block', function(block) {
      assert(block.txs.length >= 1);
      if (block.txs.length > 1)
        util.log(block.txs[1]);
    });
  }

  node.startSync();
}).catch(function(err) {
  throw err;
});
