Skip to content

Commit

Permalink
Valid key function (#50)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Endogen <[email protected]>
Co-authored-by: duelingbenjos <[email protected]>
Co-authored-by: duelingbenjos <[email protected]>

* Revert "can be reverted" (#28)

This reverts commit 390c83b.

* this was sus

---------

Co-authored-by: endogen <[email protected]>
Co-authored-by: Endogen <[email protected]>
Co-authored-by: duelingbenjos <[email protected]>
Co-authored-by: duelingbenjos <[email protected]>

* Added function to verify if a key is valid

---------

Co-authored-by: duelingbenjos <[email protected]>
Co-authored-by: duelingbenjos <[email protected]>
Co-authored-by: endogen <[email protected]>
Co-authored-by: Endogen <[email protected]>
  • Loading branch information
5 people authored Jun 4, 2024
1 parent df5fe5b commit c83643f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/contracting/stdlib/bridge/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/contracting/stdlib/bridge/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit c83643f

Please sign in to comment.