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
6 changes: 6 additions & 0 deletions packages/core-ts/src/address/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ErrorCode } from "./errors";
export type AddressKind = "G" | "M" | "C";

/**
* Severity levels for validation warnings.
* Used with `minSeverityLevel` to filter warnings by importance.
*/
export type WarningSeverity = "info" | "warn" | "error";

export type WarningCode =
| "NON_CANONICAL_ADDRESS"
| "NON_CANONICAL_ROUTING_ID"
Expand Down
24 changes: 20 additions & 4 deletions packages/core-ts/src/routing/extract.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { RoutingInput, RoutingResult } from "./types";
import { Warning } from "../address/types";
import { Warning, WarningSeverity } from "../address/types";
import { parse } from "../address/parse";
import { AddressParseError } from "../address/errors";
import { normalizeMemoTextId } from "./memo";

const SEVERITY_ORDER: Record<WarningSeverity, number> = {
info: 0,
warn: 1,
error: 2,
};

function filterBySeverity(
warnings: Warning[],
minSeverity: WarningSeverity
): Warning[] {
const threshold = SEVERITY_ORDER[minSeverity];
return warnings.filter((w) => SEVERITY_ORDER[w.severity] >= threshold);
}

export class ExtractRoutingError extends Error {
constructor(message: string) {
super(message);
Expand Down Expand Up @@ -46,6 +60,8 @@ function assertRoutableAddress(destination: string): void {
export function extractRouting(input: RoutingInput): RoutingResult {
assertRoutableAddress(input.destination);

const minSeverity = input.minSeverityLevel ?? "info";

let parsed;
try {
parsed = parse(input.destination);
Expand Down Expand Up @@ -90,7 +106,7 @@ export function extractRouting(input: RoutingInput): RoutingResult {
destinationBaseAccount: null,
routingId: null,
routingSource: "none",
warnings,
warnings: filterBySeverity(warnings, minSeverity),
};
}

Expand Down Expand Up @@ -120,7 +136,7 @@ export function extractRouting(input: RoutingInput): RoutingResult {
destinationBaseAccount: parsed.baseG,
routingId: parsed.muxedId,
routingSource: "muxed",
warnings,
warnings: filterBySeverity(warnings, minSeverity),
};
}

Expand Down Expand Up @@ -178,6 +194,6 @@ export function extractRouting(input: RoutingInput): RoutingResult {
destinationBaseAccount: parsed.address,
routingId,
routingSource,
warnings,
warnings: filterBySeverity(warnings, minSeverity),
};
}
8 changes: 7 additions & 1 deletion packages/core-ts/src/routing/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorCode, Warning, WarningCode } from "../address/types";
import { ErrorCode, Warning, WarningCode, WarningSeverity } from "../address/types";

export type RoutingSource = "muxed" | "memo" | "none";

Expand All @@ -7,6 +7,12 @@ export type RoutingInput = {
memoType: string;
memoValue: string | null;
sourceAccount: string | null;
/**
* Minimum severity level for warnings to include in the result.
* Warnings below this threshold are filtered out.
* Defaults to `'info'` (all warnings are returned).
*/
minSeverityLevel?: WarningSeverity;
};

export type KnownMemoType = "none" | "id" | "text" | "hash" | "return";
Expand Down
Loading