Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api endpoint /commitment/send #332

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/mainstay_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ request.post(options, (error, response, body) => {
curl --header "Content-Type: application/json" --request POST --data '{
"position": "0",
"token": "4c8c006d-4cee-4fef-8e06-bb8112db6314",
"commitment": "f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2abf7dbcc8931",
}' http://localhost:9000/api/v1/commitment/send
"commitment": "f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2a2bf7dbcc8931"
}' https://testnet.mainstay.xyz/api/v1/commitment/send
```

*response*
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ctrl_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ module.exports = {
return res.json({error: 'Incorrect commitment'});
}
if (/[0-9A-Fa-f]{64}/g.test(payload.commitment) === false) {
return res.json({error: 'Non hex or non 64 byte commitment'});
return res.json({error: 'Non hex or non 32 byte commitment'});
}

const data = await models.clientDetails.find({client_position: payload.position});
Expand Down
5 changes: 5 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const {jwt: {secret: jwtSecret}, onfido: {webhook: {secret: onfidoWebhookSecret}
function makeApiRoutes(app) {
const router = express.Router();

// for parsing application/json
router.use(express.json());
// for parsing application/x-www-form-urlencoded
router.use(express.urlencoded({extended: true}));

router.get('/', apiController.index);
router.get('/latestattestation', apiController.latest_attestation);
router.get('/latestcommitment', apiController.latest_commitment);
Expand Down
13 changes: 7 additions & 6 deletions test/controllers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('Test Api Controllers', () => {
/// pvtKey = bac52bbea2194e7ea1cd3da6585b66d28f1a7a3683eca91af4ba6373d323d24f
///

// non 64 byte string
// non 32 byte string
it('Route: /api/v1/commitment/send', () => {
const req = mockHttp.createRequest(
{
Expand All @@ -269,6 +269,7 @@ describe('Test Api Controllers', () => {
assert(json.error === BAD_COMMITMENT);
});

// non hex string
it('Route: /api/v1/commitment/send', () => {
const req = mockHttp.createRequest(
{
Expand All @@ -277,7 +278,7 @@ describe('Test Api Controllers', () => {
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
commitment: 'f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2abf7dbcc8*&/',
commitment: 'f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2a2bf7dbcc8*&$',
}
});
const res = mockHttp.createResponse();
Expand All @@ -286,7 +287,7 @@ describe('Test Api Controllers', () => {
assert(json.error === BAD_COMMITMENT);
});

// non 64 byte string
// non 32 byte string
it('Route: /ctrl/sendcommitment', () => {
const req = mockHttp.createRequest(
{
Expand All @@ -301,7 +302,7 @@ describe('Test Api Controllers', () => {
const res = mockHttp.createResponse();
ctrlControllers.ctrl_send_commitment(req, res);
const json = JSON.parse(res._getData());
assert(json.error === 'Non hex or non 64 byte commitment');
assert(json.error === 'Non hex or non 32 byte commitment');
});

// non hex string
Expand All @@ -313,13 +314,13 @@ describe('Test Api Controllers', () => {
body: {
position: 0,
token: '4c8c006d-4cee-4fef-8e06-bb8112db6314',
commitment: 'f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2abf7dbcc8*&/',
commitment: 'f3d424bf830dbd59eebc3f0a23491a266b7158635188e47b0e2a2bf7dbcc82*&$',
}
});
const res = mockHttp.createResponse();
ctrlControllers.ctrl_send_commitment(req, res);
const json = JSON.parse(res._getData());
assert(json.error === 'Non hex or non 64 byte commitment');
assert(json.error === 'Non hex or non 32 byte commitment');
});

it('Route: /api/v1/slotexpiry?slot_id=0', async () => {
Expand Down
Loading