-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Create tokens associated with contracts.
Switched to using Bookshelf.js as the ORM.
- Loading branch information
Showing
10 changed files
with
120 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var nconf = require('./config'); | ||
|
||
var knex = require('knex').initialize(nconf.get('db')); | ||
var bookshelf = require('bookshelf').initialize(knex); | ||
|
||
exports.knex = knex; | ||
exports.bookshelf = bookshelf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var bookshelf = require('../lib/db').bookshelf; | ||
|
||
var Token = require('./token').model; | ||
|
||
var Balance = bookshelf.Model.extend({ | ||
tableName: 'balances', | ||
tokens: function () { | ||
return this.hasMany(Token); | ||
} | ||
}); | ||
|
||
exports.model = Balance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var bookshelf = require('../lib/db').bookshelf; | ||
|
||
var Token = require('./token').model; | ||
|
||
var Contract = bookshelf.Model.extend({ | ||
tableName: 'contracts', | ||
tokens: function () { | ||
return this.hasMany(Token); | ||
} | ||
}); | ||
|
||
exports.model = Contract; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var bookshelf = require('../lib/db').bookshelf; | ||
|
||
var Balance = require('./balance').model; | ||
|
||
var Token = bookshelf.Model.extend({ | ||
tableName: 'tokens', | ||
balance: function () { | ||
return this.belongsTo(Balance); | ||
}, | ||
parent: function () { | ||
return this.belongsTo(Token); | ||
}, | ||
children: function () { | ||
return this.hasMany(Token); | ||
} | ||
}); | ||
|
||
exports.model = Token; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
}, | ||
"dependencies": { | ||
"base64url": "0.0.6", | ||
"bookshelf": "^0.7.7", | ||
"codius-engine": "git+ssh://[email protected]:codius/codius-engine.git#master", | ||
"concat-stream": "^1.4.6", | ||
"express": "^4.5.1", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,43 @@ | ||
var crypto = require('crypto'); | ||
var base64url = require('base64url'); | ||
|
||
var Token = require('../models/token').model; | ||
var Contract = require('../models/contract').model; | ||
|
||
/** | ||
* Request a token. | ||
* | ||
* A token is a multi-use endpoint for interacting with a contract via HTTP. | ||
*/ | ||
module.exports = function (req, res) { | ||
var token = base64url(crypto.pseudoRandomBytes(8)); | ||
var config = req.app.get('config'); | ||
|
||
// TODO: Token uniqueness should be checked | ||
function getUniqueToken() { | ||
var token = base64url(crypto.pseudoRandomBytes(config.get('token_size'))); | ||
|
||
// TODO: Token needs to be associated with a contract | ||
return new Token({token: token}).fetch().then(function (model) { | ||
if (model !== null) { | ||
return getUniqueToken(); | ||
} else return token; | ||
}); | ||
} | ||
|
||
res.json(200, { | ||
token: token | ||
// First we check if the contract actually exists | ||
new Contract({hash: req.query.contract}).fetch().then(function (contract) { | ||
if (!contract) { | ||
// Contract doesn't exist | ||
res.json(400, { | ||
message: "Unknown contract hash" | ||
}); | ||
} else { | ||
return getUniqueToken().then(function (token) { | ||
return Token.forge({token: token, contract: contract.get('id')}).save(); | ||
}).then(function (token) { | ||
// All done! | ||
res.json(200, { | ||
token: token.get('token') | ||
}); | ||
}); | ||
} | ||
}); | ||
}; |