Skip to content

Commit 8845436

Browse files
authored
Merge branch 'main' into fix/base-model-unknown
2 parents a967fd0 + 5df3bc8 commit 8845436

9 files changed

Lines changed: 117 additions & 20 deletions

File tree

aries_cloudagent/ledger/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def backend(self) -> str:
4848
def read_only(self) -> bool:
4949
"""Accessor for the ledger read-only flag."""
5050

51+
@abstractmethod
52+
async def is_ledger_read_only(self) -> bool:
53+
"""Check if ledger is read-only including TAA."""
54+
5155
@abstractmethod
5256
async def get_key_for_did(self, did: str) -> str:
5357
"""Fetch the verkey for a ledger DID.
@@ -266,7 +270,7 @@ async def create_and_send_schema(
266270
LOGGER.warning("Schema already exists on ledger. Returning details.")
267271
schema_id, schema_def = schema_info
268272
else:
269-
if self.read_only:
273+
if await self.is_ledger_read_only():
270274
raise LedgerError(
271275
"Error cannot write schema when ledger is in read only mode"
272276
)
@@ -461,7 +465,7 @@ async def create_and_send_credential_definition(
461465
except IndyIssuerError as err:
462466
raise LedgerError(err.message) from err
463467

464-
if self.read_only:
468+
if await self.is_ledger_read_only():
465469
raise LedgerError(
466470
"Error cannot write cred def when ledger is in read only mode"
467471
)

aries_cloudagent/ledger/indy.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ def read_only(self) -> bool:
294294
"""Accessor for the ledger read-only flag."""
295295
return self.pool.read_only
296296

297+
async def is_ledger_read_only(self) -> bool:
298+
"""Check if ledger is read-only including TAA."""
299+
if self.read_only:
300+
return self.read_only
301+
# if TAA is required and not accepted we should be in read-only mode
302+
taa = await self.get_txn_author_agreement()
303+
if taa["taa_required"]:
304+
taa_acceptance = await self.get_latest_txn_author_acceptance()
305+
if "mechanism" not in taa_acceptance:
306+
return True
307+
return self.read_only
308+
297309
async def __aenter__(self) -> "IndySdkLedger":
298310
"""
299311
Context manager entry.
@@ -758,7 +770,7 @@ async def update_endpoint_for_did(
758770
)
759771

760772
if exist_endpoint_of_type != endpoint:
761-
if self.pool.read_only:
773+
if await self.is_ledger_read_only():
762774
raise LedgerError(
763775
"Error cannot update endpoint when ledger is in read only mode"
764776
)
@@ -811,7 +823,7 @@ async def register_nym(
811823
alias: Human-friendly alias to assign to the DID.
812824
role: For permissioned ledgers, what role should the new DID have.
813825
"""
814-
if self.pool.read_only:
826+
if await self.is_ledger_read_only():
815827
raise LedgerError(
816828
"Error cannot register nym when ledger is in read only mode"
817829
)

aries_cloudagent/ledger/indy_vdr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ def read_only(self) -> bool:
283283
"""Accessor for the ledger read-only flag."""
284284
return self.pool.read_only
285285

286+
async def is_ledger_read_only(self) -> bool:
287+
"""Check if ledger is read-only including TAA."""
288+
if self.read_only:
289+
return self.read_only
290+
# if TAA is required and not accepted we should be in read-only mode
291+
taa = await self.get_txn_author_agreement()
292+
if taa["taa_required"]:
293+
taa_acceptance = await self.get_latest_txn_author_acceptance()
294+
if "mechanism" not in taa_acceptance:
295+
return True
296+
return self.read_only
297+
286298
async def __aenter__(self) -> "IndyVdrLedger":
287299
"""
288300
Context manager entry.

aries_cloudagent/ledger/tests/test_indy.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,10 @@ async def test_txn_endorse(
537537
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.add_record")
538538
@async_mock.patch("indy.ledger.build_schema_request")
539539
@async_mock.patch("indy.ledger.append_request_endorser")
540+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
540541
async def test_send_schema(
541542
self,
543+
mock_is_ledger_read_only,
542544
mock_append_request_endorser,
543545
mock_build_schema_req,
544546
mock_add_record,
@@ -550,6 +552,7 @@ async def test_send_schema(
550552
):
551553
mock_wallet = async_mock.MagicMock()
552554
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
555+
mock_is_ledger_read_only.return_value = False
553556

554557
issuer = async_mock.MagicMock(IndyIssuer)
555558
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
@@ -677,8 +680,10 @@ async def test_send_schema_already_exists(
677680
)
678681
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.add_record")
679682
@async_mock.patch("indy.ledger.build_schema_request")
683+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
680684
async def test_send_schema_ledger_transaction_error_already_exists(
681685
self,
686+
mock_is_ledger_read_only,
682687
mock_build_schema_req,
683688
mock_add_record,
684689
mock_check_existing,
@@ -690,6 +695,7 @@ async def test_send_schema_ledger_transaction_error_already_exists(
690695

691696
mock_wallet = async_mock.MagicMock()
692697
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
698+
mock_is_ledger_read_only.return_value = False
693699

694700
issuer = async_mock.MagicMock(IndyIssuer)
695701
issuer.create_schema.return_value = ("1", "{}")
@@ -764,8 +770,10 @@ async def test_send_schema_ledger_read_only(
764770
@async_mock.patch(
765771
"aries_cloudagent.ledger.indy.IndySdkLedger.check_existing_schema"
766772
)
773+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
767774
async def test_send_schema_issuer_error(
768775
self,
776+
mock_is_ledger_read_only,
769777
mock_check_existing,
770778
mock_close_pool,
771779
mock_open_ledger,
@@ -775,6 +783,7 @@ async def test_send_schema_issuer_error(
775783

776784
mock_wallet = async_mock.MagicMock()
777785
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
786+
mock_is_ledger_read_only.return_value = False
778787

779788
issuer = async_mock.MagicMock(IndyIssuer)
780789
issuer.create_schema = async_mock.CoroutineMock(
@@ -850,8 +859,10 @@ async def test_send_schema_ledger_transaction_error(
850859
)
851860
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.add_record")
852861
@async_mock.patch("indy.ledger.build_schema_request")
862+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
853863
async def test_send_schema_no_seq_no(
854864
self,
865+
mock_is_ledger_read_only,
855866
mock_build_schema_req,
856867
mock_add_record,
857868
mock_fetch_schema_by_seq_no,
@@ -863,6 +874,7 @@ async def test_send_schema_no_seq_no(
863874
mock_wallet = async_mock.MagicMock()
864875
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
865876
issuer = async_mock.MagicMock(IndyIssuer)
877+
mock_is_ledger_read_only.return_value = False
866878
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
867879
issuer.create_schema.return_value = ("schema_issuer_did:name:1.0", "{}")
868880
mock_fetch_schema_by_id.return_value = None
@@ -1143,8 +1155,10 @@ async def test_get_schema_by_wrong_seq_no(
11431155
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.find_all_records")
11441156
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.add_record")
11451157
@async_mock.patch("indy.ledger.build_cred_def_request")
1158+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
11461159
async def test_send_credential_definition(
11471160
self,
1161+
mock_is_ledger_read_only,
11481162
mock_build_cred_def,
11491163
mock_add_record,
11501164
mock_find_all_records,
@@ -1157,6 +1171,7 @@ async def test_send_credential_definition(
11571171
mock_wallet = async_mock.MagicMock()
11581172
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
11591173
mock_find_all_records.return_value = []
1174+
mock_is_ledger_read_only.return_value = False
11601175

11611176
mock_get_schema.return_value = {"seqNo": 999}
11621177
cred_def_id = f"{self.test_did}:3:CL:999:default"
@@ -1231,8 +1246,10 @@ async def test_send_credential_definition(
12311246
@async_mock.patch("aries_cloudagent.storage.indy.IndySdkStorage.add_record")
12321247
@async_mock.patch("indy.ledger.build_cred_def_request")
12331248
@async_mock.patch("indy.ledger.append_request_endorser")
1249+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
12341250
async def test_send_credential_definition_endorse_only(
12351251
self,
1252+
mock_is_ledger_read_only,
12361253
mock_append_request_endorser,
12371254
mock_build_cred_def,
12381255
mock_add_record,
@@ -1246,6 +1263,7 @@ async def test_send_credential_definition_endorse_only(
12461263
mock_wallet = async_mock.MagicMock()
12471264
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
12481265
mock_find_all_records.return_value = []
1266+
mock_is_ledger_read_only.return_value = False
12491267

12501268
mock_get_schema.return_value = {"seqNo": 999}
12511269
cred_def_id = f"{self.test_did}:3:CL:999:default"
@@ -2229,8 +2247,10 @@ async def test_get_endpoint_for_did_no_endpoint(
22292247
@async_mock.patch("indy.ledger.build_get_attrib_request")
22302248
@async_mock.patch("indy.ledger.build_attrib_request")
22312249
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2250+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
22322251
async def test_update_endpoint_for_did(
22332252
self,
2253+
mock_is_ledger_read_only,
22342254
mock_submit,
22352255
mock_build_attrib_req,
22362256
mock_build_get_attrib_req,
@@ -2240,6 +2260,7 @@ async def test_update_endpoint_for_did(
22402260
mock_wallet = async_mock.MagicMock()
22412261
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
22422262
endpoint = ["http://old.aries.ca", "http://new.aries.ca"]
2263+
mock_is_ledger_read_only.return_value = False
22432264
mock_submit.side_effect = [
22442265
json.dumps(
22452266
{
@@ -2283,8 +2304,10 @@ async def test_update_endpoint_for_did(
22832304
@async_mock.patch("indy.ledger.build_get_attrib_request")
22842305
@async_mock.patch("indy.ledger.build_attrib_request")
22852306
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2307+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
22862308
async def test_update_endpoint_for_did_no_prior_endpoints(
22872309
self,
2310+
mock_is_ledger_read_only,
22882311
mock_submit,
22892312
mock_build_attrib_req,
22902313
mock_build_get_attrib_req,
@@ -2294,6 +2317,7 @@ async def test_update_endpoint_for_did_no_prior_endpoints(
22942317
mock_wallet = async_mock.MagicMock()
22952318
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
22962319
endpoint = "http://new.aries.ca"
2320+
mock_is_ledger_read_only.return_value = False
22972321
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
22982322
with async_mock.patch.object(
22992323
IndySdkWallet, "get_public_did"
@@ -2329,8 +2353,10 @@ async def test_update_endpoint_for_did_no_prior_endpoints(
23292353
@async_mock.patch("indy.ledger.build_get_attrib_request")
23302354
@async_mock.patch("indy.ledger.build_attrib_request")
23312355
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2356+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
23322357
async def test_update_endpoint_of_type_profile_for_did(
23332358
self,
2359+
mock_is_ledger_read_only,
23342360
mock_submit,
23352361
mock_build_attrib_req,
23362362
mock_build_get_attrib_req,
@@ -2341,6 +2367,7 @@ async def test_update_endpoint_of_type_profile_for_did(
23412367
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
23422368
endpoint = ["http://company.com/oldProfile", "http://company.com/newProfile"]
23432369
endpoint_type = EndpointType.PROFILE
2370+
mock_is_ledger_read_only.return_value = False
23442371
mock_submit.side_effect = [
23452372
json.dumps(
23462373
{
@@ -2354,6 +2381,11 @@ async def test_update_endpoint_of_type_profile_for_did(
23542381
for i in range(len(endpoint))
23552382
]
23562383
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
2384+
# ledger = async_mock.patch.object(
2385+
# ledger,
2386+
# "is_ledger_read_only",
2387+
# async_mock.CoroutineMock(return_value=False),
2388+
# )
23572389
with async_mock.patch.object(
23582390
IndySdkWallet, "get_public_did"
23592391
) as mock_wallet_get_public_did:
@@ -2446,11 +2478,18 @@ async def test_update_endpoint_for_did_read_only(
24462478
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedgerPool.context_close")
24472479
@async_mock.patch("indy.ledger.build_nym_request")
24482480
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2481+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
24492482
async def test_register_nym(
2450-
self, mock_submit, mock_build_nym_req, mock_close, mock_open
2483+
self,
2484+
mock_is_ledger_read_only,
2485+
mock_submit,
2486+
mock_build_nym_req,
2487+
mock_close,
2488+
mock_open,
24512489
):
24522490
mock_wallet = async_mock.MagicMock()
24532491
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
2492+
mock_is_ledger_read_only.return_value = False
24542493
with async_mock.patch.object(
24552494
IndySdkWallet, "get_public_did"
24562495
) as mock_wallet_get_public_did, async_mock.patch.object(
@@ -2518,12 +2557,19 @@ async def test_register_nym_read_only(self, mock_close, mock_open):
25182557

25192558
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedgerPool.context_open")
25202559
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedgerPool.context_close")
2521-
async def test_register_nym_no_public_did(self, mock_close, mock_open):
2560+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
2561+
async def test_register_nym_no_public_did(
2562+
self,
2563+
mock_is_ledger_read_only,
2564+
mock_close,
2565+
mock_open,
2566+
):
25222567
mock_wallet = async_mock.MagicMock(
25232568
type="indy",
25242569
get_local_did=async_mock.CoroutineMock(),
25252570
replace_local_did_metadata=async_mock.CoroutineMock(),
25262571
)
2572+
mock_is_ledger_read_only.return_value = False
25272573
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
25282574
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
25292575
with async_mock.patch.object(
@@ -2543,14 +2589,21 @@ async def test_register_nym_no_public_did(self, mock_close, mock_open):
25432589
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedgerPool.context_close")
25442590
@async_mock.patch("indy.ledger.build_nym_request")
25452591
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2592+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
25462593
async def test_register_nym_ledger_x(
2547-
self, mock_submit, mock_build_nym_req, mock_close, mock_open
2594+
self,
2595+
mock_is_ledger_read_only,
2596+
mock_submit,
2597+
mock_build_nym_req,
2598+
mock_close,
2599+
mock_open,
25482600
):
25492601
mock_wallet = async_mock.MagicMock()
25502602
mock_build_nym_req.side_effect = IndyError(
25512603
error_code=ErrorCode.CommonInvalidParam1,
25522604
error_details={"message": "not today"},
25532605
)
2606+
mock_is_ledger_read_only.return_value = False
25542607
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
25552608
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
25562609
with async_mock.patch.object(
@@ -2570,11 +2623,18 @@ async def test_register_nym_ledger_x(
25702623
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedgerPool.context_close")
25712624
@async_mock.patch("indy.ledger.build_nym_request")
25722625
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger._submit")
2626+
@async_mock.patch("aries_cloudagent.ledger.indy.IndySdkLedger.is_ledger_read_only")
25732627
async def test_register_nym_steward_register_others_did(
2574-
self, mock_submit, mock_build_nym_req, mock_close, mock_open
2628+
self,
2629+
mock_is_ledger_read_only,
2630+
mock_submit,
2631+
mock_build_nym_req,
2632+
mock_close,
2633+
mock_open,
25752634
):
25762635
mock_wallet = async_mock.MagicMock()
25772636
self.session.context.injector.bind_provider(BaseWallet, mock_wallet)
2637+
mock_is_ledger_read_only.return_value = False
25782638
ledger = IndySdkLedger(IndySdkLedgerPool("name", checked=True), self.profile)
25792639
with async_mock.patch.object(
25802640
IndySdkWallet, "get_public_did"

aries_cloudagent/ledger/tests/test_indy_vdr.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ async def close():
3838

3939
with async_mock.patch.object(ledger.pool, "open", open), async_mock.patch.object(
4040
ledger.pool, "close", close
41+
), async_mock.patch.object(
42+
ledger, "is_ledger_read_only", async_mock.CoroutineMock(return_value=False)
4143
):
4244
yield ledger
4345

@@ -302,6 +304,10 @@ async def test_send_schema_ledger_read_only(
302304
ledger,
303305
"check_existing_schema",
304306
async_mock.CoroutineMock(return_value=False),
307+
), async_mock.patch.object(
308+
ledger,
309+
"is_ledger_read_only",
310+
async_mock.CoroutineMock(return_value=True),
305311
):
306312
with pytest.raises(LedgerError):
307313
schema_id, schema_def = await ledger.create_and_send_schema(

aries_cloudagent/protocols/coordinate_mediation/v1_0/route_manager.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ async def mediation_record_for_connection(
6666
or_default: bool = False,
6767
):
6868
"""Return relevant mediator for connection."""
69-
async with profile.session() as session:
70-
mediation_metadata = await conn_record.metadata_get(
71-
session, MediationManager.METADATA_KEY, {}
72-
)
73-
mediation_id = (
74-
mediation_metadata.get(MediationManager.METADATA_ID) or mediation_id
75-
)
69+
if conn_record.connection_id:
70+
async with profile.session() as session:
71+
mediation_metadata = await conn_record.metadata_get(
72+
session, MediationManager.METADATA_KEY, {}
73+
)
74+
mediation_id = (
75+
mediation_metadata.get(MediationManager.METADATA_ID) or mediation_id
76+
)
7677

7778
mediation_record = await self.mediation_record_if_id(
7879
profile, mediation_id, or_default

0 commit comments

Comments
 (0)