-
Notifications
You must be signed in to change notification settings - Fork 48
feat: update validate address endpoint #1263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+215
−142
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import axios from 'axios' | ||
| import { XMLParser } from 'fast-xml-parser' | ||
| import { Logger } from '@shapeshiftoss/logger' | ||
| import { getAddress, isAddress } from 'viem' | ||
|
|
||
| const OFAC_SDN_URL = 'https://sanctionslistservice.ofac.treas.gov/api/PublicationPreview/exports/SDN_ADVANCED.XML' | ||
| const REFRESH_INTERVAL_MS = 24 * 60 * 60 * 1000 // 24 hours | ||
|
|
||
| interface OfacArgs { | ||
| logger: Logger | ||
| } | ||
|
|
||
| export class Ofac { | ||
| private sanctionedAddresses: Set<string> = new Set() | ||
| private logger: Logger | ||
| private refreshInterval: NodeJS.Timeout | undefined | ||
|
|
||
| constructor(args: OfacArgs) { | ||
| this.logger = args.logger | ||
| } | ||
|
|
||
| async initialize(): Promise<void> { | ||
| try { | ||
| this.sanctionedAddresses = await this.fetchAndParseOfacList() | ||
| this.logger.info({ addressCount: this.sanctionedAddresses.size }, 'OFAC service initialized') | ||
|
|
||
| this.refreshInterval = setInterval(async () => { | ||
| try { | ||
| this.sanctionedAddresses = await this.fetchAndParseOfacList() | ||
| this.logger.info({ addressCount: this.sanctionedAddresses.size }, 'OFAC list refreshed') | ||
| } catch (err) { | ||
| this.logger.error({ err }, 'Failed to refresh OFAC list') | ||
| } | ||
| }, REFRESH_INTERVAL_MS) | ||
| } catch (err) { | ||
| this.logger.error({ err }, 'Failed to initialize OFAC service') | ||
| throw err | ||
| } | ||
kaladinlight marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| async validateAddress(address: string): Promise<{ valid: boolean }> { | ||
| if (this.sanctionedAddresses.has(this.normalizeAddress(address))) { | ||
| return { valid: false } | ||
| } | ||
|
|
||
| return { valid: true } | ||
| } | ||
|
|
||
| private async fetchAndParseOfacList(): Promise<Set<string>> { | ||
| const { data } = await axios.get<string>(OFAC_SDN_URL, { responseType: 'text' }) | ||
| return this.parseXml(data) | ||
| } | ||
kaladinlight marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private parseXml(xmlData: string): Set<string> { | ||
| const addresses = new Set<string>() | ||
|
|
||
| const parser = new XMLParser({ | ||
| ignoreAttributes: false, | ||
| attributeNamePrefix: '@_', | ||
| removeNSPrefix: true, | ||
| numberParseOptions: { hex: false, leadingZeros: false }, | ||
| }) | ||
|
|
||
| const result = parser.parse(xmlData) | ||
|
|
||
| const sanctions = result?.Sanctions | ||
| if (!sanctions) throw new Error('No Sanctions element found in OFAC XML') | ||
|
|
||
| const featureTypeIds = new Map<number, string>() | ||
| const referenceValueSets = sanctions.ReferenceValueSets | ||
|
|
||
| if (referenceValueSets?.FeatureTypeValues?.FeatureType) { | ||
| const featureTypes = Array.isArray(referenceValueSets.FeatureTypeValues.FeatureType) | ||
| ? referenceValueSets.FeatureTypeValues.FeatureType | ||
| : [referenceValueSets.FeatureTypeValues.FeatureType] | ||
|
|
||
| for (const featureType of featureTypes) { | ||
| const name = String(featureType['#text'] ?? featureType ?? '') | ||
|
|
||
| if (name.includes('Digital Currency Address')) { | ||
| const id = parseInt(featureType['@_ID'], 10) | ||
| if (!isNaN(id)) featureTypeIds.set(id, name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (featureTypeIds.size === 0) throw new Error('No Digital Currency Address feature types found') | ||
|
|
||
| const parties = sanctions.DistinctParties?.DistinctParty | ||
| if (!parties) throw new Error('No DistinctParty entries found') | ||
|
|
||
| const partyList = Array.isArray(parties) ? parties : [parties] | ||
|
|
||
| for (const party of partyList) { | ||
| const profiles = party.Profile | ||
| if (!profiles) continue | ||
|
|
||
| const profileList = Array.isArray(profiles) ? profiles : [profiles] | ||
|
|
||
| for (const profile of profileList) { | ||
| const features = profile.Feature | ||
| if (!features) continue | ||
|
|
||
| const featureList = Array.isArray(features) ? features : [features] | ||
|
|
||
| for (const feature of featureList) { | ||
| const featureTypeId = parseInt(feature['@_FeatureTypeID'], 10) | ||
| if (!featureTypeIds.has(featureTypeId)) continue | ||
|
|
||
| const featureVersions = feature.FeatureVersion | ||
| if (!featureVersions) continue | ||
|
|
||
| const featureVersionList = Array.isArray(featureVersions) ? featureVersions : [featureVersions] | ||
|
|
||
| for (const featureVersion of featureVersionList) { | ||
| const versionDetails = featureVersion.VersionDetail | ||
| if (!versionDetails) continue | ||
|
|
||
| const detailList = Array.isArray(versionDetails) ? versionDetails : [versionDetails] | ||
|
|
||
| for (const detail of detailList) { | ||
| const addr = typeof detail === 'string' ? detail : detail['#text'] | ||
| if (typeof addr === 'string' && addr.trim()) { | ||
| addresses.add(this.normalizeAddress(addr.trim())) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return addresses | ||
| } | ||
|
|
||
| private normalizeAddress(address: string): string { | ||
| if (isAddress(address, { strict: false })) return getAddress(address) | ||
| return address | ||
| } | ||
|
|
||
| stop(): void { | ||
| if (this.refreshInterval) { | ||
| clearInterval(this.refreshInterval) | ||
| this.refreshInterval = undefined | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.