Updates
This commit is contained in:
+17
-14
@@ -24,24 +24,30 @@ export type ApplicationRequest = {
|
||||
};
|
||||
|
||||
export type ApplicationRouterDependencies = {
|
||||
|
||||
/** Authentication service. */
|
||||
auth: AuthSecp256k1;
|
||||
};
|
||||
|
||||
const accountSchema = z.object({
|
||||
'x-public-key': z.string(),
|
||||
'x-signature': z.string(),
|
||||
'x-timestamp': z.coerce.number(),
|
||||
}).transform((data) => ({
|
||||
publicKey: data['x-public-key'],
|
||||
signature: data['x-signature'],
|
||||
timestamp: data['x-timestamp'],
|
||||
}));
|
||||
const accountSchema = z
|
||||
.object({
|
||||
'x-public-key': z.string(),
|
||||
'x-signature': z.string(),
|
||||
'x-timestamp': z.coerce.number(),
|
||||
})
|
||||
.transform((data) => ({
|
||||
publicKey: data['x-public-key'],
|
||||
signature: data['x-signature'],
|
||||
timestamp: data['x-timestamp'],
|
||||
}));
|
||||
|
||||
/** Exact-match application routing shared by every wire transport. */
|
||||
export class ApplicationRouter {
|
||||
/** @param routes - Validated route table keyed by exact path. */
|
||||
private constructor(private readonly deps: ApplicationRouterDependencies, private readonly routes: ReadonlyMap<string, RouteDefinition>) {}
|
||||
private constructor(
|
||||
private readonly deps: ApplicationRouterDependencies,
|
||||
private readonly routes: ReadonlyMap<string, RouteDefinition>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Load and validate the complete route table before accepting traffic.
|
||||
@@ -74,11 +80,8 @@ export class ApplicationRouter {
|
||||
* @param connection - Shared connection stream for this transport session.
|
||||
*/
|
||||
async dispatch(request: ApplicationRequest, connection: BaseStream): Promise<void> {
|
||||
// Authenticate the headers on the request.
|
||||
const { publicKey, signature, timestamp } = accountSchema.parse(request.headers);
|
||||
// Authenticate the headers on the request. (TODO: Remove the defaults, just here for testing)
|
||||
// const publicKey = request.headers?.['x-public-key'] || 'public-key';
|
||||
// const signature = request.headers?.['x-signature'] || 'signature';
|
||||
// const timestamp = request.headers?.['x-timestamp'] || Date.now();
|
||||
|
||||
// Make sure the request signature is valid and hasnt been used before
|
||||
await this.deps.auth.verifyUniqueRequest(signature);
|
||||
|
||||
@@ -26,7 +26,7 @@ export const up = async (db: Kysely<DatabaseTables>): Promise<void> => {
|
||||
.addColumn('signature', 'text', (col) => col.notNull())
|
||||
.addPrimaryKeyConstraint('pk_resource_data', [ 'resource_id', 'public_key' ])
|
||||
.execute();
|
||||
|
||||
|
||||
// Table for authed requests
|
||||
// We will store the signature and the timestamp of the request, and we will clear out rows that are older than our msTimeout for our auth
|
||||
await db.schema
|
||||
@@ -46,5 +46,7 @@ export const up = async (db: Kysely<DatabaseTables>): Promise<void> => {
|
||||
export const down = async (db: Kysely<DatabaseTables>): Promise<void> => {
|
||||
await db.schema.dropTable('resource_data').ifExists()
|
||||
.execute();
|
||||
await db.schema.dropTable('authed_requests').ifExists().execute();
|
||||
|
||||
await db.schema.dropTable('authed_requests').ifExists()
|
||||
.execute();
|
||||
};
|
||||
|
||||
@@ -28,6 +28,7 @@ export interface ResourceDataTable {
|
||||
}
|
||||
|
||||
export interface AuthedRequestsTable {
|
||||
|
||||
/** Signature of the request. */
|
||||
signature: string;
|
||||
|
||||
@@ -35,29 +36,6 @@ export interface AuthedRequestsTable {
|
||||
timestamp: Timestamp;
|
||||
}
|
||||
|
||||
// export interface PaymentsTable {
|
||||
// /** Unique identifier for the payment. */
|
||||
// payment_id: string;
|
||||
|
||||
// /** Public key of the account in the transaction */
|
||||
// public_key: string;
|
||||
|
||||
// /** Amount of the payment. This can be positive or negative.*/
|
||||
// amount: number;
|
||||
|
||||
// /** Timestamp of the payment. */
|
||||
// timestamp: Timestamp;
|
||||
|
||||
// /** Signature of the payment. */
|
||||
// signature: string;
|
||||
|
||||
// /** Hash of the message that was signed. */
|
||||
// message_hash: string;
|
||||
|
||||
// /** Resource ID of the payment. */
|
||||
// resource_id: string;
|
||||
// }
|
||||
|
||||
/** Complete Kysely schema mapping for the sync server database. */
|
||||
export interface DatabaseTables {
|
||||
resource_data: ResourceDataTable;
|
||||
|
||||
Reference in New Issue
Block a user