From 276b7c5875e80ba7e22feb284cfb2794ea939cd4 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Fri, 12 Jul 2013 23:55:51 +0200
Subject: [PATCH] Add udc/amendments/init

---
 TODO              |  2 +-
 lib/amendments.js | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/configurer.js | 28 ++++++++++++++++++++++++++++
 lib/nodecoin.js   | 33 ++++++++++++++++++++++-----------
 lib/udc.js        |  1 -
 protocol.md       |  2 +-
 6 files changed, 92 insertions(+), 14 deletions(-)
 create mode 100644 lib/amendments.js
 create mode 100644 lib/configurer.js
 delete mode 100644 lib/udc.js

diff --git a/TODO b/TODO
index 58c35cb11..81390d50e 100644
--- a/TODO
+++ b/TODO
@@ -18,7 +18,7 @@ Protocol implementation:
      ✔ REST service to store a OpenPGP certificate in AA format, with signature of it AA format too - pks/add @done (13-07-12 01:20)
 
     2) Managing Amendment submission:
-     ☐ REST service to push root certificates used to initialize Amendment Chain - udc/amendments/init
+     ✔ REST service to view root certificates used to initialize Amendment Chain - udc/amendments/init @done (13-07-12 23:55)
      ☐ REST service for receiving an Amendment data structure - udc/amendments/submit
      ☐ Check Amendment structure
      ☐ Check Amendment eligibility according to the Node rules (notably UD amount)
diff --git a/lib/amendments.js b/lib/amendments.js
new file mode 100644
index 000000000..73808e918
--- /dev/null
+++ b/lib/amendments.js
@@ -0,0 +1,40 @@
+var fs  = require('fs'),
+util    = require('util'),
+async   = require('async'),
+orm     = require('orm'),
+_       = require('underscore'),
+stream  = require('stream');
+
+module.exports.init = function (initKeys, req, res) {
+  var PublicKey = req.models.PublicKey;
+  var pubkeys = [];
+  async.forEach(initKeys, function (initkey, done) {
+    var pk = new PublicKey({ raw:initkey.data });
+    pk.construct(function (err) {
+      pubkeys.push(pk);
+      done(err);
+    });
+  }, function (err) {
+    if(err)
+      res.send(503, err);
+    else{
+      res.writeHead(200, {"Content-type": "text/plain"});
+      res.end(JSON.stringify(pubkeys));
+    }
+  });
+};
+
+module.exports.submit = function (req, res) {
+};
+
+module.exports.members = function (req, res) {
+};
+
+module.exports.self = function (req, res) {
+};
+
+module.exports.voters = function (req, res) {
+};
+
+module.exports.vote = function (req, res) {
+};
\ No newline at end of file
diff --git a/lib/configurer.js b/lib/configurer.js
new file mode 100644
index 000000000..63c2700c4
--- /dev/null
+++ b/lib/configurer.js
@@ -0,0 +1,28 @@
+var async = require('async');
+var fs    = require('fs');
+
+function Configurer(config) {
+
+  this.config = config;
+
+  this.parseFiles = function (callback) {
+    var obj = this;
+    if (obj.config.initKeys) {
+      async.forEach(obj.config.initKeys, function(file, done){
+        fs.readFile(file.path, {encoding: "utf8"}, function (err, data) {
+          file.data = data;
+          done(err);
+        });
+      },
+      callback);
+    }
+    else{
+      callback();
+    }
+    return this;
+  };
+}
+
+module.exports = function (config) {
+  return new Configurer(config);
+};
\ No newline at end of file
diff --git a/lib/nodecoin.js b/lib/nodecoin.js
index 8f722aaed..132c9eba8 100644
--- a/lib/nodecoin.js
+++ b/lib/nodecoin.js
@@ -1,15 +1,16 @@
-var pks  = require('./pks'),
-udc      = require('./udc'),
-express  = require('express'),
-orm      = require('orm'),
-async    = require('async'),
-config   = require('../config'),
-path     = require('path'),
-_        = require('underscore'),
-nodecoin = require('../lib/nodecoin');
+var pks    = require('./pks'),
+amend      = require('./amendments'),
+express    = require('express'),
+orm        = require('orm'),
+async      = require('async'),
+config     = require('../config'),
+path       = require('path'),
+_          = require('underscore'),
+nodecoin   = require('../lib/nodecoin');
+configurer = require('../lib/configurer');
 
 module.exports.pks = pks;
-module.exports.udc = udc;
+module.exports.amendments = amend;
 module.exports.express = {
 
   route: function(app){
@@ -22,7 +23,7 @@ module.exports.express = {
     app.get(    '/pks/lookup',                                  pks.lookup);
     app.get(    '/pks/add',                                     pks.add.get);
     app.post(   '/pks/add',                                     pks.add.post);
-    app.post(   '/udc/amendments/init',                         notImplemented);
+    app.get(    '/udc/amendments/init',                         _(amend.init).partial(app.get('config').initKeys));
     app.post(   '/udc/amendments/submit',                       notImplemented);
     app.post(   '/udc/amendments/vote',                         notImplemented);
     app.get(    '/udc/amendments/view/:amendment_id/members',   notImplemented);
@@ -49,7 +50,17 @@ module.exports.express = {
       }
     };
 
+    configurer(config).parseFiles(function (err) {
+      if(!err){
+        console.log("Initkeys loaded.");
+      }
+      else{
+        console.log(err);
+      }
+    });
+
     // all environments
+    app.set('config', config);
     app.set('port', process.env.PORT || config.server.port);
     app.use(express.favicon(__dirname + '/../public/favicon.ico'));
     app.use(express.static(__dirname + '/../public'));
diff --git a/lib/udc.js b/lib/udc.js
deleted file mode 100644
index a09954537..000000000
--- a/lib/udc.js
+++ /dev/null
@@ -1 +0,0 @@
-module.exports = {};
\ No newline at end of file
diff --git a/protocol.md b/protocol.md
index d01ac4b71..76ef0518c 100644
--- a/protocol.md
+++ b/protocol.md
@@ -298,7 +298,7 @@ This URL is used to manage OpenPGP certificates, making NodeCoin acting **like**
 ### udc/*
 
 This URL pattern manages all the data used by NodeCoin based on the PKS.
-* `amendments/init` is used to POST the initial keys used to forge the initial amendment (aka. Monetary Contract).
+* `amendments/init` is used to GET the initial keys used to forge the initial amendment (aka. Monetary Contract).
 * `amendments/submit` is used to POST an amendment with the required signatures (votes) in ASCII-Armored format.
 * `amendments/view/[AMENDMENT_ID]/members` is a Merkle URL referencing to the members of the Web of Trust.
 * `amendments/view/[AMENDMENT_ID]/self` shows the raw data of the amendment with the given `[AMENDMENT_ID]`.
-- 
GitLab