From 0bbfb7bd8ca4ce114791f6f31a6cf9944bda0b58 Mon Sep 17 00:00:00 2001 From: Benoit Devos Date: Fri, 10 May 2024 15:45:26 +0200 Subject: [PATCH] fix: ID loc retrieval on verified issuer dismissal; fix: unselectAll using wrong prefix. logion-network/logion-internal#1247 --- .../model/verifiedissuerselection.model.ts | 3 +- .../services/locsynchronization.service.ts | 49 +++++++++---------- .../verifiedissuerselection.service.ts | 9 ++-- .../verifiedthirdpartyselection.model.spec.ts | 2 +- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/logion/model/verifiedissuerselection.model.ts b/src/logion/model/verifiedissuerselection.model.ts index 75de39f..e4e4bda 100644 --- a/src/logion/model/verifiedissuerselection.model.ts +++ b/src/logion/model/verifiedissuerselection.model.ts @@ -72,7 +72,8 @@ export class VerifiedIssuerSelectionRepository { return this.repository.findBy(dbSpec); } - async unselectAll(issuer: string) { + async unselectAll(issuerAccount: ValidAccountId) { + const issuer = issuerAccount.getAddress(DB_SS58_PREFIX); await this.repository.update({ issuer }, { selected: false }); } } diff --git a/src/logion/services/locsynchronization.service.ts b/src/logion/services/locsynchronization.service.ts index fe471c6..253b283 100644 --- a/src/logion/services/locsynchronization.service.ts +++ b/src/logion/services/locsynchronization.service.ts @@ -227,35 +227,32 @@ export class LocSynchronizer { const nominated = extrinsic.call.method === "nominateIssuer"; const legalOfficerAddress = ValidAccountId.polkadot(requireDefined(extrinsic.signer)); if(await this.directoryService.isLegalOfficerAddressOnNode(legalOfficerAddress)) { - const issuerAddress = Adapters.asString(extrinsic.call.args["issuer"]); - const identityLoc = await this.getIssuerIdentityLoc(legalOfficerAddress, issuerAddress); - if(identityLoc) { - logger.info("Handling nomination/dismissal of issuer %s", issuerAddress); - if(!nominated) { - this.verifiedIssuerSelectionService.unselectAll(issuerAddress); - } - this.notifyVerifiedIssuerNominatedDismissed({ - legalOfficerAddress, - nominated, - issuer: identityLoc.getDescription().userIdentity, - }); + const issuerAccount = ValidAccountId.polkadot(Adapters.asString(extrinsic.call.args["issuer"])); + const identityLoc = await this.getIssuerIdentityLoc(legalOfficerAddress, issuerAccount); + logger.info("Handling nomination/dismissal of issuer %s", issuerAccount.address); + if(!nominated) { + this.verifiedIssuerSelectionService.unselectAll(issuerAccount); } + this.notifyVerifiedIssuerNominatedDismissed({ + legalOfficerAddress, + nominated, + issuer: identityLoc.getDescription().userIdentity, + }); } } - private async getIssuerIdentityLoc(legalOfficerAddress: ValidAccountId, issuerAddress: string) { - const api = await this.polkadotService.readyApi(); - const verifiedIssuer = await api.polkadot.query.logionLoc.verifiedIssuersMap(legalOfficerAddress.address, issuerAddress); - if(verifiedIssuer.isNone) { - throw new Error(`${issuerAddress} is not an issuer of LO ${legalOfficerAddress}`); - } - - const identityLocId = api.adapters.fromLocId(verifiedIssuer.unwrap().identityLoc); - const identityLoc = await this.locRequestRepository.findById(identityLocId.toString()); - if(!identityLoc) { + private async getIssuerIdentityLoc(legalOfficerAddress: ValidAccountId, issuerAddress: ValidAccountId): Promise { + const identityLocs = await this.locRequestRepository.findBy({ + expectedLocTypes: [ "Identity" ], + expectedIdentityLocType: "Polkadot", + expectedOwnerAddress: [ legalOfficerAddress ], + expectedRequesterAddress: issuerAddress, + expectedStatuses: [ "CLOSED" ] + }); + if(identityLocs.length < 1) { throw new Error("No Identity LOC available for issuer"); } - return identityLoc; + return identityLocs[0]; } private async notifyVerifiedIssuerNominatedDismissed(args: { @@ -283,13 +280,13 @@ export class LocSynchronizer { private async handleIssuerSelectedUnselected(extrinsic: JsonExtrinsic) { const legalOfficerAddress = ValidAccountId.polkadot(requireDefined(extrinsic.signer)); if(await this.directoryService.isLegalOfficerAddressOnNode(legalOfficerAddress)) { - const issuerAddress = Adapters.asString(extrinsic.call.args["issuer"]); - const identityLoc = await this.getIssuerIdentityLoc(legalOfficerAddress, issuerAddress); + const issuerAccount = ValidAccountId.polkadot(Adapters.asString(extrinsic.call.args["issuer"])); + const identityLoc = await this.getIssuerIdentityLoc(legalOfficerAddress, issuerAccount); const selected = extrinsic.call.args["selected"] as boolean; const requestId = extractUuid("loc_id", extrinsic.call.args); const locRequest = requireDefined(await this.locRequestRepository.findById(requestId)); - logger.info("Handling selection/unselection of issuer %s", issuerAddress); + logger.info("Handling selection/unselection of issuer %s", issuerAccount.address); this.verifiedIssuerSelectionService.selectUnselect(locRequest, identityLoc, selected); this.notifyVerifiedIssuerSelectedUnselected({ legalOfficerAddress, diff --git a/src/logion/services/verifiedissuerselection.service.ts b/src/logion/services/verifiedissuerselection.service.ts index 7127206..9a1cf79 100644 --- a/src/logion/services/verifiedissuerselection.service.ts +++ b/src/logion/services/verifiedissuerselection.service.ts @@ -2,6 +2,7 @@ import { DefaultTransactional, requireDefined } from "@logion/rest-api-core"; import { injectable } from "inversify"; import { LocRequestAggregateRoot } from "../model/locrequest.model.js"; import { VerifiedIssuerAggregateRoot, VerifiedIssuerSelectionFactory, VerifiedIssuerSelectionId, VerifiedIssuerSelectionRepository } from "../model/verifiedissuerselection.model.js"; +import { ValidAccountId } from "@logion/node-api"; export abstract class VerifiedIssuerSelectionService { @@ -29,8 +30,8 @@ export abstract class VerifiedIssuerSelectionService { } // else (!selection && !select) -> skip } - async unselectAll(issuerAddress: string) { - await this.verifiedIssuerSelectionRepository.unselectAll(issuerAddress); + async unselectAll(issuerAccount: ValidAccountId) { + await this.verifiedIssuerSelectionRepository.unselectAll(issuerAccount); } } @@ -55,8 +56,8 @@ export class TransactionalVerifiedIssuerSelectionService extends VerifiedIssuerS } @DefaultTransactional() - async unselectAll(issuerAddress: string) { - return super.unselectAll(issuerAddress); + async unselectAll(issuerAccount: ValidAccountId) { + return super.unselectAll(issuerAccount); } } diff --git a/test/integration/model/verifiedthirdpartyselection.model.spec.ts b/test/integration/model/verifiedthirdpartyselection.model.spec.ts index ee8be8f..b641ebd 100644 --- a/test/integration/model/verifiedthirdpartyselection.model.spec.ts +++ b/test/integration/model/verifiedthirdpartyselection.model.spec.ts @@ -60,7 +60,7 @@ describe("VerifiedIssuerSelectionRepository - write", () => { }); it("unselects by issuer LOC ID", async () => { - await repository.unselectAll("5EBxoSssqNo23FvsDeUxjyQScnfEiGxJaNwuwqBH2Twe35BX"); + await repository.unselectAll(ValidAccountId.polkadot("5EBxoSssqNo23FvsDeUxjyQScnfEiGxJaNwuwqBH2Twe35BX")); checkNumOfRows(`SELECT * FROM issuer_selection WHERE issuer = '5EBxoSssqNo23FvsDeUxjyQScnfEiGxJaNwuwqBH2Twe35BX' AND selected IS TRUE`, 0); }); });