From c83643ff350f9a55c7142107dc5a61daab982e57 Mon Sep 17 00:00:00 2001 From: crosschainer <68580992+crosschainer@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:33:02 +0200 Subject: [PATCH] Valid key function (#50) * added strptime to stdlib * Testnet (#47) * can be reverted * driver * driver * missed * missed * missed * Add loguru dependency Replace logger with loguru Better formating Remove commented our code Move constant to constants.py * update .gitignore & add dependabot config * added nacl & a new module available to contracts, crypto --------- Co-authored-by: endogen <081081@gmx.net> Co-authored-by: Endogen Co-authored-by: duelingbenjos Co-authored-by: duelingbenjos <75325191+duelingbenjos@users.noreply.github.com> * Revert "can be reverted" (#28) This reverts commit 390c83b71bd86e82293ad00ce8c5b9a24461d5db. * this was sus --------- Co-authored-by: endogen <081081@gmx.net> Co-authored-by: Endogen Co-authored-by: duelingbenjos Co-authored-by: duelingbenjos <75325191+duelingbenjos@users.noreply.github.com> * Added function to verify if a key is valid --------- Co-authored-by: duelingbenjos Co-authored-by: duelingbenjos <75325191+duelingbenjos@users.noreply.github.com> Co-authored-by: endogen <081081@gmx.net> Co-authored-by: Endogen --- src/contracting/stdlib/bridge/crypto.py | 13 +++++++++++++ src/contracting/stdlib/bridge/time.py | 6 +++++- tests/unit/test_datetime.py | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/contracting/stdlib/bridge/crypto.py b/src/contracting/stdlib/bridge/crypto.py index 90659256..e37fe7f9 100644 --- a/src/contracting/stdlib/bridge/crypto.py +++ b/src/contracting/stdlib/bridge/crypto.py @@ -15,8 +15,21 @@ def verify(vk: str, msg: str, signature: str): return True +def key_is_valid(key: str): + """ Check if the given address is valid. + Can be used with public and private keys """ + if not len(key) == 64: + return False + try: + int(key, 16) + except: + return False + return True + + crypto_module = ModuleType('crypto') crypto_module.verify = verify +crypto_module.key_is_valid = key_is_valid exports = { 'crypto': crypto_module diff --git a/src/contracting/stdlib/bridge/time.py b/src/contracting/stdlib/bridge/time.py index 3ba9a8ee..be21c9f1 100644 --- a/src/contracting/stdlib/bridge/time.py +++ b/src/contracting/stdlib/bridge/time.py @@ -99,7 +99,11 @@ def _from_datetime(cls, d: dt): minute=d.minute, second=d.second, microsecond=d.microsecond) - + + @classmethod + def strptime(cls, date_string, format): + d = dt.strptime(date_string, format) + return cls._from_datetime(d) class Timedelta: def __init__(self, weeks=0, diff --git a/tests/unit/test_datetime.py b/tests/unit/test_datetime.py index fc1f2554..276c5804 100644 --- a/tests/unit/test_datetime.py +++ b/tests/unit/test_datetime.py @@ -164,3 +164,28 @@ def test_datetime_subtraction_to_proper_timedelta(self): self.assertEqual((d - e), Timedelta(days=365)) + + def test_datetime_strptime(self): + d = dt(2019, 1, 1) + self.assertEqual(str(Datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S')), str(d)) + + + def test_datetime_strptime_invalid_format(self): + d = dt(2019, 1, 1) + with self.assertRaises(ValueError): + Datetime.strptime(str(d), '%Y-%m-%d') + + def test_datetime_strptime_invalid_date(self): + with self.assertRaises(ValueError): + Datetime.strptime('2019-02-30 12:00:00', '%Y-%m-%d %H:%M:%S') + + + def test_datetime_strptime_invalid_date_format(self): + with self.assertRaises(ValueError): + Datetime.strptime('2019-02-30 12:00:00', '%Y-%m-%d %H:%M:%S') + + + def test_datetime_returns_correct_datetime_cls(self): + d = dt(2019, 1, 1) + self.assertEqual(Datetime.strptime(str(d), '%Y-%m-%d %H:%M:%S'), Datetime(2019, 1, 1)) +