Skip to content

Commit

Permalink
feat: add authorizedEids to multisig records
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-HocNguyena committed Jul 15, 2024
1 parent 6d71b37 commit e0f5573
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/core/agent/records/identifierMetadataRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IdentifierMetadataRecordProps {
signifyOpName?: string;
multisigManageAid?: string;
groupMetadata?: groupMetadata;
authorizedEids?: string[];
}

class IdentifierMetadataRecord extends BaseRecord {
Expand All @@ -30,6 +31,7 @@ class IdentifierMetadataRecord extends BaseRecord {
theme!: number;
multisigManageAid?: string | undefined;
groupMetadata?: groupMetadata;
authorizedEids?: string[] | undefined;

static readonly type = "IdentifierMetadataRecord";
readonly type = IdentifierMetadataRecord.type;
Expand All @@ -49,6 +51,7 @@ class IdentifierMetadataRecord extends BaseRecord {
this.createdAt = props.createdAt ?? new Date();
this.theme = props.theme;
this.groupMetadata = props.groupMetadata;
this.authorizedEids = props.authorizedEids;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/agent/records/identifierStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class IdentifierStorage {
| "isPending"
| "isDeleted"
| "groupMetadata"
| "authorizedEids"
>
>
): Promise<void> {
Expand All @@ -70,6 +71,8 @@ class IdentifierStorage {
identifierMetadataRecord.isDeleted = metadata.isDeleted;
if (metadata.groupMetadata !== undefined)
identifierMetadataRecord.groupMetadata = metadata.groupMetadata;
if (metadata.authorizedEids !== undefined)
identifierMetadataRecord.authorizedEids = metadata.authorizedEids;
await this.storageService.update(identifierMetadataRecord);
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/agent/services/multiSigService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,7 @@ describe("Multisig sig service of agent", () => {
expect(sendExchangesMock).toBeCalledTimes(
multisigMockMembers["signing"].length
);
expect(identifierStorage.updateIdentifierMetadata).toBeCalled();
});

test("Can join end role authorization", async () => {
Expand Down Expand Up @@ -1584,5 +1585,6 @@ describe("Multisig sig service of agent", () => {
},
});
expect(sendExchangesMock).toBeCalled();
expect(identifierStorage.updateIdentifierMetadata).toBeCalled();
});
});
56 changes: 36 additions & 20 deletions src/core/agent/services/multiSigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,6 @@ class MultiSigService extends AgentService {
const multisigId = op.name.split(".")[1];
const isPending = !op.done;

if (isPending) {
const pendingOperation = await this.operationPendingStorage.save({
id: op.name,
recordType: OperationPendingRecordType.Group,
});
Agent.agent.signifyNotifications.addPendingOperationToQueue(
pendingOperation
);
}
await this.identifierStorage.createIdentifierMetadataRecord({
id: multisigId,
displayName: ourMetadata.displayName,
Expand All @@ -147,12 +138,25 @@ class MultiSigService extends AgentService {
signifyOpName: result.op.name, //we save the signifyOpName here to sync the multisig's status later
isPending,
multisigManageAid: ourIdentifier,
authorizedEids: [],
});
ourMetadata.groupMetadata.groupCreated = true;
await this.identifierStorage.updateIdentifierMetadata(
ourMetadata.id,
ourMetadata
);
if (isPending) {
const pendingOperation = await this.operationPendingStorage.save({
id: op.name,
recordType: OperationPendingRecordType.Group,
});
Agent.agent.signifyNotifications.addPendingOperationToQueue(
pendingOperation
);
} else {
// Trigger the end role authorization if the operation is done
await Agent.agent.multiSigs.endRoleAuthorization(signifyName);
}
return { identifier: multisigId, signifyName, isPending };
}

Expand Down Expand Up @@ -468,16 +472,6 @@ class MultiSigService extends AgentService {
const multisigId = op.name.split(".")[1];
const isPending = !op.done;

if (isPending) {
const pendingOperation = await this.operationPendingStorage.save({
id: op.name,
recordType: OperationPendingRecordType.Group,
});
Agent.agent.signifyNotifications.addPendingOperationToQueue(
pendingOperation
);
}

await this.identifierStorage.createIdentifierMetadataRecord({
id: multisigId,
displayName: meta.displayName,
Expand All @@ -493,6 +487,19 @@ class MultiSigService extends AgentService {
identifier
);

if (isPending) {
const pendingOperation = await this.operationPendingStorage.save({
id: op.name,
recordType: OperationPendingRecordType.Group,
});
Agent.agent.signifyNotifications.addPendingOperationToQueue(
pendingOperation
);
} else {
// Trigger the end role authorization if the operation is done
await Agent.agent.multiSigs.endRoleAuthorization(signifyName);
}

return { identifier: multisigId, signifyName, isPending };
}

Expand Down Expand Up @@ -799,14 +806,15 @@ class MultiSigService extends AgentService {
const ourAid: Aid = await this.props.signifyClient
.identifiers()
.get(ourIdentifier.signifyName as string);
const authorizedEids = [];
for (const member of multisigMembers) {
const eid = Object.keys(member.ends.agent)[0]; //agent of member
const stamp = new Date().toISOString().replace("Z", "000+00:00");

const endRoleRes = await this.props.signifyClient
.identifiers()
.addEndRole(multisigSignifyName, "agent", eid, stamp);
const op = await endRoleRes.op();
await endRoleRes.op();
const rpy = endRoleRes.serder;
const sigs = endRoleRes.sigs;
const mstate = hab["state"];
Expand All @@ -831,7 +839,11 @@ class MultiSigService extends AgentService {
recp,
{ gid: aid }
);
authorizedEids.push(eid);
}
await this.identifierStorage.updateIdentifierMetadata(hab["prefix"], {
authorizedEids,
});
}

async joinAuthorization(requestExn: AuthorizationRequestExn): Promise<void> {
Expand Down Expand Up @@ -883,6 +895,10 @@ class MultiSigService extends AgentService {
recp,
{ gid: hab["prefix"] }
);
multisigMetadataRecord.authorizedEids?.push(rpyeid);
await this.identifierStorage.updateIdentifierMetadata(hab["prefix"], {
authorizedEids: multisigMetadataRecord.authorizedEids,
});
}
}

Expand Down
28 changes: 15 additions & 13 deletions src/core/agent/services/signifyNotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,22 @@ class SignifyNotificationService extends AgentService {
await this.markNotification(notif.i);
return;
}
const multisigIdentifier = await Agent.agent.identifiers.getIdentifier(
multisigId
);
const multisigIdentifier =
await this.identifierStorage.getIdentifierMetadata(multisigId);
if (!multisigIdentifier) {
await this.markNotification(notif.i);
return;
}
if (multisigNotification[0].exn.e.rpy.r == "/end/role/add") {
const rpyRoute = multisigNotification[0].exn.e.rpy.r;
if (
rpyRoute === "/end/role/add" &&
multisigIdentifier.authorizedEids?.includes(
multisigNotification[0].exn.e.rpy.a.eid
)
) {
await this.markNotification(notif.i);
return;
} else if (rpyRoute === "/end/role/add") {
await this.markNotification(notif.i);
await Agent.agent.multiSigs.joinAuthorization(
multisigNotification[0].exn
Expand Down Expand Up @@ -382,15 +390,9 @@ class SignifyNotificationService extends AgentService {
// Trigger add end role authorization for multi-sigs
const multisigIdentifier =
await this.identifierStorage.getIdentifierMetadata(recordId);
const { ourIdentifier } =
await Agent.agent.multiSigs.getMultisigParticipants(
multisigIdentifier.signifyName
);
if (ourIdentifier.groupMetadata?.groupInitiator) {
await Agent.agent.multiSigs.endRoleAuthorization(
multisigIdentifier.signifyName
);
}
await Agent.agent.multiSigs.endRoleAuthorization(
multisigIdentifier.signifyName
);
callback({
opType: pendingOperation.recordType,
oid: recordId,
Expand Down

0 comments on commit e0f5573

Please sign in to comment.