Skip to content

Commit

Permalink
base meta with 'public_key'
Browse files Browse the repository at this point in the history
  • Loading branch information
moky committed Oct 14, 2023
1 parent 1650e43 commit 66c05df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions dimp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
'QuitGroupCommand', 'QueryGroupCommand', 'ResetGroupCommand',
'HireGroupCommand', 'FireGroupCommand', 'ResignGroupCommand',

'CommandGeneralFactory', 'CommandFactoryManager',

'MessageEnvelope', 'BaseMessage',
'PlainMessage', 'EncryptedMessage', 'NetworkMessage',

Expand Down
20 changes: 10 additions & 10 deletions dimp/mkm/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,35 @@
class BaseMeta(Dictionary, Meta, ABC):

def __init__(self, meta: Dict[str, Any] = None,
version: int = None, key: VerifyKey = None,
version: int = None, public_key: VerifyKey = None,
seed: Optional[str] = None, fingerprint: Optional[TransportableData] = None):
# check parameters
if meta is not None:
# 0. meta info from network
assert version is None and key is None and seed is None and fingerprint is None, \
'params error: %s, %s, %s, %s, %s' % (meta, version, key, seed, fingerprint)
assert version is None and public_key is None and seed is None and fingerprint is None, \
'params error: %s, %s, %s, %s, %s' % (meta, version, public_key, seed, fingerprint)
# waiting to verify
# all metas must be verified before saving into local storage
status = 0
elif seed is None or fingerprint is None:
# 1. new meta with type & public key only
assert version is not None and version > 0 and key is not None and not MetaType.has_seed(version), \
'meta info error: %s, %s, %s, %s' % (version, key, seed, fingerprint)
assert version is not None and version > 0 and public_key is not None and not MetaType.has_seed(version), \
'meta info error: %s, %s, %s, %s' % (version, public_key, seed, fingerprint)
assert seed is None and fingerprint is None, 'meta seed/fingerprint error'
meta = {
'type': version,
'key': key.dictionary,
'key': public_key.dictionary,
}
# generated meta, or loaded from local storage,
# no need to verify again.
status = 1
else:
# 2. new meta with type, public key, seed & fingerprint
assert version is not None and version > 0 and key is not None and MetaType.has_seed(version), \
'meta info error: %s, %s, %s, %s' % (version, key, seed, fingerprint)
assert version is not None and version > 0 and public_key is not None and MetaType.has_seed(version), \
'meta info error: %s, %s, %s, %s' % (version, public_key, seed, fingerprint)
meta = {
'type': version,
'key': key.dictionary,
'key': public_key.dictionary,
'seed': seed,
'fingerprint': fingerprint.object,
}
Expand All @@ -103,7 +103,7 @@ def __init__(self, meta: Dict[str, Any] = None,
super().__init__(dictionary=meta)
# lazy load
self.__type = version
self.__key = key
self.__key = public_key
self.__seed = seed
self.__fingerprint = fingerprint
self.__status = status
Expand Down
8 changes: 4 additions & 4 deletions dimp/mkm/user_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def verify(self, data: bytes, signature: bytes) -> bool:
# Override
def encrypt(self, data: bytes) -> bytes:
barrack = self.data_source
assert barrack is not None, 'user data source not set yet'
assert isinstance(barrack, UserDataSource), 'user data source error: %s' % barrack
# NOTICE: meta.key will never changed, so use visa.key to encrypt message
# is the better way
key = barrack.public_key_for_encryption(identifier=self.identifier)
assert key is not None, 'failed to get encrypt key for user: %s' % self.identifier
return key.encrypt(data=data)
return key.encrypt(data=data, extra={})

# Override
def sign(self, data: bytes) -> bytes:
Expand All @@ -96,14 +96,14 @@ def sign(self, data: bytes) -> bytes:
# Override
def decrypt(self, data: bytes) -> Optional[bytes]:
barrack = self.data_source
assert barrack is not None, 'user data source not set yet'
assert isinstance(barrack, UserDataSource), 'user data source error: %s' % barrack
# NOTICE: if you provide a public key in visa document for encryption,
# here you should return the private key paired with visa.key
keys = barrack.private_keys_for_decryption(identifier=self.identifier)
assert len(keys) > 0, 'failed to get decrypt keys: %s' % self.identifier
for key in keys:
# try decrypting it with each private key
plaintext = key.decrypt(data=data)
plaintext = key.decrypt(data=data, params={})
if plaintext is not None:
# OK!
return plaintext
Expand Down

0 comments on commit 66c05df

Please sign in to comment.