Skip to content

Commit

Permalink
validator and tests additions
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Jun 1, 2024
2 parents 6ad013f + 01be368 commit 4268371
Show file tree
Hide file tree
Showing 15 changed files with 1,710 additions and 198 deletions.
2 changes: 1 addition & 1 deletion commune/code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def process_response(self, response):



def file2fns(self, filepath):
def file2fns(self, filepath = '~/commune/utils/dict.py'):
'''
"""
Documentation for `get_fns` function:
Expand Down
67 changes: 42 additions & 25 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self,
private_key = key.__dict__.get('private_key', private_key)
crypto_type = key.__dict__.get('crypto_type', crypto_type)
derive_path = key.__dict__.get('derive_path', derive_path)
ss58_address = key.__dict__.get('key', ss58_address)
ss58_address = key.__dict__.get('ss58_address', ss58_address)
path = key.__dict__.get('path', path)
public_key = key.__dict__.get('public_key', public_key)
ss58_format = key.__dict__.get('ss58_format', ss58_format)
Expand Down Expand Up @@ -177,9 +177,22 @@ def add_key(cls, path:str, mnemonic:str = None, password:str=None, refresh:bool=
if password != None:
key_json = cls.encrypt(data=key_json, password=password)
cls.put(path, key_json)
cls.add_key_address(path, key.ss58_address)
cls.update()
return json.loads(key_json)

@classmethod
def add_key_address(cls, key, address):
key2address = cls.key2address(update=False)
key2address[key] = address
cls.put('key2address', key2address)

@classmethod
def rm_key_address(cls, key):
key2address = cls.key2address(update=False)
key2address.pop(key, None)
cls.put('key2address', key2address)


@classmethod
def update(cls, **kwargs):
Expand Down Expand Up @@ -361,10 +374,9 @@ def get_keys(cls, search=None, clean_failed_keys=False):


@classmethod
def key2address(cls, search=None, max_age=None, update=False, **kwargs):
def key2address(cls, search=None, update=False, **kwargs):
path = 'key2address'
key2address = []
key2address = cls.get(path, key2address,max_age=max_age, update=update)
key2address = cls.get(path, [],max_age=None, update=update)
if len(key2address) == 0:
key2address = { k: v.ss58_address for k,v in cls.get_keys(search).items()}
cls.put(path, key2address)
Expand Down Expand Up @@ -442,9 +454,7 @@ def rm_key(cls, key=None):
if key not in keys:
raise Exception(f'key {key} not found, available keys: {keys}')
c.rm(key2path[key])
cls.update()
assert c.exists(key2path[key]) == False, 'key not deleted'

cls.rm_key_address(key)
return {'deleted':[key]}

@property
Expand Down Expand Up @@ -547,7 +557,7 @@ def to_json(self, password: str = None ) -> dict:
if password != None:
state_dict[k] = self.encrypt(data=state_dict[k], password=password)
if '_ss58_address' in state_dict:
state_dict['key'] = state_dict.pop('_ss58_address')
state_dict['ss58_address'] = state_dict.pop('_ss58_address')
state_dict = json.dumps(state_dict)

return state_dict
Expand All @@ -562,8 +572,8 @@ def from_json(cls, obj: Union[str, dict], password: str = None) -> dict:
for k,v in obj.items():
if cls.is_encrypted(obj[k]) and password != None:
obj[k] = cls.decrypt(data=obj[k], password=password)
if 'key' in obj:
obj['_ss58_address'] = obj.pop('key')
if 'ss58_address' in obj:
obj['_ss58_address'] = obj.pop('ss58_address')
return cls(**obj)

@classmethod
Expand Down Expand Up @@ -1297,21 +1307,28 @@ def test_key_encryption(self, test_key='test.key'):



def test_key_management(self):
if self.key_exists('test'):
self.rm_key('test')
key1 = self.get_key('test')
assert self.key_exists('test'), f'Key management failed, key still exists'
self.mv_key('test', 'test2')
key2 = self.get_key('test2')
assert key1.ss58_address == key2.ss58_address, f'Key management failed, {key1.ss58_address} != {key2.ss58_address}'
assert self.key_exists('test2'), f'Key management failed, key does not exist'
assert not self.key_exists('test'), f'Key management failed, key still exists'
self.mv_key('test2', 'test')
assert self.key_exists('test'), f'Key management failed, key does not exist'
assert not self.key_exists('test2'), f'Key management failed, key still exists'
self.rm_key('test')
assert not self.key_exists('test'), f'Key management failed, key still exists'
def test_key_management(self, key1='test.key' , key2='test2.key'):
if self.key_exists(key1):
self.rm_key(key1)
if self.key_exists(key2):
self.rm_key(key2)


self.add_key(key1)
k1 = self.get_key(key1)
assert self.key_exists(key1), f'Key management failed, key still exists'
self.mv_key(key1, key2)
k2 = self.get_key(key2)
assert k1.ss58_address == k2.ss58_address, f'Key management failed, {k1.ss58_address} != {k2.ss58_address}'
assert self.key_exists(key2), f'Key management failed, key does not exist'
assert not self.key_exists(key1), f'Key management failed, key still exists'
self.mv_key(key2, key1)
assert self.key_exists(key1), f'Key management failed, key does not exist'
assert not self.key_exists(key2), f'Key management failed, key still exists'
self.rm_key(key1)
# self.rm_key(key2)
assert not self.key_exists(key1), f'Key management failed, key still exists'
assert not self.key_exists(key2), f'Key management failed, key still exists'
return {'success': True, 'msg': 'test_key_management passed'}

@classmethod
Expand Down
Loading

0 comments on commit 4268371

Please sign in to comment.