Skip to content

Commit

Permalink
fix: try adding key only if not already added
Browse files Browse the repository at this point in the history
fix: continue connect if key add succeeds

code simplification, cleanup
  • Loading branch information
sidvishnoi committed Sep 23, 2024
1 parent fcbcfa4 commit ec8ef27
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/background/services/keyShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
53 changes: 35 additions & 18 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ export class OpenPaymentsService {
walletAddressUrl,
amount,
recurring,
walletAddressInfo,
skipAutoKeyShare,
}: ConnectWalletPayload) {
const walletAddress = await getWalletInformation(walletAddressUrl);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Tabs.Tab['id']>,
result: GrantResult,
Expand Down
1 change: 0 additions & 1 deletion src/popup/components/ConnectWalletForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export const ConnectWalletForm = ({
setErrors((e) => ({ ...e, keyPair: '', connect: '' }));
const res = await connectWallet({
walletAddressUrl: toWalletAddressUrl(walletAddressUrl),
walletAddressInfo,
amount,
recurring,
skipAutoKeyShare,
Expand Down
1 change: 0 additions & 1 deletion src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class MessageManager<TMessages extends MessageMap> {
// #region Popup ↦ BG
export interface ConnectWalletPayload {
walletAddressUrl: string;
walletAddressInfo: WalletAddress;
amount: string;
recurring: boolean;
skipAutoKeyShare: boolean;
Expand Down

0 comments on commit ec8ef27

Please sign in to comment.