Skip to content
Snippets Groups Projects
Commit 5d05d17b authored by Adrien Béraud's avatar Adrien Béraud
Browse files

add addr queries

parent 102a6df5
Branches
No related tags found
No related merge requests found
...@@ -21,9 +21,7 @@ var regContract; ...@@ -21,9 +21,7 @@ var regContract;
var reg; var reg;
function unlockAccount() { function unlockAccount() {
//account = web3.eth.accounts[0];
web3.personal.unlockAccount(coinbase, "toto"); web3.personal.unlockAccount(coinbase, "toto");
//loadContract();
} }
function getRemainingGaz() { function getRemainingGaz() {
...@@ -105,6 +103,10 @@ function initContract() { ...@@ -105,6 +103,10 @@ function initContract() {
}); });
} }
function isHashZero(h) {
return h == "0x" || h == "0x0000000000000000000000000000000000000000";
}
function startServer() { function startServer() {
console.log("Starting web server"); console.log("Starting web server");
var app = express(); var app = express();
...@@ -112,12 +114,29 @@ function startServer() { ...@@ -112,12 +114,29 @@ function startServer() {
app.use(bodyParser.json()); app.use(bodyParser.json());
app.get("/name/:name", function(req, http_res) { app.get("/name/:name", function(req, http_res) {
reg.addr(req.params.name, function(err, res) { reg.addr(req.params.name, function(err, res) {
http_res.end(JSON.stringify({"addr": res})); if (isHashZero(res)) {
http_res.status(404).end(JSON.stringify({"error": "name not registred"}));
} else {
http_res.end(JSON.stringify({"name": req.params.name,"addr": res}));
}
}); });
}); });
app.get("/owner/:name", function(req, http_res) { app.get("/name/:name/owner", function(req, http_res) {
reg.owner(req.params.name, function(err, res) { reg.owner(req.params.name, function(err, res) {
http_res.end(JSON.stringify({"owner": res})); if (isHashZero(res)) {
http_res.status(404).end(JSON.stringify({"error": "name not registred"}));
} else {
http_res.end(JSON.stringify({"name": req.params.name,"owner": res}));
}
//http_res.end(JSON.stringify({"name": req.params.name,"owner": res}));
});
});
app.get("/addr/:addr", function(req, http_res) {
if (!req.params.addr.startsWith("0x"))
req.params.addr = "0x" + req.params.addr;
reg.name(req.params.addr, function(err, res) {
var b = new Buffer(res.substr(2, res.indexOf("000")-2), 'hex');
http_res.end(JSON.stringify({"name": b.toString()}));
}); });
}); });
app.post("/name/:name", function(req, http_res) { app.post("/name/:name", function(req, http_res) {
...@@ -127,8 +146,7 @@ function startServer() { ...@@ -127,8 +146,7 @@ function startServer() {
} }
console.log("Got reg request (" + req.params.name + " -> " + req.body.addr + ") from " + req.body.owner); console.log("Got reg request (" + req.params.name + " -> " + req.body.addr + ") from " + req.body.owner);
reg.addr(req.params.name, function(err, res) { reg.addr(req.params.name, function(err, res) {
if (res == "0x" || res == "0x0000000000000000000000000000000000000000") { if (isHashZero(res)) {
console.log("Remaing gaz: " + getRemainingGaz());
unlockAccount(); unlockAccount();
reg.reserveFor.sendTransaction(req.params.name, req.body.owner, req.body.addr, { reg.reserveFor.sendTransaction(req.params.name, req.body.owner, req.body.addr, {
from: coinbase, from: coinbase,
......
...@@ -85,7 +85,7 @@ contract GlobalRegistrar is Registrar { ...@@ -85,7 +85,7 @@ contract GlobalRegistrar is Registrar {
function addr(bytes32 _name) constant returns (address) { return m_toRecord[_name].primary; } function addr(bytes32 _name) constant returns (address) { return m_toRecord[_name].primary; }
function register(bytes32 _name) constant returns (address) { return m_toRecord[_name].subRegistrar; } function register(bytes32 _name) constant returns (address) { return m_toRecord[_name].subRegistrar; }
function content(bytes32 _name) constant returns (bytes32) { return m_toRecord[_name].content; } function content(bytes32 _name) constant returns (bytes32) { return m_toRecord[_name].content; }
function name(address _owner) constant returns (bytes32 o_name) { return m_toName[_owner]; } function name(address _a) constant returns (bytes32 o_name) { return m_toName[_a]; }
mapping (address => bytes32) m_toName; mapping (address => bytes32) m_toName;
mapping (bytes32 => Record) m_toRecord; mapping (bytes32 => Record) m_toRecord;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment