Skip to content
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
2 changes: 2 additions & 0 deletions src/configValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface ConfigValidationErrorType {
severity: "error" | "warning";
}

export type ConfigValidationErrorType = ConfigValidationIssue;

export interface ConfigValidation {
valid: boolean;
errors: ConfigValidationErrorType[];
Expand Down
38 changes: 38 additions & 0 deletions src/dedup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
import crypto from "crypto";

export interface IdempotencyKeyParams {
invoiceId: string;
payer: string;
amount: bigint;
nonce?: string;
}

const knownKeys = new Set<string>();

/**
* Generate a canonical idempotency key for payment deduplication.
*
* Computes a deterministic SHA-256 hex digest over `{invoiceId}:{payer}:{amount}`
* (with an optional `:{nonce}` suffix when supplied).
*/
export function generateIdempotencyKey(params: IdempotencyKeyParams): string {
const base = `${params.invoiceId}:${params.payer}:${params.amount.toString()}`;
const payload = params.nonce !== undefined ? `${base}:${params.nonce}` : base;
return crypto.createHash("sha256").update(payload).digest("hex");
}

/** Check if an idempotency key has already been registered in memory. */
export function isKnownKey(key: string): boolean {
return knownKeys.has(key);
}

/** Register an idempotency key in memory. */
export function registerKey(key: string): void {
knownKeys.add(key);
}

/** Clear all registered idempotency keys (useful for test teardown). */
export function clearKeys(): void {
knownKeys.clear();
}

export class Deduplicator<T> {
private _inflight = new Map<string, Promise<T>>();
private _hits = 0;
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ export {
buildRevealTransactionFromStorage,
} from "./confidential.js";

export { Deduplicator } from "./dedup.js";
export {
Deduplicator,
generateIdempotencyKey,
isKnownKey,
registerKey,
clearKeys,
} from "./dedup.js";
export type { IdempotencyKeyParams } from "./dedup.js";

export { TxQueue } from "./queue.js";

Expand Down
Loading
Loading