class CredDefStoreFile(CredDefStore): def __init__(self, baseDir: str, name: str): self.store = DirectoryStore(baseDir, name) @staticmethod def key(name: str, version: str, dest: str): key = "{}_{}_{}".format(name, version, dest) return base64.urlsafe_b64encode(key.encode()).decode() def addCredDef(self, name: str, version: str, dest: str, type: str, ip: str, port: int, keys: Dict): key = self.key(name, version, dest) self.store.put(key, json.dumps({ TYPE: type, IP: ip, PORT: port, KEYS: keys })) def getCredDef(self, name: str, version: str, dest: str = None): key = self.key(name, version, dest) value = self.store.get(key) if value: credDef = json.loads(value) credDef.update({ NAME: name, VERSION: version, TARGET_NYM: dest }) return credDef
class AttributeStoreFile(AttributeStore): def __init__(self, baseDir: str, name: str): self.store = DirectoryStore(baseDir, name) @staticmethod def attrKey(name: str, dest: Optional[str] = None): dest = dest or "" key = "{}_{}".format(name, dest) return base64.urlsafe_b64encode(key.encode()).decode() @staticmethod def attrKeyParts(attrKey: str): key = base64.urlsafe_b64decode(attrKey).decode() return str.rsplit(key, "_", 1) @staticmethod def constructAttrData(fileData: str, name: str, dest: Optional[str]=None): attr = json.loads(fileData) attr[NAME] = name if dest: attr[TARGET_NYM] = dest return attr # TODO: May be need to provide hash type also, assuming sha256 for now def addAttribute(self, name: str, val: Any, origin: str, dest: Optional[str]=None, encKey: Optional[str]=None, encType: Optional[str]=None, hashed: bool=False): key = self.attrKey(name, dest) if hashed: dataKeyName = HASH elif encKey: dataKeyName = ENC else: dataKeyName = RAW value = {dataKeyName: val, ORIGIN: origin} if encKey: value.update({SKEY: encKey}) if encType: value.update({ENC_TYPE: encType}) self.store.put(key, value=json.dumps(value)) def getAttribute(self, name: str, dest: Optional[str] = None): key = self.attrKey(name, dest) value = self.store.get(key) if value: return self.constructAttrData(value, name, dest) @property def attributes(self): return [self.constructAttrData(val, *self.attrKeyParts(key)) for key, val in self.store.iterator()]