Skip to content

Commit

Permalink
Merge pull request #38 from ChronoBank/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ega-forever authored Nov 14, 2017
2 parents aedf2d0 + c8bee9b commit a895d19
Show file tree
Hide file tree
Showing 19 changed files with 5,475 additions and 28 deletions.
8 changes: 7 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Chronobank/eth-rest configuration
* @module config
* @returns {Object} Configuration
*/
require('dotenv').config();

const config = {
Expand All @@ -6,7 +11,8 @@ const config = {
},
rest: {
domain: process.env.DOMAIN || 'localhost',
port: parseInt(process.env.REST_PORT) || 8081
port: parseInt(process.env.REST_PORT) || 8081,
auth: true
},
web3: {
network: process.env.NETWORK || 'development',
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
* Expose an express web server
* @module middleware-eth-rest
*/

const config = require('./config'),
express = require('express'),
routes = require('./routes'),
authMiddleware = require('./utils/authMiddleware'),
cors = require('cors'),
Promise = require('bluebird'),
mongoose = require('mongoose'),
bodyParser = require('body-parser');

/**
* @module entry point
* @description expose an express web server for txs
* and accounts manipulation
*/
mongoose.Promise = Promise;
mongoose.connect(config.mongo.uri, {useMongoClient: true});

Expand All @@ -19,6 +20,7 @@ let app = express();
app.use(cors());
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(authMiddleware);

routes(app);

Expand Down
48 changes: 45 additions & 3 deletions models/accountModel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
/**
* Mongoose model. Used to store hashes, which need to be pinned.
* @module models/accountModel
* @returns {Object} Mongoose model
*/

const mongoose = require('mongoose'),
bcrypt = require('bcryptjs'),
messages = require('../factories/messages/accountMessageFactory');
SALT_WORK_FACTOR = 10;

require('mongoose-long')(mongoose);

/** @model accountModel
* @description account model - represents an eth account
/**
* Account model definition
* @param {Object} obj Describes account's model
* @return {Object} Model's object
*/
const Account = new mongoose.Schema({
address: {
Expand All @@ -15,7 +25,39 @@ const Account = new mongoose.Schema({
},
balance: {type: mongoose.Schema.Types.Long, default: 0},
created: {type: Date, required: true, default: Date.now},
erc20token : {type: mongoose.Schema.Types.Mixed, default: {}},
erc20token: {type: mongoose.Schema.Types.Mixed, default: {}},
password: {type: String}
});

/**
* Use virtual field for encrypting the password
*/
Account.virtual('clean_password')
.set(function (clean_password) {
this.password = this.encryptPassword(clean_password);
})
.get(function () { return this.password });

Account.methods = {
/**
* Password comparison
* @param {string} plainPassword
* @return {boolean}
*/
authenticate: function(plainPassword) {
return bcrypt.compareSync(plainPassword, this.password)
},
/**
*
* @param {string} password Plain password to encrypt
* @return {string} Encrypted password
*/
encryptPassword: function(password) {
if (!password)
return '';
const salt = bcrypt.genSaltSync(SALT_WORK_FACTOR);
return bcrypt.hashSync(password, salt);
}
};

module.exports = mongoose.model('EthAccount', Account);
Loading

0 comments on commit a895d19

Please sign in to comment.