Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from girishgoudapatil/master
Browse files Browse the repository at this point in the history
blockchain ack api
  • Loading branch information
monkeypants committed Jul 31, 2016
2 parents 6145be8 + 003819a commit e562497
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*~
*node_modules
2 changes: 1 addition & 1 deletion etherium/EInvoiceACK.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract EInvoiceACK {
gw_datetime: gw_datetime,
post_datetime: post_datetime
});
return tx.origin;
return this;
}

function getAckPayloadHash() returns (string) {
Expand Down
56 changes: 56 additions & 0 deletions etherium/api/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var express = require('express');
var path = require('path');

var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var acks = require('./routes/ack');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api/ack', acks);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;
90 changes: 90 additions & 0 deletions etherium/api/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('api:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
18 changes: 18 additions & 0 deletions etherium/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "api",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ethersim": "^0.5.0",
"express": "~4.13.4",
"jade": "~1.11.0",
"morgan": "~1.7.0",
"web3": "^0.16.0"
}
}
8 changes: 8 additions & 0 deletions etherium/api/public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
106 changes: 106 additions & 0 deletions etherium/api/routes/ack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var express = require('express');
var path = require('path');
var fs = require('fs');
var router = express.Router();

var EtherSim = require('ethersim');
var sim = new EtherSim.init();

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(sim.provider);

var templatePath = path.join(__dirname, '../..', 'EInvoiceACK.sol')
var contractSource = fs.readFileSync(templatePath, {
encoding : 'utf8'
});

var compiled, code, abi;
sim.createAccounts(2, function(err) {
if (err) {
console.log(err);
return process.exit(1);
}
web3.eth.getAccounts(function(err, accounts){
if (!accounts) {
console.log("Please create at least one account to start with");
return process.exit(1);
}
web3.eth.defaultAccount = accounts[0];
sim.setBalance(accounts[0], 9999934000500000, function() {})
compiled = web3.eth.compile.solidity(contractSource);
code = compiled.EInvoiceACK.code;
abi = compiled.EInvoiceACK.info.abiDefinition;
});
})

var codes = {
1 : 'created',
2 : 'received',
3 : 'approved',
4 : 'disputed',
5 : 'cancelled',
6 : 'paid'
};

/* POST ack
*
* Example Payload
* {
* payload_hash : "odihodhodh;odhodhohocnkcich",
* url : "http://govhack.org",
* code : 1,
* gw_datetime : 6963963963
* }
*
*
*/

router.post('/', function(req, res, next) {
var ack = req.body;
if (!(ack && ack.payload_hash && ack.url && ack.code && ack.gw_datetime)) {
return res.status(422).send(new Error("Missing mandatory body parameters"));
}
if (!codes[ack.code]) {
return res.status(422).send(new Error("Ack status code is not supported"));
}
web3.eth.contract(abi).new({
data: code
}, function (err, contract) {
if (err) {
return res.status(422).send(err);
}
try{
if (contract.transactionHash && contract.address) {
var address = contract.address
res.send(address);
}else if(contract.transactionHash && !contract.address){
//no op
}
}catch(error){
res.status(500).send(error);
}
})
});


router.get('/:address', function (req, res, next) {
var address = req.params.address;
if (!address) {
return res.status(422).send(new Error("Missing address query parameter"));
}
var contract = web3.eth.contract(abi);
var contractInstance = contract.at(address);
res.send({
payload_hash : contractInstance.getAckPayloadHash(),
url : contractInstance.getAckUrl(),
code : contractInstance.getAckstatus(),
gw_datetime : contractInstance.getAckgw_datetime(),
post_datetime : contractInstance.getAckPostDateTime()
});
})

router.get('/codes', function (req, res, next) {
res.send(codes)
});
module.exports = router;
6 changes: 6 additions & 0 deletions etherium/api/views/error.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends layout

block content
h1= message
h2= error.status
pre #{error.stack}
7 changes: 7 additions & 0 deletions etherium/api/views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content

0 comments on commit e562497

Please sign in to comment.