-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
541c27a
commit 4479239
Showing
15 changed files
with
142 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
import { isString } from 'formik'; | ||
import * as yup from 'yup'; | ||
|
||
import { checkEntityAddressIsCompliant } from '@app/query/common/compliance-checker/compliance-checker.query'; | ||
|
||
export function complianceValidator( | ||
shouldCheckCompliance: yup.StringSchema<string, yup.AnyObject> | ||
) { | ||
return yup.string().test({ | ||
message: 'Compliance check failed', | ||
async test(value) { | ||
if (!isString(value)) return false; | ||
|
||
if (!shouldCheckCompliance.isValidSync(value)) return true; | ||
|
||
try { | ||
const resp = await checkEntityAddressIsCompliant(value); | ||
return !resp.isOnSanctionsList; | ||
} catch (e) { | ||
return true; | ||
} | ||
}, | ||
}); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import { BitcoinAccount } from '@shared/crypto/bitcoin/bitcoin.utils'; | ||
import type { P2Ret, P2TROut } from '@scure/btc-signer'; | ||
|
||
import { useCurrentNativeSegwitAccount } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; | ||
import { useCurrentTaprootAccount } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks'; | ||
import { ZERO_INDEX } from '@shared/constants'; | ||
|
||
interface CurrentBitcoinAccountLoaderProps { | ||
children(data: { nativeSegwit: BitcoinAccount; taproot: BitcoinAccount }): React.ReactNode; | ||
import type { Signer } from '@app/store/accounts/blockchain/bitcoin/bitcoin-signer'; | ||
import { useCurrentAccountNativeSegwitSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; | ||
import { useCurrentAccountTaprootSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks'; | ||
|
||
interface CurrentBitcoinSignerLoaderProps { | ||
children(data: { nativeSegwit: Signer<P2Ret>; taproot: Signer<P2TROut> }): React.ReactNode; | ||
} | ||
export function CurrentBitcoinAccountLoader({ children }: CurrentBitcoinAccountLoaderProps) { | ||
const nativeSegwit = useCurrentNativeSegwitAccount(); | ||
const taproot = useCurrentTaprootAccount(); | ||
export function CurrentBitcoinSignerLoader({ children }: CurrentBitcoinSignerLoaderProps) { | ||
const nativeSegwit = useCurrentAccountNativeSegwitSigner()?.(ZERO_INDEX); | ||
const taproot = useCurrentAccountTaprootSigner()?.(ZERO_INDEX); | ||
if (!taproot || !nativeSegwit) return null; | ||
return children({ nativeSegwit, taproot }); | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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
75 changes: 75 additions & 0 deletions
75
src/app/query/common/compliance-checker/compliance-checker.query.ts
This file contains 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,75 @@ | ||
import { type UseQueryOptions, useQueries } from '@tanstack/react-query'; | ||
import axios from 'axios'; | ||
|
||
import type { BitcoinNetworkModes } from '@shared/constants'; | ||
import { ensureArray } from '@shared/utils'; | ||
|
||
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; | ||
import { useCurrentNetwork } from '@app/store/networks/networks.selectors'; | ||
|
||
const checkApi = 'https://api.chainalysis.com/api/risk/v2/entities'; | ||
|
||
const headers = { | ||
// Known public key, do not open a vulnerability report for this | ||
Token: '51d3c7529eb08a8c62d41d70d006bdcd4248150fbd6826d5828ac908e7c12073', | ||
}; | ||
|
||
async function registerEntityAddressComplianceCheck(address: string) { | ||
const resp = await axios.post(checkApi, { address }, { headers }); | ||
return resp.data; | ||
} | ||
|
||
async function checkEntityAddressComplianceCheck(address: string) { | ||
const resp = await axios.get(checkApi + '/' + address, { headers }); | ||
return resp.data; | ||
} | ||
|
||
interface ComplianceReport { | ||
risk: 'None' | 'Low' | 'Moderate' | 'Severe'; | ||
isOnSanctionsList: boolean; | ||
} | ||
export async function checkEntityAddressIsCompliant(address: string): Promise<ComplianceReport> { | ||
await registerEntityAddressComplianceCheck(address); | ||
const entityReport = await checkEntityAddressComplianceCheck(address); | ||
return { ...entityReport, isOnSanctionsList: entityReport.risk === 'Severe' }; | ||
} | ||
|
||
const oneWeekInMs = 604_800_000; | ||
|
||
function makeComplianceQuery( | ||
address: string, | ||
network: BitcoinNetworkModes | ||
): UseQueryOptions<ComplianceReport> { | ||
return { | ||
enabled: network === 'mainnet', | ||
queryKey: ['address-compliance-check', address], | ||
async queryFn() { | ||
return checkEntityAddressIsCompliant(address); | ||
}, | ||
cacheTime: Infinity, | ||
staleTime: oneWeekInMs, | ||
refetchInterval: oneWeekInMs, | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
refetchOnWindowFocus: false, | ||
}; | ||
} | ||
|
||
function useCheckAddressComplianceQueries(addresses: string[]) { | ||
const network = useCurrentNetwork(); | ||
return useQueries({ | ||
queries: addresses.map(address => | ||
makeComplianceQuery(address, network.chain.bitcoin.bitcoinNetwork) | ||
), | ||
}); | ||
} | ||
|
||
export function useBreakOnNonCompliantEntity(address: string | string[]) { | ||
const analytics = useAnalytics(); | ||
const complianceReports = useCheckAddressComplianceQueries(ensureArray(address)); | ||
|
||
if (complianceReports.some(report => report.data?.isOnSanctionsList)) { | ||
void analytics.track('non_compliant_entity_detected'); | ||
throw new Error('Unable to handle request, errorCode: 1398'); | ||
} | ||
} |
This file contains 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