Skip to content

Commit 8f3e91e

Browse files
committed
(feat) Implemented OFAC list address check
1 parent 5541211 commit 8f3e91e

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

ofac.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["0x179f48c78f57a3a78f0608cc9197b8972921d1d2", "0x1967d8af5bd86a497fb3dd7899a020e47560daaf", "0x19aa5fe80d33a56d56c78e82ea5e50e5d80b4dff", "0x19aa5fe80d33a56d56c78e82ea5e50e5d80b4dff", "0x1da5821544e25c636c1417ba96ade4cf6d2f9b5a", "0x2f389ce8bd8ff92de3402ffce4691d17fc4f6535", "0x2f389ce8bd8ff92de3402ffce4691d17fc4f6535", "0x2f50508a8a3d323b91336fa3ea6ae50e55f32185", "0x308ed4b7b49797e1a98d3818bff6fe5385410370", "0x3cbded43efdaf0fc77b9c55f6fc9988fcc9b757d", "0x3efa30704d2b8bbac821307230376556cf8cc39e", "0x48549a34ae37b12f6a30566245176994e17c6b4a", "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", "0x530a64c0ce595026a4a556b703644228179e2d57", "0x5512d943ed1f7c8a43f3435c85f7ab68b30121b0", "0x5a7a51bfb49f190e5a6060a5bc6052ac14a3b59f", "0x5f48c2a71b2cc96e3f0ccae4e39318ff0dc375b2", "0x6be0ae71e6c41f2f9d0d1a3b8d0f75e6f6a0b46e", "0x6f1ca141a28907f78ebaa64fb83a9088b02a8352", "0x746aebc06d2ae31b71ac51429a19d54e797878e9", "0x77777feddddffc19ff86db637967013e6c6a116c", "0x797d7ae72ebddcdea2a346c1834e04d1f8df102b", "0x8576acc5c05d6ce88f4e49bf65bdf0c62f91353c", "0x901bb9583b24d97e995513c6778dc6888ab6870e", "0x961c5be54a2ffc17cf4cb021d863c42dacd47fc1", "0x97b1043abd9e6fc31681635166d430a458d14f9c", "0x9c2bc757b66f24d60f016b6237f8cdd414a879fa", "0x9f4cda013e354b8fc285bf4b9a60460cee7f7ea9", "0xa7e5d5a720f06526557c513402f2e6b5fa20b008", "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", "0xc455f7fd3e0e12afd51fba5c106909934d8a0e4a", "0xca0840578f57fe71599d29375e16783424023357", "0xd0975b32cea532eadddfc9c60481976e39db3472", "0xd882cfc20f52f2599d84b8e8d58c7fb62cfe344b", "0xd882cfc20f52f2599d84b8e8d58c7fb62cfe344b", "0xe1d865c3d669dcc8c57c8d023140cb204e672ee4", "0xe7aa314c77f4233c18c6cc84384a9247c0cf367b", "0xed6e0a7e4ac94d976eebfb82ccf777a3c6bad921", "0xf3701f445b6bdafedbca97d1e477357839e4120d", "0xfac583c0cf07ea434052c49115a4682172ab6b4f", "0xfec8a60023265364d066a1212fde3930f6ae8da7", "0xffbac21a641dcfe4552920138d90f3638b3c9fba"]

pyinjective/composer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pyinjective.proto.injective.tokenfactory.v1beta1 import tx_pb2 as token_factory_tx_pb
4545
from pyinjective.proto.injective.wasmx.v1 import tx_pb2 as wasmx_tx_pb
4646
from pyinjective.utils.denom import Denom
47+
from pyinjective.core.ofac import OfacChecker
4748

4849
REQUEST_TO_RESPONSE_TYPE_MAP = {
4950
"MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrderResponse,
@@ -147,6 +148,7 @@ def __init__(
147148
self.derivative_markets = derivative_markets
148149
self.binary_option_markets = binary_option_markets
149150
self.tokens = tokens
151+
self._ofac_checker = OfacChecker()
150152

151153
def Coin(self, amount: int, denom: str):
152154
"""
@@ -471,6 +473,8 @@ def MsgBid(self, sender: str, bid_amount: float, round: float):
471473

472474
# region Authz module
473475
def MsgGrantGeneric(self, granter: str, grantee: str, msg_type: str, expire_in: int):
476+
if self._ofac_checker.is_blacklisted(granter):
477+
raise Exception(f"Address {granter} is in the OFAC list")
474478
auth = cosmos_authz_pb.GenericAuthorization(msg=msg_type)
475479
any_auth = any_pb2.Any()
476480
any_auth.Pack(auth, type_url_prefix="")
@@ -2125,6 +2129,9 @@ def MsgGrantTyped(
21252129
subaccount_id: str,
21262130
**kwargs,
21272131
):
2132+
if self._ofac_checker.is_blacklisted(granter):
2133+
raise Exception(f"Address {granter} is in the OFAC list")
2134+
21282135
auth = None
21292136
if msg_type == "CreateSpotLimitOrderAuthz":
21302137
auth = injective_authz_pb.CreateSpotLimitOrderAuthz(

pyinjective/core/broadcaster.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import math
2+
import json
3+
import os
24
from abc import ABC, abstractmethod
35
from decimal import Decimal
46
from typing import List, Optional
@@ -12,7 +14,7 @@
1214
from pyinjective.constant import GAS_PRICE
1315
from pyinjective.core.gas_limit_estimator import GasLimitEstimator
1416
from pyinjective.core.network import Network
15-
17+
from pyinjective.core.ofac import OfacChecker
1618

1719
class BroadcasterAccountConfig(ABC):
1820
@property
@@ -62,6 +64,10 @@ def __init__(
6264
self._client = client
6365
self._composer = composer
6466
self._fee_calculator = fee_calculator
67+
self._ofac_checker = OfacChecker()
68+
69+
if self._ofac_checker.is_blacklisted(address=self._account_config.trading_injective_address):
70+
raise Exception(f"Address {self._account_config.trading_injective_address} is in the OFAC list")
6571

6672
@classmethod
6773
def new_using_simulation(

pyinjective/core/ofac.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import asyncio
2+
import json
3+
import os
4+
5+
import aiohttp
6+
7+
OFAC_LIST_URL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json"
8+
OFAC_LIST_FILENAME = "ofac.json"
9+
10+
class OfacChecker:
11+
def __init__(self):
12+
self._ofac_list_path = self.get_ofac_list_path()
13+
if not os.path.exists(self._ofac_list_path):
14+
self.download_ofac_list()
15+
16+
with open(self._ofac_list_path, 'r') as f:
17+
self._ofac_list = json.load(f)
18+
19+
@classmethod
20+
def get_ofac_list_path(cls):
21+
current_directory = os.getcwd()
22+
while os.path.basename(current_directory) != 'sdk-python':
23+
current_directory = os.path.dirname(current_directory)
24+
return os.path.join(current_directory, OFAC_LIST_FILENAME)
25+
26+
@classmethod
27+
async def download_ofac_list(cls):
28+
async with aiohttp.ClientSession() as session:
29+
try:
30+
async with session.get(OFAC_LIST_URL) as response:
31+
response.raise_for_status()
32+
text_content = await response.text()
33+
ofac_list = json.loads(text_content)
34+
ofac_file_path = cls.get_ofac_list_path()
35+
with open(ofac_file_path, 'w') as f:
36+
json.dump(ofac_list, f)
37+
return
38+
except (aiohttp.ClientError, json.JSONDecodeError) as e:
39+
raise Exception(f"Error fetching OFAC list: {e}")
40+
41+
def is_blacklisted(self, address: str) -> bool:
42+
return address in self._ofac_list
43+
44+
if __name__ == "__main__":
45+
asyncio.run(OfacChecker.download_ofac_list())

tests/core/test_broadcaster.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
import os
3+
from unittest.mock import Mock
4+
5+
import pytest
6+
7+
import pyinjective.core.ofac as ofac
8+
from pyinjective import PrivateKey
9+
from pyinjective.async_client import AsyncClient
10+
from pyinjective.composer import Composer
11+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk, StandardAccountBroadcasterConfig
12+
from pyinjective.core.network import Network
13+
14+
15+
def test_broadcast_address_in_ofac_list():
16+
private_key_banned = PrivateKey.from_mnemonic("test mnemonic never use other places")
17+
public_key_banned = private_key_banned.to_public_key()
18+
address_banned = public_key_banned.to_address()
19+
20+
ofac_list = [address_banned.to_acc_bech32()]
21+
ofac.OFAC_LIST_FILENAME = "ofac_test.json"
22+
with open(ofac.OfacChecker.get_ofac_list_path(), "w") as ofac_file:
23+
json.dump(ofac_list, ofac_file)
24+
25+
network = Network.local()
26+
client = AsyncClient(
27+
network=Network.local(),
28+
)
29+
composer = Mock(spec=Composer)
30+
31+
account_config = StandardAccountBroadcasterConfig(private_key=private_key_banned.to_hex())
32+
with pytest.raises(Exception):
33+
_ = MsgBroadcasterWithPk(
34+
network=network,
35+
account_config=account_config,
36+
client=client,
37+
composer=composer,
38+
fee_calculator=Mock(),
39+
)
40+
os.remove(ofac.OfacChecker.get_ofac_list_path())
41+
ofac.OFAC_LIST_FILENAME = "ofac.json"

0 commit comments

Comments
 (0)