diff --git a/src/background/services/keyShare.ts b/src/background/services/keyShare.ts index c9d66a48..5159c9a0 100644 --- a/src/background/services/keyShare.ts +++ b/src/background/services/keyShare.ts @@ -13,7 +13,7 @@ export class KeyShareService { Object.assign(this, { browser, storage }); } - async addPublicKeyToWallet(_: { walletAddressInfo: WalletAddress }) { + async addPublicKeyToWallet(_walletAddress: WalletAddress) { throw new Error('Not implemented for provided wallet yet'); } } diff --git a/src/background/services/openPayments.ts b/src/background/services/openPayments.ts index 8e48e3b5..3e6c57b5 100644 --- a/src/background/services/openPayments.ts +++ b/src/background/services/openPayments.ts @@ -312,7 +312,6 @@ export class OpenPaymentsService { walletAddressUrl, amount, recurring, - walletAddressInfo, skipAutoKeyShare, }: ConnectWalletPayload) { const walletAddress = await getWalletInformation(walletAddressUrl); @@ -347,26 +346,31 @@ export class OpenPaymentsService { await this.initClient(walletAddress.id); - if (!skipAutoKeyShare) { - const keyShare = new KeyShareService({ - browser: this.browser, - storage: this.storage, - }); - try { - await keyShare.addPublicKeyToWallet({ walletAddressInfo }); - } catch (error) { - // TODO: add error with code to be used for logic in UI - throw new Error(`ADD_PUBLIC_KEY_TO_WALLET:${error.message}`); + try { + await this.completeGrant( + amount, + walletAddress, + recurring, + InteractionIntent.CONNECT, + ); + } catch (error) { + if ( + error.message === this.t('connectWallet_error_invalidClient') && + !skipAutoKeyShare + ) { + // add key to wallet and try again + await this.addPublicKeyToWallet(walletAddress); + await this.completeGrant( + amount, + walletAddress, + recurring, + InteractionIntent.CONNECT, + ); + } else { + throw error; } } - await this.completeGrant( - amount, - walletAddress, - recurring, - InteractionIntent.CONNECT, - ); - await this.storage.set({ walletAddress, rateOfPay, @@ -501,6 +505,19 @@ export class OpenPaymentsService { return grantDetails; } + private async addPublicKeyToWallet(walletAddress: WalletAddress) { + const keyShare = new KeyShareService({ + browser: this.browser, + storage: this.storage, + }); + try { + await keyShare.addPublicKeyToWallet(walletAddress); + } catch (err) { + // TODO: add error with code to be used for logic in UI + throw new Error(`ADD_PUBLIC_KEY_TO_WALLET:${err.message}`); + } + } + private async redirectToWelcomeScreen( tabId: NonNullable, result: GrantResult, diff --git a/src/popup/components/ConnectWalletForm.tsx b/src/popup/components/ConnectWalletForm.tsx index 413df00e..f67c29fd 100644 --- a/src/popup/components/ConnectWalletForm.tsx +++ b/src/popup/components/ConnectWalletForm.tsx @@ -115,7 +115,6 @@ export const ConnectWalletForm = ({ setErrors((e) => ({ ...e, keyPair: '', connect: '' })); const res = await connectWallet({ walletAddressUrl: toWalletAddressUrl(walletAddressUrl), - walletAddressInfo, amount, recurring, skipAutoKeyShare, diff --git a/src/shared/messages.ts b/src/shared/messages.ts index 2d48895d..33103f31 100644 --- a/src/shared/messages.ts +++ b/src/shared/messages.ts @@ -89,7 +89,6 @@ export class MessageManager { // #region Popup ↦ BG export interface ConnectWalletPayload { walletAddressUrl: string; - walletAddressInfo: WalletAddress; amount: string; recurring: boolean; skipAutoKeyShare: boolean;