exports.up = function(knex) { return knex.schema.createTable('accounts', function(t) { t.increments('id').unsigned().primary(); t.int('user_id').notNull(); t.foreign('user_id').references('users.id'); t.int('uuid').notNull(); t.double('balance'); t.double('pending'); t.enu('network', ['Bitcoin', 'Liquid', 'Lightning', 'CoinOS']).notNull(); t.longtext('contract').collate('utf8mb4').nullable(); // custodial ? // knex.raw("ALTER TABLE accounts MODIFY `contract` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL") // t.string('hide'); // t.string('index'); t.int('liquid_asset_id').notNull(); t.foreign('liquid_asset_id').references('liquid_assets.id'); t.timestamps(); }) } exports.down = function(knex) { return knex.schema.dropTable('accounts'); }; // Change // // move assets fields (asset, ticker, name, precision...) to separate liquid_assets reference // separate network specific fields to bitcoin_accounts / liquid_accounts / coinos_accounts tables ? or just non_custodial ? // eg liquid -> liquid_asset info // non-custodial ? -> privkey, pubkey, path, seed ? // contract, hide, index ? // // const { DataTypes } = require('sequelize'); // const attributes = { // id: { // type: DataTypes.INTEGER(11), // allowNull: false, // defaultValue: null, // primaryKey: true, // autoIncrement: true, // comment: null, // field: "id" // }, // user_id: { // type: DataTypes.INTEGER(11), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "user_id" // }, // createdAt: { // type: DataTypes.DATE, // allowNull: false, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "createdAt" // }, // updatedAt: { // type: DataTypes.DATE, // allowNull: false, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "updatedAt" // }, // path: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "path" // }, // seed: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "seed" // }, // network: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "network" // }, // name: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "name" // }, // contract: { // type: DataTypes.TEXT, // get: function() { // return JSON.parse(this.getDataValue("contract")); // }, // set: function(value) { // return this.setDataValue("contract", JSON.stringify(value)); // }, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "contract" // }, // domain: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "domain" // }, // ticker: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "ticker" // }, // asset: { // type: DataTypes.STRING(255), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "asset" // }, // balance: { // type: DataTypes.DOUBLE, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "balance" // }, // pending: { // type: DataTypes.DOUBLE, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "pending" // }, // hide: { // type: DataTypes.BOOLEAN, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "hide" // }, // index: { // type: DataTypes.INTEGER(11), // allowNull: false, // defaultValue: 0, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "index" // }, // privkey: { // type: DataTypes.STRING, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "privkey" // }, // pubkey: { // type: DataTypes.STRING, // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "pubkey" // }, // precision: { // type: DataTypes.INTEGER(11), // allowNull: true, // defaultValue: null, // primaryKey: false, // autoIncrement: false, // comment: null, // field: "precision" // }, // }; // const options = { // tableName: "accounts", // comment: "", // indexes: [] // }; // db["Account"] = db.define("accounts_model", attributes, options); 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
const axios = require("axios");
const bodyParser = require("body-parser");
const compression = require("compression");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const express = require("express");
const { Op } = require("sequelize");
const persist = require("./lib/persist");

ah = require("express-async-handler");

config = require("./config");
networks = [];
prod = process.env.NODE_ENV === "production";
fail = msg => {
  throw new Error(msg);
};

addresses = {};
challenge = {};
change = [];
exceptions = [];
issuances = {};
logins = {};
seen = [];
sessions = {};
sockets = {};
unaccounted = [];

convert = persist("data/conversions.json");

SATS = 100000000;
toSats = n => Math.round(n * SATS);

app = express();
app.enable("trust proxy");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use((err, req, res, next) => {
  const details = {
    path: req.path,
    body: req.body,
    msg: err.message,
    stack: err.stack
  };

  if (req.user) details.username = req.user.username;

  l.error("JSON Error: ", JSON.stringify(details));
  res.status(500);
  res.set({
    "Cache-Control": "no-cache"
  });
  res.send(err.message);
  return res.end();
});
app.use(cookieParser());
app.use(cors({ credentials: true, origin: "*" }));
app.use(compression());

server = require("http").Server(app);

require("./db");
require("./lib/logging");
require("./lib/utils");
require("./lib/sockets");
require("./lib/webhooks");
require("./lib/passport");
require("./lib/register");
require("./lib/send");
require("./lib/sync");

require("./routes/assets");
require("./routes/info");

require("./routes/users");

// Exclude plugins for minimalist version
if (process.env.SCOPE !== "MIN") {
  if (config.bitcoin) networks.push("bitcoin");
  if (config.liquid) networks.push("liquid");
  if (config.lna) networks.push("lightning");

  require("./lib/rates");
  require("./lib/notifications");

  if (config.imap) require("./lib/mail");
  if (config.lnurl) require("./routes/lnurl");
  if (config.mailgun) require("./routes/funding");

  require("./routes/invoices");
  require("./routes/payments");
  require("./routes/swaps");
}

//  Scope based Route Handling

var referralsRouter = require("./routes/referrals.js");
app.use("/referrals", referralsRouter);

var adminRouter = require("./routes/admin.js");
app.use("/admin", adminAuth, adminRouter);

require("./startup");

app.post("/email", async (req, res) => {
  try {
    let file = persist("data/emails.json");
    file.emails = [...file.emails, req.body];

    try {
      // Require:
      var postmark = require("postmark");

      // Send an email:
      var client = new postmark.ServerClient(config.postmark);

      await client.sendEmail({
        From: "support@coinos.io",
        To: "support@coinos.io",
        Subject: req.body.subject || "Email Signup",
        HtmlBody: JSON.stringify(req.body),
        TextBody: JSON.stringify(req.body),
        MessageStream: "outbound"
      });

      res.send({ ok: true });
    } catch (e) {
      console.log("problem sending email", e);
      res.code(500).send(e.message);
    }
  } catch (e) {
    console.log(e);
    res.code(500).send(e.message);
  }
});

app.use((err, req, res, next) => {
  const details = {
    path: req.path,
    body: req.body,
    msg: err.message,
    stack: err.stack
  };

  if (req.user) details.username = req.user.username;

  l.error("uncaught error: ", JSON.stringify(details));
  res.status(500);
  res.set({
    "Cache-Control": "no-cache"
  });
  res.send(err.message);
});

server.listen(config.port, () =>
  l.info(`CoinOS Server listening on port ${config.port}`)
);

process.on("SIGINT", process.exit);

process.on("uncaughtException", function(exception) {
  console.log(exception);
});