Skip to content

Commit

Permalink
aesirxio#3: Step 2.a: Get concordium account nonce#3
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuong committed Feb 24, 2023
1 parent 387f91b commit aff4191
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 99 deletions.
9 changes: 1 addition & 8 deletions api/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,4 @@ PORT=80
DBUSER=web3Id
DBPASS=demo
DBHOST=localhost:27017
DBNAME=web3Id
CONCORDIUM_NODE=
CONCORDIUM_PORT=9095
SMARTCONTRACT_NAME=
SMARTCONTRACT_INDEX=0
SMARTCONTRACT_SUBINDEX=0
PRIVATE_KEY=
SMARTCONTRACT_RAWSCHEMA=//
DBNAME=web3Id
1 change: 0 additions & 1 deletion api/api-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ router.route("/preregistration").post(preregistrationController.add);
// Account routes
const accountController = require("./controllers/accountController");
router.route("/account/v1/:account/nonce").get(accountController.getNonce);
router.route("/prerregistration/id/:id/account/:account").put(accountController.update);


// Export API routes
Expand Down
88 changes: 0 additions & 88 deletions api/controllers/accountController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Import account model
const Account = require("../models/accountModel");
const Preregistration = require("../models/preregistrationModel");

const Concordium = require("../web3/concordium");
const concordium = new Concordium();

exports.getNonce = async (req, res) => {
const account = req.params.account;
Expand Down Expand Up @@ -34,87 +30,3 @@ exports.getNonce = async (req, res) => {
});
});
};

exports.update = async (req, res) => {

const account = req.params.account;
const signature = req.query.signature;

// Validate missing signature
if (!signature)
{
res.status(406).json({error: "Missing signature"}).end();
}

// Validate signature not base64
if (!signature.match(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/))
{
res.status(406).json({error: "Signature not base64"}).end();
}

// Validate signature not json
try {
let isJson = JSON.parse(signature);

if (isJson && typeof isJson === "object") {
res.status(406).json({error: "Signature not json"}).end();
}
} catch (e) {}

// Validate invalid format of the account
if (!account || !account.match(/^[a-zA-Z0-9]+$/))
{
res.status(500).json({error: "Account is not valid"}).end();
}

// Validate account in collection
Account.findOne({ address: account }, async (err, accountObj) => {

if (err) {
res.status(500).end();
}

if (accountObj === null) {
res.status(404).end();
}

const nonce = accountObj.nonce;

// Validate signature by concordium
if (!(await concordium.validateAccount(
String(nonce),
JSON.parse(Buffer.from(signature, "base64").toString()),
account ))
) {
// Clear nonce in the account when signature verification failed
Account.updateOne({ address: account }, { nonce: null }, () => {});
res.status(403).end();
}

// Clear nonce in the account after signature verification
Account.updateOne({ address: account }, { nonce: null }, () => {});
});

// Validate preregistration in collection
Preregistration.findOne({ id: req.params.id }, (err, preregistrationObj) => {

if (err) {
res.status(500).end();
return;
}

if (preregistrationObj === null) {
res.status(404).end();
}

// Validate Id already linked to another account
if (preregistrationObj.account && preregistrationObj.account !== account)
{
res.status(406).end();
}

Preregistration.updateOne({ id: req.params.id }, { account: account }, () => {
res.json({result: true}).status(201).end();
});
});
};
2 changes: 1 addition & 1 deletion api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ app.get("/", (req, res) => res.status(404).end());
app.use("/", apiRoutes);
// Launch app to listen to specified port
app.listen(port, function () {
console.log("Running web3 Id API on port " + port);
console.log("Running web3 backend API on port " + port);
});
2 changes: 1 addition & 1 deletion api/models/def/accountDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports.account = function () {
},
nonce: {
type: Number,
required: false,
required: true,
},
};
};

0 comments on commit aff4191

Please sign in to comment.