Skip to content

Commit

Permalink
add /info.json endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block committed Nov 16, 2023
1 parent 760a16d commit 27ef40a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import cors from 'cors';
import type { Express, Request, Response } from 'express';
import express from 'express';
import { readFileSync } from 'fs';
import http from 'http';
import log from 'loglevel';
import { register } from 'prom-client';
Expand All @@ -20,10 +21,15 @@ import {
JsonRpcErrorCodes,
} from './lib/json-rpc.js';

import { config } from './config.js';
import { jsonRpcApi } from './json-rpc-api.js';
import { requestCounter, responseHistogram } from './metrics.js';
import type { ProofOfWork } from './pow.js';

const packagejson = process.env.npm_package_json
? JSON.parse(readFileSync(process.env.npm_package_json).toString())
: {};

export class HttpApi {
#api: Express;
#server: http.Server;
Expand Down Expand Up @@ -206,6 +212,22 @@ export class HttpApi {
if (this.pow) {
this.pow.setupRoutes(this.#api);
}

this.#api.get('/info.json', (req, res) => {
res.setHeader('content-type', 'application/json');
const registrationRequirements: string[] = [];
if (this.pow) {
registrationRequirements.push('proof-of-work-sha256-v0');
}

res.json({
server: process.env.npm_package_name,
maxFileSize: config.maxRecordDataSize,
registrationRequirements: registrationRequirements,
version: packagejson.version,
sdkVersion: packagejson.dependencies['@tbd54566975/dwn-sdk-js'],
});
});
}

#listen(port: number, callback?: () => void): void {
Expand Down
4 changes: 2 additions & 2 deletions src/pow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export class ProofOfWork {
}

setupRoutes(server: Express): void {
server.get('/register', (req: Request, res: Response) =>
server.get('/register/pow', (req: Request, res: Response) =>
this.getChallenge(req, res),
);
server.post('/register', (req: Request, res: Response) =>
server.post('/register/pow', (req: Request, res: Response) =>
this.verifyChallenge(req, res),
);
}
Expand Down
43 changes: 26 additions & 17 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('http api', function () {
await clearDwn();
});

describe('/register', function () {
describe('/register/pow', function () {
it('returns a register challenge', async function () {
const response = await fetch('http://localhost:3000/register');
const response = await fetch('http://localhost:3000/register/pow');
expect(response.status).to.equal(200);
const body = (await response.json()) as {
challenge: string;
Expand All @@ -77,7 +77,9 @@ describe('http api', function () {
});

it('accepts a correct registration challenge', async function () {
const challengeResponse = await fetch('http://localhost:3000/register');
const challengeResponse = await fetch(
'http://localhost:3000/register/pow',
);
expect(challengeResponse.status).to.equal(200);
const body = (await challengeResponse.json()) as {
challenge: string;
Expand All @@ -92,7 +94,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const submitResponse = await fetch('http://localhost:3000/register', {
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand All @@ -113,7 +115,9 @@ describe('http api', function () {
continue;
}

const challengeResponse = await fetch('http://localhost:3000/register');
const challengeResponse = await fetch(
'http://localhost:3000/register/pow',
);
expect(challengeResponse.status).to.equal(200);
const body = (await challengeResponse.json()) as {
challenge: string;
Expand Down Expand Up @@ -151,22 +155,27 @@ describe('http api', function () {
'ms',
);

const submitResponse = await fetch('http://localhost:3000/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
challenge: body.challenge,
response: response,
did: p.did,
}),
});
const submitResponse = await fetch(
'http://localhost:3000/register/pow',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
challenge: body.challenge,
response: response,
did: p.did,
}),
},
);

expect(submitResponse.status).to.equal(200);
}
}).timeout(120000);

it('rejects an invalid nonce', async function () {
const challengeResponse = await fetch('http://localhost:3000/register');
const challengeResponse = await fetch(
'http://localhost:3000/register/pow',
);
expect(challengeResponse.status).to.equal(200);
const body = (await challengeResponse.json()) as {
challenge: string;
Expand All @@ -182,7 +191,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const submitResponse = await fetch('http://localhost:3000/register', {
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand All @@ -204,7 +213,7 @@ describe('http api', function () {
response = generateNonce(5);
}

const submitResponse = await fetch('http://localhost:3000/register', {
const submitResponse = await fetch('http://localhost:3000/register/pow', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand Down

0 comments on commit 27ef40a

Please sign in to comment.