Skip to content

Commit

Permalink
Removed obsolete code in RegisteredTenantGate.
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jan 9, 2024
1 parent 1ba211e commit 0720f67
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 375 deletions.
21 changes: 16 additions & 5 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{
"all": true,
"cache": false,
"extension": [".js"],
"include": ["dist/esm/src/**"],
"exclude": ["dist/esm/src/types/**"],
"reporter": ["text", "cobertura", "html"]
}
"extension": [
".js"
],
"include": [
"dist/esm/src/**"
],
"exclude": [
"dist/esm/src/types/**",
"dist/esm/src/**/*-types.js"
],
"reporter": [
"text",
"cobertura",
"html"
]
}
12 changes: 2 additions & 10 deletions src/dwn-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { HttpServerShutdownHandler } from './lib/http-server-shutdown-handler.js
import { type Config, config as defaultConfig } from './config.js';
import { HttpApi } from './http-api.js';
import { setProcessHandlers } from './process-handlers.js';
import { RegisteredTenantGate } from './registered-tenant-gate.js';
import { getDWNConfig, getDialectFromURI } from './storage.js';
import { WsApi } from './ws-api.js';
import { RegistrationManager } from './registration/registration-manager.js';
Expand Down Expand Up @@ -59,21 +58,14 @@ export class DwnServer {
new URL(this.config.tenantRegistrationStore),
);

let tenantGate: RegisteredTenantGate;
let registrationManager: RegistrationManager;
if (!this.dwn) {

tenantGate = new RegisteredTenantGate(
tenantGateDB,
this.config.registrationProofOfWorkEnabled,
termsOfService,
);
registrationManager = await RegistrationManager.create({ sqlDialect: tenantGateDB, termsOfService });

this.dwn = await Dwn.create(getDWNConfig(this.config, tenantGate));
this.dwn = await Dwn.create(getDWNConfig(this.config, registrationManager.getTenantGate()));
}

this.#httpApi = new HttpApi(this.config, this.dwn, tenantGate, registrationManager);
this.#httpApi = new HttpApi(this.config, this.dwn, registrationManager);

await this.#httpApi.start(this.config.port, () => {
log.info(`HttpServer listening on port ${this.config.port}`);
Expand Down
41 changes: 12 additions & 29 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { config } from './config.js';
import { type DwnServerError } from './dwn-error.js';
import { jsonRpcApi } from './json-rpc-api.js';
import { requestCounter, responseHistogram } from './metrics.js';
import type { RegisteredTenantGate } from './registered-tenant-gate.js';
import type { RegistrationManager } from './registration/registration-manager.js';

const packageJson = process.env.npm_package_json ? JSON.parse(readFileSync(process.env.npm_package_json).toString()) : {};
Expand All @@ -28,17 +27,18 @@ export class HttpApi {
#config: Config;
#api: Express;
#server: http.Server;
tenantGate: RegisteredTenantGate;
registrationManager: RegistrationManager;
dwn: Dwn;

constructor(config: Config, dwn: Dwn, tenantGate: RegisteredTenantGate, registrationManager: RegistrationManager) {
constructor(config: Config, dwn: Dwn, registrationManager: RegistrationManager) {
this.#config = config;
this.#api = express();
this.#server = http.createServer(this.#api);
this.dwn = dwn;
this.tenantGate = tenantGate;
this.registrationManager = registrationManager;

if (registrationManager !== undefined) {
this.registrationManager = registrationManager;
}

this.#setupMiddleware();
this.#setupRoutes();
Expand Down Expand Up @@ -196,35 +196,21 @@ export class HttpApi {
#setupRegistrationRoutes(): void {
if (this.#config.registrationProofOfWorkEnabled) {
this.#api.get('/register/proof-of-work', async (_req: Request, res: Response) => {
const proofOfWorkChallenge = await this.registrationManager.getProofOfWorkChallenge();
const proofOfWorkChallenge = this.registrationManager.getProofOfWorkChallenge();
res.json(proofOfWorkChallenge);
});
}

if (this.#config.termsOfServiceFilePath !== undefined) {
this.#api.get('/register/terms-of-service', (_req: Request, res: Response) => res.send(this.tenantGate.termsOfService));
this.#api.post('/register/terms-of-service', async (req: Request, res: Response) => {
try {
await this.tenantGate.handleTermsOfServicePost(req.body);
res.status(200).json({ success: true });
} catch (error) {
const dwnServerError = error as DwnServerError;

if (dwnServerError.code !== undefined) {
res.status(400).json({
success : false,
reason : dwnServerError.message,
});
} else {
console.log('Error handling terms-of-service POST:', error);
res.status(500).json({ success: false });
}
}
});
this.#api.get('/register/terms-of-service', (_req: Request, res: Response) => res.send(this.registrationManager.getTermsOfService()));
}

this.#api.post('/registration', async (req: Request, res: Response) => {
const requestBody = req.body;
console.log('Registration request:', requestBody);

try {
await this.registrationManager.handleRegistrationRequest(req.body);
await this.registrationManager.handleRegistrationRequest(requestBody);
res.status(200).json({ success: true });
} catch (error) {
const dwnServerError = error as DwnServerError;
Expand All @@ -240,9 +226,6 @@ export class HttpApi {
}

async start(port: number, callback?: () => void): Promise<http.Server> {
if (this.tenantGate) {
await this.tenantGate.initialize();
}
this.#listen(port, callback);
return this.#server;
}
Expand Down
152 changes: 0 additions & 152 deletions src/registered-tenant-gate.ts

This file was deleted.

35 changes: 28 additions & 7 deletions src/registration/registration-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ import type { Dialect } from "@tbd54566975/dwn-sql-store";
import { ProofOfWorkManager } from "./proof-of-work-manager.js";
import { ProofOfWork } from "./proof-of-work.js";
import { RegistrationStore } from "./registration-store.js";
import type { RegistrationRequest } from "./registration-types.js";
import type { RegistrationData, RegistrationRequest } from "./registration-types.js";
import type { ProofOfWorkChallengeModel } from "./proof-of-work-types.js";
import { DwnServerError, DwnServerErrorCode } from "../dwn-error.js";
import type { TenantGate } from "@tbd54566975/dwn-sdk-js";
import { RegistrationTenantGate } from "./registration-tenant-gate.js";

export class RegistrationManager {
private tenantGate: TenantGate;
private proofOfWorkManager: ProofOfWorkManager;
private registrationStore: RegistrationStore;

private termsOfServiceHash?: string;
private termsOfService?: string;

public getTenantGate(): TenantGate {
return this.tenantGate;
}

public getTermsOfService(): string {
return this.termsOfService;
}

public getTermsOfServiceHash(): string {
return this.termsOfServiceHash;
}

private constructor (termsOfService?: string) {
if (termsOfService) {
this.termsOfServiceHash = ProofOfWork.hashAsHexString([termsOfService]);
Expand All @@ -31,17 +42,19 @@ export class RegistrationManager {
const { termsOfService, sqlDialect } = input;

// Initialize and start ProofOfWorkManager.
const proofOfWorkManager = new RegistrationManager(termsOfService);
proofOfWorkManager.proofOfWorkManager = await ProofOfWorkManager.create({
const registrationManager = new RegistrationManager(termsOfService);
registrationManager.proofOfWorkManager = await ProofOfWorkManager.create({
autoStart: true,
desiredSolveCountPerMinute: 10,
initialMaximumHashValue: '00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'
});

// Initialize RegistrationStore.
proofOfWorkManager.registrationStore = await RegistrationStore.create(sqlDialect);

return proofOfWorkManager;
const registrationStore = await RegistrationStore.create(sqlDialect);
registrationManager.registrationStore = registrationStore;
registrationManager.tenantGate = await RegistrationTenantGate.create(registrationStore, registrationManager.getTermsOfServiceHash());

return registrationManager;
}

public getProofOfWorkChallenge(): ProofOfWorkChallengeModel {
Expand All @@ -67,6 +80,14 @@ export class RegistrationManager {
});

// Store tenant registration data in database.
await this.registrationStore.insertOrUpdateTenantRegistration(registrationRequest.registrationData);
await this.recordTenantRegistration(registrationRequest.registrationData);
}

/**
* Records the given registration data in the database.
* Exposed as a public method for testing purposes.
*/
public async recordTenantRegistration(registrationData: RegistrationData): Promise<void> {
await this.registrationStore.insertOrUpdateTenantRegistration(registrationData);
}
}
Loading

0 comments on commit 0720f67

Please sign in to comment.