Skip to content

Commit 099021a

Browse files
committed
fix(core): various recovery fixes
1 parent 7093274 commit 099021a

File tree

9 files changed

+179
-137
lines changed

9 files changed

+179
-137
lines changed

src/core/agent/agent.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -795,16 +795,9 @@ class Agent {
795795
* Wipe local database and secure storage to start fresh.
796796
*/
797797
async wipeLocalDatabase(): Promise<void> {
798-
// Stop background services
799798
this.keriaNotificationService.stopPolling();
800-
801-
// Wipe the storage session (this deletes the database file)
802799
await this.storageSession.wipe(walletId);
803-
804-
// Wipe secure storage
805800
await SecureStorage.wipe();
806-
807-
// Mark agent as offline
808801
this.markAgentStatus(false);
809802
}
810803
}

src/core/agent/services/connectionService.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,6 @@ describe("Connection service of agent", () => {
979979
},
980980
];
981981

982-
identifierStorage.getAllIdentifiers = jest
983-
.fn()
984-
.mockResolvedValue([localIdentifier]);
985982
contactListMock.mockReturnValue(cloudContacts);
986983
contactStorage.findById = jest.fn().mockResolvedValue(null);
987984
connectionPairStorage.findById = jest.fn().mockResolvedValue(null);
@@ -1008,7 +1005,7 @@ describe("Connection service of agent", () => {
10081005
});
10091006
});
10101007

1011-
test("should restore connection pairs from cloud contact data during recovery", async () => {
1008+
test("should restore connection pairs from a cloud contact in multiple profiles", async () => {
10121009
const localIdentifier = { id: "Eabc123" };
10131010
const anotherLocalIdentifier = { id: "Fdef456" };
10141011
identifierStorage.getAllIdentifiers = jest
@@ -1021,15 +1018,14 @@ describe("Connection service of agent", () => {
10211018
oobi: "http://oobi.com/Dcontact1",
10221019
createdAt: "2025-01-01T00:00:00.000Z",
10231020
"Eabc123:createdAt": "2025-01-02T00:00:00.000Z",
1024-
"Xyz789:createdAt": "2025-01-03T00:00:00.000Z", // This one should be ignored
1021+
"Fdef456:createdAt": "2025-01-03T00:00:00.000Z", // This one should be ignored
10251022
};
10261023
contactListMock.mockReturnValue([cloudContact]);
10271024
contactStorage.findById = jest.fn().mockResolvedValue(null);
10281025
connectionPairStorage.findById = jest.fn().mockResolvedValue(null);
10291026

10301027
await connectionService.syncKeriaContacts();
10311028

1032-
// Verify that the main contact record was saved
10331029
expect(contactStorage.save).toHaveBeenCalledTimes(1);
10341030
expect(contactStorage.save).toHaveBeenCalledWith({
10351031
id: "Dcontact1",
@@ -1038,9 +1034,7 @@ describe("Connection service of agent", () => {
10381034
groupId: undefined,
10391035
createdAt: expect.any(Date),
10401036
});
1041-
1042-
// Verify that the connection pair for the matching identifier was created
1043-
expect(connectionPairStorage.save).toHaveBeenCalledTimes(1);
1037+
expect(connectionPairStorage.save).toHaveBeenCalledTimes(2);
10441038
expect(connectionPairStorage.save).toHaveBeenCalledWith({
10451039
id: "Eabc123:Dcontact1",
10461040
contactId: "Dcontact1",
@@ -1049,6 +1043,14 @@ describe("Connection service of agent", () => {
10491043
pendingDeletion: false,
10501044
createdAt: expect.any(Date),
10511045
});
1046+
expect(connectionPairStorage.save).toHaveBeenCalledWith({
1047+
id: "Fdef456:Dcontact1",
1048+
contactId: "Dcontact1",
1049+
identifier: "Fdef456",
1050+
creationStatus: CreationStatus.COMPLETE,
1051+
pendingDeletion: false,
1052+
createdAt: expect.any(Date),
1053+
});
10521054
});
10531055

10541056
test("Can get multisig linked contacts", async () => {

src/core/agent/services/connectionService.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,6 @@ class ConnectionService extends AgentService {
709709
}
710710

711711
async syncKeriaContacts(): Promise<void> {
712-
const localIdentifiers = await this.identifierStorage.getAllIdentifiers();
713-
const localAIDs = localIdentifiers.map((id) => id.id);
714712
const cloudContacts = await this.props.signifyClient.contacts().list();
715713

716714
for (const contact of cloudContacts) {
@@ -732,22 +730,18 @@ class ConnectionService extends AgentService {
732730
const keyParts = key.split(":");
733731
if (keyParts.length === 2 && keyParts[1] === "createdAt") {
734732
const aid = keyParts[0];
735-
if (localAIDs.includes(aid)) {
736-
const pairId = `${aid}:${contact.id}`;
737-
const pairExists = await this.connectionPairStorage.findById(
738-
pairId
739-
);
740-
741-
if (!pairExists) {
742-
await this.connectionPairStorage.save({
743-
id: pairId,
744-
contactId: contact.id,
745-
identifier: aid,
746-
creationStatus: CreationStatus.COMPLETE,
747-
pendingDeletion: false,
748-
createdAt: new Date(contact[key] as string),
749-
});
750-
}
733+
const pairId = `${aid}:${contact.id}`;
734+
const pairExists = await this.connectionPairStorage.findById(pairId);
735+
736+
if (!pairExists) {
737+
await this.connectionPairStorage.save({
738+
id: pairId,
739+
contactId: contact.id,
740+
identifier: aid,
741+
creationStatus: CreationStatus.COMPLETE,
742+
pendingDeletion: false,
743+
createdAt: new Date(contact[key] as string),
744+
});
751745
}
752746
}
753747
}

src/core/agent/services/credentialService.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,13 @@ class CredentialService extends AgentService {
217217
let iteration = 0;
218218

219219
while (returned !== 0) {
220+
// Pagination must be sorted to ensure if KERIA parses new credentials the re-indexing is much less likely to interfere
220221
const result = await this.props.signifyClient.credentials().list({
221222
skip: iteration * 24,
222-
limit: 24 + iteration * 24,
223+
limit: 24,
224+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
225+
// @ts-ignore - @TODO - foconnor: object[] type is incorrect in signify-ts, to correct to string[].
226+
sort: ["-a-dt"],
223227
});
224228
cloudCredentials.push(...result);
225229

src/core/agent/services/identifierService.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,16 +1240,14 @@ describe("Single sig service of agent", () => {
12401240
}
12411241
);
12421242
expect(updateIdentifierMock).toBeCalledWith(groupMemberMetadataRecord.id, {
1243-
name: `XX-QOP7zdP-kJs8nlwVR290XfyAk:${
1244-
groupMemberMetadataRecord.groupMetadata!.groupId
1245-
}:${groupMemberMetadataRecord.displayName}`,
1243+
name: `1.2.0.2:XX-QOP7zdP-kJs8nlwVR290XfyAk:1:group-id:testUser:${groupMemberMetadataRecord.displayName}`,
12461244
});
12471245
expect(identifierStorage.updateIdentifierMetadata).toBeCalledWith(
12481246
groupMetadataRecord.id,
12491247
{ isDeleted: true, pendingDeletion: false }
12501248
);
12511249
expect(updateIdentifierMock).toBeCalledWith(groupMetadataRecord.id, {
1252-
name: `XX-0ADQpus-mQmmO4mgWcT3ekDz:${groupMetadataRecord.displayName}`,
1250+
name: `1.2.0.2:XX-0ADQpus-mQmmO4mgWcT3ekDz:${groupMetadataRecord.displayName}`,
12531251
});
12541252
expect(markNotificationMock).toBeCalledWith(findNotificationsResult[0].id);
12551253
expect(notificationStorage.deleteById).toBeCalledWith(
@@ -1717,7 +1715,7 @@ describe("Single sig service of agent", () => {
17171715
{ isDeleted: true, pendingDeletion: false }
17181716
);
17191717
expect(updateIdentifierMock).toBeCalledWith(identifierMetadataRecord.id, {
1720-
name: `XX-0ADQpus-mQmmO4mgWcT3ekDz:${identifierMetadataRecord.displayName}`,
1718+
name: `1.2.0.2:XX-0ADQpus-mQmmO4mgWcT3ekDz:${identifierMetadataRecord.displayName}`,
17211719
});
17221720
expect(markNotificationMock).toBeCalledWith(findNotificationsResult[0].id);
17231721
expect(notificationStorage.deleteById).toBeCalledWith(
@@ -1847,7 +1845,7 @@ describe("Single sig service of agent", () => {
18471845
{ isDeleted: true, pendingDeletion: false }
18481846
);
18491847
expect(updateIdentifierMock).toBeCalledWith(identifierMetadataRecord.id, {
1850-
name: `XX-0ADQpus-mQmmO4mgWcT3ekDz:${identifierMetadataRecord.displayName}`,
1848+
name: `1.2.0.2:XX-0ADQpus-mQmmO4mgWcT3ekDz:${identifierMetadataRecord.displayName}`,
18511849
});
18521850
expect(markNotificationMock).toBeCalledWith(findNotificationsResult[0].id);
18531851
expect(notificationStorage.deleteById).toBeCalledWith(
@@ -1922,7 +1920,7 @@ describe("Single sig service of agent", () => {
19221920
{ isDeleted: true, pendingDeletion: false }
19231921
);
19241922
expect(updateIdentifierMock).toBeCalledWith(groupMemberMetadataRecord.id, {
1925-
name: `XX-0ADQpus-mQmmO4mgWcT3ekDz:${groupMemberMetadataRecord.displayName}`,
1923+
name: `1.2.0.2:XX-0ADQpus-mQmmO4mgWcT3ekDz:1:${groupMemberMetadataRecord.groupMetadata?.groupId}:${groupMemberMetadataRecord.groupMetadata?.proposedUsername}:${groupMemberMetadataRecord.displayName}`,
19261924
});
19271925
expect(markNotificationMock).toBeCalledWith(findNotificationsResult[0].id);
19281926
expect(notificationStorage.deleteById).toBeCalledWith(
@@ -2028,16 +2026,14 @@ describe("Single sig service of agent", () => {
20282026
}
20292027
);
20302028
expect(updateIdentifierMock).toBeCalledWith(groupMemberMetadataRecord.id, {
2031-
name: `XX-QOP7zdP-kJs8nlwVR290XfyAk:${
2032-
groupMemberMetadataRecord.groupMetadata!.groupId
2033-
}:${groupMemberMetadataRecord.displayName}`,
2029+
name: `1.2.0.2:XX-QOP7zdP-kJs8nlwVR290XfyAk:1:group-id:testUser:${groupMemberMetadataRecord.displayName}`,
20342030
});
20352031
expect(identifierStorage.updateIdentifierMetadata).toBeCalledWith(
20362032
groupMetadataRecord.id,
20372033
{ isDeleted: true, pendingDeletion: false }
20382034
);
20392035
expect(updateIdentifierMock).toBeCalledWith(groupMetadataRecord.id, {
2040-
name: `XX-0ADQpus-mQmmO4mgWcT3ekDz:${groupMetadataRecord.displayName}`,
2036+
name: `1.2.0.2:XX-0ADQpus-mQmmO4mgWcT3ekDz:${groupMetadataRecord.displayName}`,
20412037
});
20422038
expect(markNotificationMock).toBeCalledWith(findNotificationsResult[0].id);
20432039
expect(notificationStorage.deleteById).toBeCalledWith(

src/core/agent/services/identifierService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ class IdentifierService extends AgentService {
786786
creationStatus,
787787
createdAt: new Date(identifierDetail.icp_dt),
788788
sxlt: identifierDetail.salty?.sxlt,
789-
isDeleted: identifier.name.startsWith(
789+
isDeleted: parsed.theme.startsWith(
790790
IdentifierService.DELETED_IDENTIFIER_THEME
791791
),
792792
});
@@ -800,7 +800,7 @@ class IdentifierService extends AgentService {
800800
creationStatus,
801801
createdAt: new Date(identifierDetail.icp_dt),
802802
sxlt: identifierDetail.salty?.sxlt,
803-
isDeleted: identifier.name.startsWith(
803+
isDeleted: parsed.theme.startsWith(
804804
IdentifierService.DELETED_IDENTIFIER_THEME
805805
),
806806
});
@@ -857,7 +857,7 @@ class IdentifierService extends AgentService {
857857
groupUsername: mhabParsed.groupMetadata.proposedUsername,
858858
creationStatus,
859859
createdAt: new Date(identifierDetail.icp_dt),
860-
isDeleted: identifier.name.startsWith(
860+
isDeleted: parsed.theme.startsWith(
861861
IdentifierService.DELETED_IDENTIFIER_THEME
862862
),
863863
});

0 commit comments

Comments
 (0)