Skip to content

Commit

Permalink
update the ISO currency code regular expression (#674)
Browse files Browse the repository at this point in the history
* fix the ISO currency code regex to match the documentation

* include integration tests for verifying the currency codes
  • Loading branch information
ckeshava authored Feb 13, 2024
1 parent 6f484c6 commit a38f61d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Added support for `XChainModifyBridge` flag maps (fixing an issue with `NFTokenCreateOffer` flag names)
- Fixed `XChainModifyBridge` validation to allow just clearing of `MinAccountCreateAmount`
- Currency codes with special characters not being allowed by IssuedCurrency objects.

## [2.5.0] - 2023-11-30

Expand Down
65 changes: 65 additions & 0 deletions tests/integration/transactions/test_trust_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
)
from tests.integration.reusable_values import WALLET
from xrpl.models.amounts import IssuedCurrencyAmount
from xrpl.models.exceptions import XRPLModelException
from xrpl.models.transactions import TrustSet, TrustSetFlag
from xrpl.wallet import Wallet

Expand All @@ -27,3 +28,67 @@ async def test_basic_functionality(self, client):
client,
)
self.assertTrue(response.is_successful())

@test_async_and_sync(globals())
async def test_special_chars_curr_codes(self, client):
issuer_wallet = Wallet.create()
response = await sign_and_reliable_submission_async(
TrustSet(
account=WALLET.address,
flags=TrustSetFlag.TF_SET_NO_RIPPLE,
limit_amount=IssuedCurrencyAmount(
issuer=issuer_wallet.address,
currency="$$$",
value="100",
),
),
WALLET,
client,
)
self.assertTrue(response.is_successful())

response = await sign_and_reliable_submission_async(
TrustSet(
account=WALLET.address,
flags=TrustSetFlag.TF_SET_NO_RIPPLE,
limit_amount=IssuedCurrencyAmount(
issuer=issuer_wallet.address,
currency="^%#",
value="100",
),
),
WALLET,
client,
)
self.assertTrue(response.is_successful())

response = await sign_and_reliable_submission_async(
TrustSet(
account=WALLET.address,
flags=TrustSetFlag.TF_SET_NO_RIPPLE,
limit_amount=IssuedCurrencyAmount(
issuer=issuer_wallet.address,
currency="a1@",
value="100",
),
),
WALLET,
client,
)
self.assertTrue(response.is_successful())

# currency codes must have exactly 3 characters
with self.assertRaises(XRPLModelException) as error:
TrustSet(
account=WALLET.address,
flags=TrustSetFlag.TF_SET_NO_RIPPLE,
limit_amount=IssuedCurrencyAmount(
issuer=issuer_wallet.address,
currency="abcd",
value="100",
),
)
self.assertEqual(
error.exception.args[0],
"{'currency': 'Invalid currency abcd'}",
)
19 changes: 19 additions & 0 deletions tests/unit/models/currencies/test_issued_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ def test_lower_currency_code_format(self):
)
self.assertTrue(obj.is_valid())

def test_lower_currency_code_special_chars(self):
obj = IssuedCurrency(
currency="$$$",
issuer=_ACCOUNT,
)
self.assertTrue(obj.is_valid())

obj = IssuedCurrency(
currency="^%#",
issuer=_ACCOUNT,
)
self.assertTrue(obj.is_valid())

obj = IssuedCurrency(
currency="a1@",
issuer=_ACCOUNT,
)
self.assertTrue(obj.is_valid())

def test_incorrect_currency_code_format(self):
# the "+" is not allowed in a currency format"
with self.assertRaises(XRPLModelException):
Expand Down
7 changes: 6 additions & 1 deletion xrpl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class XRPLException(Exception):
pass


ISO_CURRENCY_REGEX: Final[Pattern[str]] = re.compile("[A-Za-z0-9]{3}")
SPECIAL_CHARS_CURRENCY_CODE = re.escape("?!@#$%^&*(){}[]<>|")
ISO_CURRENCY_REGEX: Final[Pattern[str]] = re.compile(
"[A-Za-z0-9" + SPECIAL_CHARS_CURRENCY_CODE + "]{3}"
)
"""
Matches ISO currencies like "USD" or "EUR" in the format allowed by XRPL.
Check the docs for more information:
https://xrpl.org/currency-formats.html#standard-currency-codes
:meta private:
"""
Expand Down

0 comments on commit a38f61d

Please sign in to comment.