Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Modules crypto, sql and system
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasIsasmendi committed May 5, 2017
1 parent e745517 commit 556cb35
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
21 changes: 21 additions & 0 deletions modules/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ var modules, library, self, __private = {}, shared = {};

__private.loaded = false;

/**
* Initializes library with scope content.
* @class
* @classdesc Main Crypto methods.
* @param {setImmediateCallback} cb - Callback function.
* @param {scope} scope - App instance.
*/
// Constructor
function Crypto (cb, scope) {
library = scope;
Expand All @@ -18,15 +25,29 @@ function Crypto (cb, scope) {
}

// Public methods
/**
* Calls helpers.sandbox.callMethod().
* @implements module:helpers#callMethod
* @param {function} call - Method to call.
* @param {*} args - List of arguments.
* @param {function} cb - Callback function.
*/
Crypto.prototype.sandboxApi = function (call, args, cb) {
sandboxHelper.callMethod(shared, call, args, cb);
};

// Events
/**
* Assigns scope to modules variable.
* @param {scope} scope - Loaded modules.
*/
Crypto.prototype.onBind = function (scope) {
modules = scope;
};

/**
* Sets to true private variable loaded.
*/
Crypto.prototype.onBlockchainReady = function () {
__private.loaded = true;
};
Expand Down
93 changes: 93 additions & 0 deletions modules/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ __private.SINGLE_QUOTES_DOUBLED = '\'\'';
__private.DOUBLE_QUOTES = /"/g;
__private.DOUBLE_QUOTES_DOUBLED = '""';

/**
* Initializes library with scope content.
* @class
* @classdesc Main Sql methods.
* @param {setImmediateCallback} cb - Callback function.
* @param {scope} scope - App instance.
*/
// Constructor
function Sql (cb, scope) {
library = scope;
Expand All @@ -24,6 +31,13 @@ function Sql (cb, scope) {
}

// Private methods
/**
* Adds scape values based on input type.
* @private
* @param {*} what
* @return {string}
* @throws {string} Unsupported data (with type)
*/
__private.escape = function (what) {
switch (typeof what) {
case 'string':
Expand All @@ -49,10 +63,21 @@ __private.escape = function (what) {
throw 'Unsupported data ' + typeof what;
};

/**
* Adds double quotes to input string.
* @private
* @param {string} str
* @return {string}
*/
__private.escape2 = function (str) {
return '"' + str.replace(__private.DOUBLE_QUOTES, __private.DOUBLE_QUOTES_DOUBLED) + '"';
};

/**
* @private
* @param {Object} obj
* @param {string} dappid
*/
__private.pass = function (obj, dappid) {
for (var property in obj) {
if (typeof obj[property] === 'object') {
Expand Down Expand Up @@ -85,6 +110,16 @@ __private.pass = function (obj, dappid) {
}
};

/**
* Creates sql query to dapps
* @implements {jsonSql.build}
* @implements {library.db.query}
* @implements {async.until}
* @param {string} action
* @param {Object} config
* @param {function} cb
* @return {setImmediateCallback} cb, err, data
*/
__private.query = function (action, config, cb) {
var sql = null;

Expand Down Expand Up @@ -150,6 +185,16 @@ __private.query = function (action, config, cb) {
};

// Public methods
/**
* Creates sql sentences to dapp_ tables based on config param and runs them.
* @implements {jsonSql.build}
* @implements {async.eachSeries}
* @implements {library.db.none}
* @param {string} dappid
* @param {Object} config
* @param {function} cb
* @return {setImmediateCallback} err message | cb
*/
Sql.prototype.createTables = function (dappid, config, cb) {
if (!config) {
return setImmediate(cb, 'Invalid table format');
Expand Down Expand Up @@ -190,6 +235,15 @@ Sql.prototype.createTables = function (dappid, config, cb) {
});
};

/**
* Drops tables based on config param.
* @implements {async.eachSeries}
* @implements {library.db.none}
* @param {string} dappid
* @param {Object} config
* @param {function} cb
* @return {setImmediateCallback} err message | cb
*/
Sql.prototype.dropTables = function (dappid, config, cb) {
var tables = [];
for (var i = 0; i < config.length; i++) {
Expand Down Expand Up @@ -217,40 +271,79 @@ Sql.prototype.dropTables = function (dappid, config, cb) {
}, cb);
};

/**
* Calls helpers.sandbox.callMethod().
* @implements module:helpers#callMethod
* @param {function} call - Method to call.
* @param {*} args - List of arguments.
* @param {function} cb - Callback function.
*/
Sql.prototype.sandboxApi = function (call, args, cb) {
sandboxHelper.callMethod(shared, call, args, cb);
};

// Events
/**
* Assigns scope to modules variable.
* @param {scope} scope - Loaded modules.
*/
Sql.prototype.onBind = function (scope) {
modules = scope;
};

/**
* Sets to true private variable loaded.
*/
Sql.prototype.onBlockchainReady = function () {
__private.loaded = true;
};

// Shared API
/**
* @implements {__private.query.call}
* @param {Object} req
* @param {function} cb
*/
shared.select = function (req, cb) {
var config = extend({}, req.body, {dappid: req.dappid});
__private.query.call(this, 'select', config, cb);
};

/**
* @implements {__private.query.call}
* @param {Object} req
* @param {function} cb
*/
shared.batch = function (req, cb) {
var config = extend({}, req.body, {dappid: req.dappid});
__private.query.call(this, 'batch', config, cb);
};

/**
* @implements {__private.query.call}
* @param {Object} req
* @param {function} cb
*/
shared.insert = function (req, cb) {
var config = extend({}, req.body, {dappid: req.dappid});
__private.query.call(this, 'insert', config, cb);
};

/**
* @implements {__private.query.call}
* @param {Object} req
* @param {function} cb
*/
shared.update = function (req, cb) {
var config = extend({}, req.body, {dappid: req.dappid});
__private.query.call(this, 'update', config, cb);
};

/**
* @implements {__private.query.call}
* @param {Object} req
* @param {function} cb
*/
shared.remove = function (req, cb) {
var config = extend({}, req.body, {dappid: req.dappid});
__private.query.call(this, 'remove', config, cb);
Expand Down
82 changes: 82 additions & 0 deletions modules/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ var modules, library, self, __private = {}, shared = {};

var rcRegExp = /[a-z]+$/;

/**
* Initializes library with scope content and private variables:
* - os
* - version
* - port
* - height
* - nethash
* - broadhash
* - minVersion
* - nonce
* @class
* @classdesc Main System methods.
* @implements {os}
* @param {setImmediateCallback} cb - Callback function.
* @param {scope} scope - App instance.
*/
// Constructor
function System (cb, scope) {
library = scope;
Expand All @@ -37,38 +53,77 @@ function System (cb, scope) {
}

// Public methods
/**
* Returns private variables object content.
* @return {Object}
*/
System.prototype.headers = function () {
return __private;
};

/**
* Gets private variable `os`
* @return {string}
*/
System.prototype.getOS = function () {
return __private.os;
};

/**
* Gets private variable `version`
* @return {string}
*/
System.prototype.getVersion = function () {
return __private.version;
};

/**
* Gets private variable `port`
* @return {number}
*/
System.prototype.getPort = function () {
return __private.port;
};

/**
* Gets private variable `height`
* @return {number}
*/
System.prototype.getHeight = function () {
return __private.height;
};

/**
* Gets private variable `nethash`
* @return {hash}
*/
System.prototype.getNethash = function () {
return __private.nethash;
};

/**
* Gets private variable `nethash` and compares with input param.
* @param {hash}
* @return {boolean} True if input param is equal to private value.
*/
System.prototype.networkCompatible = function (nethash) {
return __private.nethash === nethash;
};

/**
* Gets private variable `minVersion`
* @return {string}
*/
System.prototype.getMinVersion = function () {
return __private.minVersion;
};

/**
* Checks version compatibility from input param against private values.
* @implements {semver}
* @param {string} version
* @return {boolean}
*/
System.prototype.versionCompatible = function (version) {
var versionChar;

Expand All @@ -87,6 +142,13 @@ System.prototype.versionCompatible = function (version) {
return semver.satisfies(version, this.minVersion);
};

/**
* Gets private nethash or creates a new one, based on input param and data.
* @implements {library.db.query}
* @implements {crypto.createHash}
* @param {*} cb
* @return {hash|setImmediateCallback} err | private nethash or new hash.
*/
System.prototype.getBroadhash = function (cb) {
if (typeof cb !== 'function') {
return __private.broadhash;
Expand All @@ -107,6 +169,15 @@ System.prototype.getBroadhash = function (cb) {
});
};

/**
* Updates private broadhash and height values.
* @implements {async.series}
* @implements {System.getBroadhash}
* @implements {modules.blocks.getLastBlock}
* @implements {modules.transport.headers}
* @param {function} cb Callback function
* @return {setImmediateCallback} cb, err
*/
System.prototype.update = function (cb) {
async.series({
getBroadhash: function (seriesCb) {
Expand All @@ -129,11 +200,22 @@ System.prototype.update = function (cb) {
});
};

/**
* Calls helpers.sandbox.callMethod().
* @implements module:helpers#callMethod
* @param {function} call - Method to call.
* @param {*} args - List of arguments.
* @param {function} cb - Callback function.
*/
System.prototype.sandboxApi = function (call, args, cb) {
sandboxHelper.callMethod(shared, call, args, cb);
};

// Events
/**
* Assigns scope to modules variable.
* @param {scope} scope - Loaded modules.
*/
System.prototype.onBind = function (scope) {
modules = scope;
};
Expand Down

0 comments on commit 556cb35

Please sign in to comment.