Skip to content

Commit

Permalink
refactor to base manager
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Aug 2, 2023
1 parent 7b1752a commit 1d13d6b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 72 deletions.
38 changes: 34 additions & 4 deletions aries_cloudagent/ledger/multiple_ledger/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ...core.profile import Profile
from ...ledger.base import BaseLedger
from ...messaging.valid import IndyDID
from ...multitenant.manager import BaseMultitenantManager


class MultipleLedgerManagerError(BaseError):
Expand All @@ -31,10 +32,6 @@ async def get_write_ledgers(self) -> List[str]:
async def get_ledger_id_by_ledger_pool_name(self, pool_name: str) -> str:
"""Return ledger_id by ledger pool name."""

@abstractmethod
async def set_profile_write_ledger(self, ledger_id: str, profile: Profile) -> str:
"""Set the write ledger for the profile."""

@abstractmethod
async def get_prod_ledgers(self) -> Mapping:
"""Return configured production ledgers."""
Expand Down Expand Up @@ -65,3 +62,36 @@ def extract_did_from_identifier(self, identifier: str) -> str:
return identifier.split(":")[-1]
else:
return identifier.split(":")[0]

async def set_profile_write_ledger(self, ledger_id: str, profile: Profile) -> str:
"""Set the write ledger for the profile."""
if ledger_id not in self.writable_ledgers:
raise MultipleLedgerManagerError(
f"Provided Ledger identifier {ledger_id} is not write configurable."
)
extra_settings = {}
multi_tenant_mgr = self.profile.inject_or(BaseMultitenantManager)
multi_ledgers = self.production_ledgers | self.non_production_ledgers
if ledger_id in multi_ledgers:
profile.context.injector.bind_instance(
BaseLedger, multi_ledgers.get(ledger_id)
)
self._update_settings(profile.context.settings, ledger_id)
self._update_settings(extra_settings, ledger_id)
if multi_tenant_mgr:
await multi_tenant_mgr.update_wallet(
profile.context.settings["wallet.id"],
extra_settings,
)
return ledger_id
raise MultipleLedgerManagerError(f"No ledger info found for {ledger_id}.")

def _update_settings(self, settings, ledger_id: str):
endorser_info = self.get_endorser_info_for_ledger(ledger_id)
if endorser_info:
endorser_alias, endorser_did = endorser_info
settings["endorser.endorser_alias"] = endorser_alias
settings["endorser.endorser_public_did"] = endorser_did
else:
settings["endorser.none"] = "true"
settings["ledger.write_ledger"] = ledger_id
34 changes: 0 additions & 34 deletions aries_cloudagent/ledger/multiple_ledger/indy_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ...core.profile import Profile
from ...ledger.base import BaseLedger
from ...ledger.error import LedgerError
from ...multitenant.manager import BaseMultitenantManager
from ...wallet.crypto import did_is_self_certified

from ..indy import IndySdkLedger
Expand Down Expand Up @@ -91,39 +90,6 @@ async def get_ledger_id_by_ledger_pool_name(self, pool_name: str) -> str:
"in either production_ledgers or non_production_ledgers"
)

async def set_profile_write_ledger(self, ledger_id: str, profile: Profile) -> str:
"""Set the write ledger for the profile."""
if ledger_id not in self.writable_ledgers:
raise MultipleLedgerManagerError(
f"Provided Ledger identifier {ledger_id} is not write configurable."
)
extra_settings = {}
multi_tenant_mgr = self.profile.inject_or(BaseMultitenantManager)
multi_ledgers = self.production_ledgers | self.non_production_ledgers
if ledger_id in multi_ledgers:
profile.context.injector.bind_instance(
BaseLedger, multi_ledgers.get(ledger_id)
)
endorser_info = self.get_endorser_info_for_ledger(ledger_id)
if endorser_info:
endorser_alias, endorser_did = endorser_info
profile.context.settings["endorser.endorser_alias"] = endorser_alias
profile.context.settings["endorser.endorser_public_did"] = endorser_did
profile.context.settings["ledger.write_ledger"] = ledger_id
if multi_tenant_mgr:
# may not use an endorser, so check first...
if endorser_info:
extra_settings["endorser.endorser_alias"] = endorser_alias
extra_settings["endorser.endorser_public_did"] = endorser_did
# persist the write ledger...
extra_settings["ledger.write_ledger"] = ledger_id
await multi_tenant_mgr.update_wallet(
profile.context.settings["wallet.id"],
extra_settings,
)
return ledger_id
raise MultipleLedgerManagerError(f"No ledger info found for {ledger_id}.")

async def _get_ledger_by_did(
self,
ledger_id: str,
Expand Down
34 changes: 0 additions & 34 deletions aries_cloudagent/ledger/multiple_ledger/indy_vdr_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ...core.profile import Profile
from ...ledger.base import BaseLedger
from ...ledger.error import LedgerError
from ...multitenant.manager import BaseMultitenantManager
from ...wallet.crypto import did_is_self_certified

from ..indy_vdr import IndyVdrLedger
Expand Down Expand Up @@ -74,39 +73,6 @@ async def get_nonprod_ledgers(self) -> Mapping:
"""Return non_production ledgers mapping."""
return self.non_production_ledgers

async def set_profile_write_ledger(self, ledger_id: str, profile: Profile) -> str:
"""Set the write ledger for the profile."""
if ledger_id not in self.writable_ledgers:
raise MultipleLedgerManagerError(
f"Provided Ledger identifier {ledger_id} is not write configurable."
)
extra_settings = {}
multi_tenant_mgr = self.profile.inject_or(BaseMultitenantManager)
multi_ledgers = self.production_ledgers | self.non_production_ledgers
if ledger_id in multi_ledgers:
profile.context.injector.bind_instance(
BaseLedger, multi_ledgers.get(ledger_id)
)
endorser_info = self.get_endorser_info_for_ledger(ledger_id)
if endorser_info:
endorser_alias, endorser_did = endorser_info
profile.context.settings["endorser.endorser_alias"] = endorser_alias
profile.context.settings["endorser.endorser_public_did"] = endorser_did
profile.context.settings["ledger.write_ledger"] = ledger_id
if multi_tenant_mgr:
# may not use an endorser, so check first...
if endorser_info:
extra_settings["endorser.endorser_alias"] = endorser_alias
extra_settings["endorser.endorser_public_did"] = endorser_did
# persist the write ledger...
extra_settings["ledger.write_ledger"] = ledger_id
await multi_tenant_mgr.update_wallet(
profile.context.settings["wallet.id"],
extra_settings,
)
return ledger_id
raise MultipleLedgerManagerError(f"No ledger info found for {ledger_id}.")

async def get_ledger_inst_by_id(self, ledger_id: str) -> Optional[BaseLedger]:
"""Return BaseLedger instance."""
return self.production_ledgers.get(
Expand Down

0 comments on commit 1d13d6b

Please sign in to comment.