Skip to content

Commit 89f4d78

Browse files
fix: resolve the error related to profile context switching
Signed-off-by: Yuki I <omoge.real@gmail.com>
1 parent 3caf680 commit 89f4d78

File tree

5 files changed

+441
-62
lines changed

5 files changed

+441
-62
lines changed

acapy_agent/anoncreds/default/legacy_indy/registry.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,8 @@
7474
RevRegDefState,
7575
RevRegDefValue,
7676
)
77-
from ...models.schema import (
78-
AnonCredsSchema,
79-
GetSchemaResult,
80-
SchemaResult,
81-
SchemaState,
82-
)
83-
from ...models.schema_info import AnoncredsSchemaInfo
77+
from ...models.schema import AnonCredsSchema, GetSchemaResult, SchemaResult, SchemaState
78+
from ...models.schema_info import AnonCredsSchemaInfo
8479
from ...revocation import (
8580
CATEGORY_REV_LIST,
8681
CATEGORY_REV_REG_DEF,
@@ -828,6 +823,7 @@ async def _revoc_reg_entry_with_fix(
828823
rev_list.issuer_id,
829824
write_ledger=write_ledger,
830825
endorser_did=endorser_did,
826+
profile=profile,
831827
)
832828
except LedgerTransactionError as err:
833829
if "InvalidClientRequest" in err.roll_up:
@@ -1214,29 +1210,32 @@ async def txn_submit(
12141210
taa_accept: Optional[bool] = None,
12151211
sign_did: DIDInfo = sentinel,
12161212
write_ledger: bool = True,
1213+
profile: Optional[Profile] = None,
12171214
) -> str:
12181215
"""Submit a transaction to the ledger."""
12191216

12201217
try:
12211218
async with ledger:
1222-
return await shield(
1223-
ledger.txn_submit(
1224-
ledger_transaction,
1225-
sign=sign,
1226-
taa_accept=taa_accept,
1227-
sign_did=sign_did,
1228-
write_ledger=write_ledger,
1229-
)
1230-
)
1219+
kwargs = {
1220+
"sign": sign,
1221+
"taa_accept": taa_accept,
1222+
"sign_did": sign_did,
1223+
"write_ledger": write_ledger,
1224+
}
1225+
1226+
if profile is not None:
1227+
kwargs["profile"] = profile
1228+
1229+
return await shield(ledger.txn_submit(ledger_transaction, **kwargs))
12311230
except LedgerError as err:
12321231
raise AnonCredsRegistrationError(err.roll_up) from err
12331232

12341233
async def get_schema_info_by_id(
12351234
self, profile: Profile, schema_id: str
1236-
) -> AnoncredsSchemaInfo:
1235+
) -> AnonCredsSchemaInfo:
12371236
"""Get schema info by schema id."""
12381237
schema_id_parts = re.match(r"^(\w+):2:([^:]+):([^:]+)$", schema_id)
1239-
return AnoncredsSchemaInfo(
1238+
return AnonCredsSchemaInfo(
12401239
issuer_id=schema_id_parts.group(1),
12411240
name=schema_id_parts.group(2),
12421241
version=schema_id_parts.group(3),

acapy_agent/anoncreds/default/legacy_indy/tests/test_registry.py

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from .....anoncreds.base import AnonCredsSchemaAlreadyExists
1717
from .....anoncreds.default.legacy_indy import registry as test_module
1818
from .....anoncreds.issuer import AnonCredsIssuer
19-
from .....askar.profile_anon import (
20-
AskarAnoncredsProfileSession,
21-
)
19+
from .....askar.profile_anon import AskarAnoncredsProfileSession
2220
from .....connections.models.conn_record import ConnRecord
2321
from .....core.event_bus import EventBus
2422
from .....ledger.base import BaseLedger
@@ -31,9 +29,7 @@
3129
from .....protocols.endorse_transaction.v1_0.models.transaction_record import (
3230
TransactionRecord,
3331
)
34-
from .....revocation_anoncreds.models.issuer_cred_rev_record import (
35-
IssuerCredRevRecord,
36-
)
32+
from .....revocation_anoncreds.models.issuer_cred_rev_record import IssuerCredRevRecord
3733
from .....tests import mock
3834
from .....utils.testing import create_test_profile
3935
from ....models.credential_definition import (
@@ -51,6 +47,8 @@
5147
RevRegDefValue,
5248
)
5349
from ....models.schema import AnonCredsSchema, GetSchemaResult, SchemaResult
50+
from .....core.profile import Profile
51+
from .....wallet.did_info import DIDInfo
5452

5553
B58 = alphabet if isinstance(alphabet, str) else alphabet.decode("ascii")
5654
INDY_DID = rf"^(did:sov:)?[{B58}]{{21,22}}$"
@@ -1219,3 +1217,92 @@ async def test_get_schem_info(self):
12191217
assert result.issuer_id == "XduBsoPyEA4szYMy3pZ8De"
12201218
assert result.name == "minimal-33279d005748b3cc"
12211219
assert result.version == "1.0"
1220+
1221+
@mock.patch.object(
1222+
IndyLedgerRequestsExecutor,
1223+
"get_ledger_for_identifier",
1224+
return_value=(
1225+
"mock_ledger_id",
1226+
mock.MagicMock(
1227+
spec=BaseLedger,
1228+
send_revoc_reg_entry=mock.CoroutineMock(return_value="mock_seq_no"),
1229+
__aenter__=mock.CoroutineMock(return_value=mock.MagicMock()),
1230+
__aexit__=mock.CoroutineMock(return_value=None),
1231+
profile=mock.MagicMock(spec=Profile),
1232+
),
1233+
),
1234+
)
1235+
@mock.patch.object(AskarAnoncredsProfileSession, "handle")
1236+
async def test_register_revocation_list_passes_profile(
1237+
self, mock_askar_handle, mock_get_ledger_for_id
1238+
):
1239+
"""Test register_revocation_list passes profile kwarg via helper."""
1240+
self.profile.inject_or = mock.MagicMock()
1241+
1242+
test_profile = self.profile
1243+
1244+
test_rev_reg_def = RevRegDef(
1245+
tag="tag",
1246+
cred_def_id="IssuerDID:3:CL:1:tag",
1247+
value=RevRegDefValue(
1248+
max_cred_num=100, public_keys={}, tails_hash="", tails_location=""
1249+
),
1250+
issuer_id="IssuerDID",
1251+
type="CL_ACCUM",
1252+
)
1253+
test_rev_list = RevList(
1254+
issuer_id="IssuerDID",
1255+
current_accumulator="dummy_accum_value",
1256+
revocation_list=[0],
1257+
timestamp=1234567890,
1258+
rev_reg_def_id="IssuerDID:4:IssuerDID:3:CL:1:tag:CL_ACCUM:tag",
1259+
)
1260+
1261+
await self.registry.register_revocation_list(
1262+
test_profile,
1263+
test_rev_reg_def,
1264+
test_rev_list,
1265+
{},
1266+
)
1267+
1268+
mock_ledger_instance = mock_get_ledger_for_id.return_value[1]
1269+
1270+
mock_ledger_instance.send_revoc_reg_entry.assert_called_once()
1271+
1272+
_call_args, call_kwargs = mock_ledger_instance.send_revoc_reg_entry.call_args
1273+
1274+
assert "profile" in call_kwargs
1275+
assert call_kwargs["profile"] is test_profile
1276+
assert call_kwargs["write_ledger"] is True
1277+
1278+
async def test_registry_txn_submit_passes_profile(self):
1279+
"""Test registry txn_submit passes profile kwarg to ledger txn_submit."""
1280+
mock_ledger = mock.MagicMock(BaseLedger, autospec=True)
1281+
mock_ledger.txn_submit = mock.CoroutineMock(return_value="mock_ledger_response")
1282+
mock_ledger.__aenter__ = mock.CoroutineMock(return_value=mock_ledger)
1283+
mock_ledger.__aexit__ = mock.CoroutineMock(return_value=None)
1284+
1285+
test_profile = self.profile
1286+
test_txn_data = '{"a": 1}'
1287+
test_sign_did = mock.MagicMock(spec=DIDInfo)
1288+
1289+
await self.registry.txn_submit(
1290+
ledger=mock_ledger,
1291+
ledger_transaction=test_txn_data,
1292+
sign=True,
1293+
taa_accept=True,
1294+
sign_did=test_sign_did,
1295+
write_ledger=True,
1296+
profile=test_profile,
1297+
)
1298+
1299+
mock_ledger.txn_submit.assert_called_once()
1300+
1301+
_call_args, call_kwargs = mock_ledger.txn_submit.call_args
1302+
1303+
assert "profile" in call_kwargs
1304+
assert call_kwargs["profile"] is test_profile
1305+
assert call_kwargs["sign"] is True
1306+
assert call_kwargs["taa_accept"] is True
1307+
assert call_kwargs["sign_did"] is test_sign_did
1308+
assert call_kwargs["write_ledger"] is True

acapy_agent/ledger/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from hashlib import sha256
88
from typing import List, Optional, Sequence, Tuple, Union
99

10+
from ..core.profile import Profile
1011
from ..indy.issuer import DEFAULT_CRED_DEF_TAG, IndyIssuer, IndyIssuerError
1112
from ..messaging.valid import IndyDID
1213
from ..utils import sentinel
@@ -175,7 +176,7 @@ async def rotate_public_did_keypair(self, next_seed: Optional[str] = None) -> No
175176
"""
176177

177178
@abstractmethod
178-
async def get_wallet_public_did(self) -> DIDInfo:
179+
async def get_wallet_public_did(self, profile: Optional[Profile] = None) -> DIDInfo:
179180
"""Fetch the public DID from the wallet."""
180181

181182
@abstractmethod
@@ -193,7 +194,7 @@ async def accept_txn_author_agreement(
193194
"""Save a new record recording the acceptance of the TAA."""
194195

195196
@abstractmethod
196-
async def get_latest_txn_author_acceptance(self):
197+
async def get_latest_txn_author_acceptance(self, profile: Optional[Profile] = None):
197198
"""Look up the latest TAA acceptance."""
198199

199200
def taa_digest(self, version: str, text: str):
@@ -219,6 +220,7 @@ async def txn_submit(
219220
taa_accept: Optional[bool] = None,
220221
sign_did: DIDInfo = sentinel,
221222
write_ledger: bool = True,
223+
profile: Optional[Profile] = None,
222224
) -> str:
223225
"""Write the provided (signed and possibly endorsed) transaction to the ledger."""
224226

@@ -414,6 +416,7 @@ async def send_revoc_reg_entry(
414416
issuer_did: Optional[str] = None,
415417
write_ledger: bool = True,
416418
endorser_did: Optional[str] = None,
419+
profile: Optional[Profile] = None,
417420
) -> dict:
418421
"""Publish a revocation registry entry to the ledger."""
419422

@@ -622,9 +625,7 @@ async def send_schema_anoncreds(
622625
LedgerObjectAlreadyExistsError: If the schema already exists on the ledger.
623626
624627
"""
625-
from acapy_agent.anoncreds.default.legacy_indy.registry import (
626-
LegacyIndyRegistry,
627-
)
628+
from acapy_agent.anoncreds.default.legacy_indy.registry import LegacyIndyRegistry
628629

629630
public_info = await self.get_wallet_public_did()
630631
if not public_info:

0 commit comments

Comments
 (0)