Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
/public/stylesheets/*.css
/public/stylesheets/*.css.map
.DS_Store
/config/local*.js
5 changes: 1 addition & 4 deletions bin/www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

import * as debugModule from "debug";
import { createApp } from "../src/app";
import { closeContext } from "../src/context";
const debug = debugModule("faucet:server");
import * as http from "http";

main();

async function main() {
try {
const [app, context] = await createApp();
const app = await createApp();

/**
* Get port from environment and store in Express.
Expand Down Expand Up @@ -51,8 +50,6 @@ async function main() {
} catch (err) {
console.error(`Error at closing ${err}`);
} finally {
console.log("Cleanup context");
await closeContext(context);
process.exit();
}
});
Expand Down
20 changes: 20 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require("path");

module.exports = {
knex: {
client: "pg",
connection: {
host: "localhost",
port: 5432,
database: "codechain-keystore",
user: "codechain"
},
migrations: {
directory: path.resolve(__dirname, "..", "migrations"),
tableName: "knex"
},
seeds: {
directory: path.resolve(__dirname, "..", "seeds")
}
},
};
1 change: 1 addition & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("config").get("knex");
36 changes: 36 additions & 0 deletions migrations/00000000000000_keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const table = "keys";

exports.up = async (knex) => {
const hasTable = await knex.schema.hasTable(table);
if (!hasTable) {
return knex.schema.createTable(table, (t) => {
t.string("type").notNullable();
t.string("address").notNullable();
t.integer("version").notNullable();
t.string("kdf").notNullable();
t.json("kdfparams");
t.string("mac").notNullable();
t.string("cipher").notNullable();
t.json("cipherparams");
t.string("ciphertext").notNullable();
t.json("meta");

t.primary(["type", "address"]);
});
}
else {
// TODO: Add detailed error message
throw Error();
}
};

exports.down = async (knex) => {
const hasTable = await knex.schema.hasTable(table);
if (hasTable) {
return knex.schema.dropTable(table);
}
else {
// TODO: Add detailed error message
throw Error();
}
};
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
"start": "ts-node ./bin/www.ts",
"test": "yarn lint && jest --env node",
"lint": "tslint -p . && prettier '**/*.ts' -l",
"fmt": "tslint -p . --fix && prettier '**/*ts' --write"
"fmt": "tslint -p . --fix && prettier '**/*ts' --write",
"migrate": "knex migrate:latest --knexfile ./knexfile.js",
"rollback": "knex migrate:rollback --knexfile ./knexfile.js",
"seed": "knex seed:run --knexfile ./knexfile.js"
},
"dependencies": {
"codechain-keystore": "~0.4.0",
"config": "^2.0.1",
"@types/config": "^0.0.34",
"codechain-keystore": "^0.6.1",
"codechain-primitives": "^1.0.1",
"config": "^3.1.0",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"knex": "^0.16.5",
"morgan": "~1.9.1",
"morgan-body": "^2.4.5",
"objection": "^1.6.8",
"pg": "^7.10.0",
"request": "^2.88.0",
"request-promise": "^4.2.2"
},
Expand Down
6 changes: 6 additions & 0 deletions seeds/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const table = "keys";

exports.seed = async (knex) => {
await knex(table).del();
return knex(table).insert([]);
};
10 changes: 1 addition & 9 deletions src/__test__/ping.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Application } from "express";
import * as request from "supertest";
import { createApp } from "../app";
import { closeContext, Context } from "../context";

let app: Application;
let context: Context;

beforeEach(async () => {
const res = await createApp();
app = res[0];
context = res[1];
});

afterEach(async () => {
await closeContext(context);
app = await createApp();
});

test("ping", async () => {
Expand Down
14 changes: 8 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as config from "config";
import * as express from "express";
import * as createError from "http-errors";
import * as knex from "knex";
import * as logger from "morgan";
import { Model } from "objection";
import * as path from "path";
const morganBody = require("morgan-body");

import { Context, createContext } from "./context";
import { createRouter as createApiRouter } from "./routes/api";
import { createRouter as createPingRouter } from "./routes/ping";

export async function createApp(): Promise<[express.Application, Context]> {
export async function createApp(): Promise<express.Application> {
Model.knex(knex(config.get("knex")));
const app = express();

// view engine setup
Expand All @@ -20,9 +23,8 @@ export async function createApp(): Promise<[express.Application, Context]> {
app.use(express.static(path.join(__dirname, "public")));
morganBody(app);

const context = await createContext();
app.use("/api", createApiRouter(context));
app.use("/ping", createPingRouter(context));
app.use("/api", createApiRouter());
app.use("/ping", createPingRouter());

// catch 404 and forward to error handler
app.use((req, res, next) => {
Expand All @@ -40,5 +42,5 @@ export async function createApp(): Promise<[express.Application, Context]> {
res.status(err.status || 500);
res.render("error");
});
return [app, context];
return app;
}
18 changes: 0 additions & 18 deletions src/context.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/models/key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SecretStorage } from "codechain-keystore";
import { Model, snakeCaseMappers } from "objection";

export default class KeyModel extends Model {
public static columnNameMappers = snakeCaseMappers();

public static tableName = "keys";
public static idColumn = ["type", "address"];
public readonly type!: "asset" | "platform";
public readonly address!: string;
public readonly version!: number;

public readonly kdf!: string;
public readonly kdfparams?: any;
public readonly mac!: string;

public readonly cipher!: string;
public readonly cipherparams?: any;
public readonly ciphertext!: string;

public readonly meta!: any;

public toJSON(): SecretStorage {
return {
crypto: {
ciphertext: this.ciphertext,
cipherparams: this.cipherparams,
cipher: this.cipher,
kdf: this.kdf,
kdfparams: this.kdfparams,
mac: this.mac
},
id: this.address,
version: this.version,
address: this.address,
meta: this.meta
};
}
}
Loading