From 9dc093e99324f0ffb32e92f921653a2272b71ee0 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Mon, 5 Aug 2024 13:05:53 +0100 Subject: [PATCH] Return result from `linkIdentities` --- frontend/openchat-client/src/openchat.ts | 5 +++-- frontend/openchat-shared/src/domain/identity.ts | 2 ++ frontend/openchat-shared/src/domain/worker.ts | 3 +++ frontend/openchat-worker/src/worker.ts | 8 +++++--- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/frontend/openchat-client/src/openchat.ts b/frontend/openchat-client/src/openchat.ts index d48c96fbd1..0db3f11b7b 100644 --- a/frontend/openchat-client/src/openchat.ts +++ b/frontend/openchat-client/src/openchat.ts @@ -420,6 +420,7 @@ import type { SubmitProofOfUniquePersonhoodResponse, Achievement, PayForDiamondMembershipResponse, + LinkIdentitiesResponse, } from "openchat-shared"; import { AuthProvider, @@ -7356,8 +7357,8 @@ export class OpenChat extends OpenChatAgentWorker { initiatorDelegation: DelegationChain, approverKey: ECDSAKeyIdentity, approverDelegation: DelegationChain, - ) { - this.sendRequest({ + ): Promise { + return this.sendRequest({ kind: "linkIdentities", initiatorKey: initiatorKey.getKeyPair(), initiatorDelegation: initiatorDelegation.toJSON(), diff --git a/frontend/openchat-shared/src/domain/identity.ts b/frontend/openchat-shared/src/domain/identity.ts index f44c178b09..3147468a21 100644 --- a/frontend/openchat-shared/src/domain/identity.ts +++ b/frontend/openchat-shared/src/domain/identity.ts @@ -96,3 +96,5 @@ export type ApproveIdentityLinkResponse = | "link_request_not_found" | "invalid_signature" | "delegation_too_old"; + +export type LinkIdentitiesResponse = InitiateIdentityLinkResponse | ApproveIdentityLinkResponse; diff --git a/frontend/openchat-shared/src/domain/worker.ts b/frontend/openchat-shared/src/domain/worker.ts index d621f3002f..31a1016e5e 100644 --- a/frontend/openchat-shared/src/domain/worker.ts +++ b/frontend/openchat-shared/src/domain/worker.ts @@ -169,6 +169,7 @@ import type { GenerateChallengeResponse, GetDelegationResponse, GetOpenChatIdentityResponse, + LinkIdentitiesResponse, PrepareDelegationResponse, SiwePrepareLoginResponse, SiwsPrepareLoginResponse, @@ -2104,4 +2105,6 @@ export type WorkerResult = T extends Init ? void : T extends SubmitProofOfUniquePersonhood ? SubmitProofOfUniquePersonhoodResponse + : T extends LinkIdentities + ? LinkIdentitiesResponse : never; diff --git a/frontend/openchat-worker/src/worker.ts b/frontend/openchat-worker/src/worker.ts index 8b323b3058..17fc362ef8 100644 --- a/frontend/openchat-worker/src/worker.ts +++ b/frontend/openchat-worker/src/worker.ts @@ -23,6 +23,7 @@ import { type GetOpenChatIdentityResponse, type ChallengeAttempt, type CreateOpenChatIdentityError, + type LinkIdentitiesResponse, } from "openchat-shared"; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -1813,7 +1814,7 @@ async function linkIdentities( initiatorDelegation: JsonnableDelegationChain, approverKey: CryptoKeyPair, approverDelegation: JsonnableDelegationChain, -) { +): Promise { const initiatorIdentity = DelegationIdentity.fromDelegation( await ECDSAKeyIdentity.fromKeyPair(initiatorKey), DelegationChain.fromJSON(initiatorDelegation), @@ -1833,7 +1834,8 @@ async function linkIdentities( } const initiateResponse = await initiatorAgent.initiateIdentityLink(approver); - if (initiateResponse === "success") { - await approverAgent.approveIdentityLink(initiator); + if (initiateResponse !== "success") { + return initiateResponse; } + return await approverAgent.approveIdentityLink(initiator); }