Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/background/services/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class WalletService {
return {
walletAddress: { ...walletAddress, url },
isKeyAdded,
isKeyAutoAddSupported: KeyAutoAddService.supports(walletAddress),
...budgetInfo,
};
}
Expand All @@ -97,7 +98,6 @@ export class WalletService {
amount,
recurring,
autoKeyAdd,
autoKeyAddConsent,
} = params;

await this.generateKeys();
Expand All @@ -109,17 +109,13 @@ export class WalletService {
const intent = InteractionIntent.CONNECT;

const isKeyAdded = await isKeyAddedToWallet(walletAddress.id, keyId);
const shouldAutoAddKey = !isKeyAdded && !!autoKeyAddConsent;
if (!isKeyAdded) {
if (!autoKeyAdd) {
throw new ErrorWithKey('connectWallet_error_invalidClient');
}
if (!KeyAutoAddService.supports(walletAddress)) {
throw new ErrorWithKey('connectWalletKeyService_error_notImplemented');
}
if (!autoKeyAddConsent) {
throw new ErrorWithKey('connectWalletKeyService_error_noConsent');
}
}

let tabId: TabId | undefined;
Expand All @@ -143,7 +139,7 @@ export class WalletService {
);
};

if (shouldAutoAddKey) {
if (!isKeyAdded && autoKeyAdd) {
try {
this.setConnectState(this.t('connectWalletKeyService_text_stepAddKey'));
await closeTabsByFilter(browser, closeTabFilter);
Expand Down
43 changes: 31 additions & 12 deletions src/pages/popup/components/ConnectWalletForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ export const ConnectWalletForm = ({
walletAddressUrl: null,
amount: null,
keyPair:
state?.status === 'error:key'
state?.status === 'error:key' ||
(state?.status === 'error' &&
typeof state.error === 'object' &&
state.error?.key === 'connectWallet_error_invalidClient')
? toErrorInfo(deepClone(state.error))
: null,
connect:
state?.status === 'error' ? toErrorInfo(deepClone(state.error)) : null,
state?.status === 'error' &&
(typeof state.error === 'object'
? state.error.key !== 'connectWallet_error_invalidClient'
: true)
? toErrorInfo(deepClone(state.error))
: null,
Comment on lines -111 to +123
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This situation will be improved in coming follow-ups.

});
const [isValidating, setIsValidating] = React.useState({
walletAddressUrl: false,
Expand Down Expand Up @@ -252,6 +260,15 @@ export const ConnectWalletForm = ({
walletAddressInfo ??
(await getWalletInfo(toWalletAddressUrl(walletAddressUrl)));

if (
!walletInfo.isKeyAdded &&
!autoKeyAddConsent.current &&
walletInfo.isKeyAutoAddSupported
) {
setShowConsent(true);
return;
}

const amountInput = document.querySelector<HTMLInputElement>(
'input#connectAmount',
)!;
Expand All @@ -270,9 +287,7 @@ export const ConnectWalletForm = ({

try {
setIsSubmitting(true);
let skipAutoKeyShare = autoKeyShareFailed || !keyAddNeeded;
if (errors.keyPair) {
skipAutoKeyShare = true;
setAutoKeyShareFailed(true);
}

Expand All @@ -283,19 +298,17 @@ export const ConnectWalletForm = ({
rateOfPay: walletInfo.defaultRateOfPay,
maxRateOfPay: walletInfo.maxRateOfPay,
recurring,
autoKeyAdd: !skipAutoKeyShare,
autoKeyAddConsent: autoKeyAddConsent.current,
autoKeyAdd: autoKeyAddConsent.current,
});
if (res.success) {
onConnect();
} else {
if (isErrorWithKey(res.error)) {
const error = res.error;
if (error.key.startsWith('connectWalletKeyService_error_')) {
if (error.key === 'connectWalletKeyService_error_noConsent') {
setShowConsent(true);
return;
}
if (
error.key.startsWith('connectWalletKeyService_error_') ||
error.key === 'connectWallet_error_invalidClient'
) {
setErrors((prev) => ({ ...prev, keyPair: toErrorInfo(error) }));
} else {
setErrors((prev) => ({ ...prev, connect: toErrorInfo(error) }));
Expand All @@ -317,6 +330,12 @@ export const ConnectWalletForm = ({
}
}, [defaultValues.walletAddressUrl, handleWalletAddressUrlChange]);

const showManualKeyCopy =
(errors.keyPair ||
autoKeyShareFailed ||
walletAddressInfo?.isKeyAutoAddSupported === false) &&
keyAddNeeded;

if (showConsent) {
return (
<AutoKeyAddConsent
Expand Down Expand Up @@ -447,7 +466,7 @@ export const ConnectWalletForm = ({
)}
</fieldset>

{(errors.keyPair || autoKeyShareFailed) && keyAddNeeded && (
{showManualKeyCopy && (
<ManualKeyPairNeeded
error={{
message: t('connectWallet_error_failedAutoKeyAdd'),
Expand Down
7 changes: 6 additions & 1 deletion src/shared/helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ export const getWalletInformation = async (

export const getConnectWalletBudgetInfo = async (
walletAddress: WalletAddress,
): Promise<Omit<ConnectWalletAddressInfo, 'walletAddress' | 'isKeyAdded'>> => {
): Promise<
Omit<
ConnectWalletAddressInfo,
'walletAddress' | 'isKeyAdded' | 'isKeyAutoAddSupported'
>
> => {
const {
DEFAULT_BUDGET,
DEFAULT_RATE_OF_PAY,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface ConnectWalletAddressInfo {
defaultBudget: number;
defaultRateOfPay: AmountValue;
maxRateOfPay: AmountValue;
isKeyAutoAddSupported: boolean;
isKeyAdded: boolean;
}

Expand All @@ -117,7 +118,6 @@ export interface ConnectWalletPayload {
maxRateOfPay: AmountValue;
recurring: boolean;
autoKeyAdd: boolean;
autoKeyAddConsent: boolean | null;
}

export interface ReconnectWalletPayload {
Expand Down