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
No related branches found
No related tags found
No related merge requests found
......@@ -21,9 +21,7 @@ var regContract;
var reg;
function unlockAccount() {
//account = web3.eth.accounts[0];
web3.personal.unlockAccount(coinbase, "toto");
//loadContract();
}
function getRemainingGaz() {
......@@ -105,6 +103,10 @@ function initContract() {
});
}
function isHashZero(h) {
return h == "0x" || h == "0x0000000000000000000000000000000000000000";
}
function startServer() {
console.log("Starting web server");
var app = express();
......@@ -112,12 +114,29 @@ function startServer() {
app.use(bodyParser.json());
app.get("/name/:name", function(req, http_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) {
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) {
......@@ -127,8 +146,7 @@ function startServer() {
}
console.log("Got reg request (" + req.params.name + " -> " + req.body.addr + ") from " + req.body.owner);
reg.addr(req.params.name, function(err, res) {
if (res == "0x" || res == "0x0000000000000000000000000000000000000000") {
console.log("Remaing gaz: " + getRemainingGaz());
if (isHashZero(res)) {
unlockAccount();
reg.reserveFor.sendTransaction(req.params.name, req.body.owner, req.body.addr, {
from: coinbase,
......
......@@ -85,7 +85,7 @@ contract GlobalRegistrar is Registrar {
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 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 (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