Files
self/app/src/utils/ofac.ts
Justin Hernandez 431f556542 chore: centralize license header checks (#952)
* chore: centralize license header scripts

* chore: run license header checks from root

* add header to other files

* add header to bundle

* add migration script and update check license headers

* convert license to mobile sdk

* migrate license headers

* remove headers from common; convert remaining

* fix headers

* add license header checks
2025-08-25 11:30:23 -07:00

69 lines
2.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
// SPDX-License-Identifier: BUSL-1.1
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
import { TREE_URL, TREE_URL_STAGING } from '@selfxyz/common/constants';
import type { OfacTree } from '@selfxyz/common/utils/types';
export type OfacVariant = 'passport' | 'id_card';
// Generic helper to fetch a single OFAC tree and validate the response shape.
const fetchTree = async (url: string): Promise<any> => {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`HTTP error fetching ${url}! status: ${res.status}`);
}
const responseData = await res.json();
if (responseData.status !== 'success' || !responseData.data) {
throw new Error(
`Failed to fetch tree from ${url}: ${
responseData.message || 'Invalid response format'
}`,
);
}
return responseData.data;
};
// Main public helper that retrieves the three OFAC trees depending on the variant (passport vs id_card).
export const fetchOfacTrees = async (
environment: 'prod' | 'stg',
variant: OfacVariant = 'passport',
): Promise<OfacTree> => {
const baseUrl = environment === 'prod' ? TREE_URL : TREE_URL_STAGING;
const ppNoNatUrl = `${baseUrl}/ofac/passport-no-nationality`;
const nameDobUrl = `${baseUrl}/ofac/name-dob${
variant === 'id_card' ? '-id' : ''
}`;
const nameYobUrl = `${baseUrl}/ofac/name-yob${
variant === 'id_card' ? '-id' : ''
}`;
// For ID cards, we intentionally skip fetching the (large) passport-number-tree.
if (variant === 'id_card') {
const [nameDobData, nameYobData] = await Promise.all([
fetchTree(nameDobUrl),
fetchTree(nameYobUrl),
]);
return {
passportNoAndNationality: null,
nameAndDob: nameDobData,
nameAndYob: nameYobData,
};
}
// Passport variant → fetch all three.
const [ppNoNatData, nameDobData, nameYobData] = await Promise.all([
fetchTree(ppNoNatUrl),
fetchTree(nameDobUrl),
fetchTree(nameYobUrl),
]);
return {
passportNoAndNationality: ppNoNatData,
nameAndDob: nameDobData,
nameAndYob: nameYobData,
};
};