Skip to content

Commit

Permalink
Merge branch 'develop' into decoding_e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuri authored Nov 28, 2024
2 parents 34cec6e + 827ae99 commit 8b83e99
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
12 changes: 12 additions & 0 deletions shared/constants/multichain/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BtcAccountType, SolAccountType } from '@metamask/keyring-api';
import { BITCOIN_WALLET_SNAP_ID } from '../../lib/accounts/bitcoin-wallet-snap';
import { SOLANA_WALLET_SNAP_ID } from '../../lib/accounts/solana-wallet-snap';

export const MULTICHAIN_ACCOUNT_TYPE_TO_SNAP_ID = {
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
[BtcAccountType.P2wpkh]: BITCOIN_WALLET_SNAP_ID,
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(solana)
[SolAccountType.DataAccount]: SOLANA_WALLET_SNAP_ID,
///: END:ONLY_INCLUDE_IF
};
23 changes: 23 additions & 0 deletions shared/lib/accounts/snaps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SnapId } from '@metamask/snaps-sdk';
import { BITCOIN_WALLET_SNAP_ID } from './bitcoin-wallet-snap';
import { SOLANA_WALLET_SNAP_ID } from './solana-wallet-snap';

/**
* A constant array that contains the IDs of whitelisted multichain
* wallet Snaps. These Snaps can be used by the extension to implement
* core features (e.g. Send flow).
*
* @constant
* @type {SnapId[]}
*/
const WHITELISTED_SNAPS = [BITCOIN_WALLET_SNAP_ID, SOLANA_WALLET_SNAP_ID];

/**
* Checks if the given Snap ID corresponds to a multichain wallet Snap.
*
* @param id - The ID of the Snap to check.
* @returns True if the Snap ID is in the whitelist, false otherwise.
*/
export function isMultichainWalletSnap(id: SnapId): boolean {
return WHITELISTED_SNAPS.includes(id);
}
66 changes: 40 additions & 26 deletions ui/components/app/wallet-overview/coin-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
} from '@metamask/utils';

///: BEGIN:ONLY_INCLUDE_IF(build-flask)
import { BtcAccountType, InternalAccount } from '@metamask/keyring-api';
import { InternalAccount, isEvmAccountType } from '@metamask/keyring-api';
import { SnapId } from '@metamask/snaps-sdk';
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
import { ChainId } from '../../../../shared/constants/network';
Expand Down Expand Up @@ -92,7 +93,7 @@ import {
///: END:ONLY_INCLUDE_IF
} from '../../../store/actions';
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
import { BITCOIN_WALLET_SNAP_ID } from '../../../../shared/lib/accounts/bitcoin-wallet-snap';
import { isMultichainWalletSnap } from '../../../../shared/lib/accounts/snaps';
///: END:ONLY_INCLUDE_IF
import {
getMultichainIsEvm,
Expand Down Expand Up @@ -315,15 +316,17 @@ const CoinButtons = ({
(approval) => {
return (
approval.type === 'snap_dialog' &&
approval.origin === BITCOIN_WALLET_SNAP_ID
account.metadata.snap &&
account.metadata.snap.id === approval.origin &&
isMultichainWalletSnap(account.metadata.snap.id as SnapId)
);
},
);

if (templatedSnapApproval) {
history.push(`${CONFIRMATION_V_NEXT_ROUTE}/${templatedSnapApproval.id}`);
}
}, [unapprovedTemplatedConfirmations, history]);
}, [unapprovedTemplatedConfirmations, history, account]);
///: END:ONLY_INCLUDE_IF

const setCorrectChain = useCallback(async () => {
Expand Down Expand Up @@ -361,32 +364,43 @@ const CoinButtons = ({
},
{ excludeMetaMetricsId: false },
);
switch (account.type) {
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
case BtcAccountType.P2wpkh: {
try {
// FIXME: We switch the tab before starting the send flow (we
// faced some inconsistencies when changing it after).
await dispatch(setDefaultHomeActiveTabName('activity'));
await sendMultichainTransaction(
BITCOIN_WALLET_SNAP_ID,
account.id,
chainId as CaipChainId,
);
} catch {
// Restore the previous tab in case of any error (see FIXME comment above).
await dispatch(setDefaultHomeActiveTabName(currentActivityTabName));
}

break;
///: BEGIN:ONLY_INCLUDE_IF(build-flask)
if (!isEvmAccountType(account.type)) {
// Non-EVM (Snap) Send flow
if (!account.metadata.snap) {
throw new Error('Non-EVM needs to be Snap accounts');
}
///: END:ONLY_INCLUDE_IF
default: {
await setCorrectChain();
await dispatch(startNewDraftTransaction({ type: AssetType.native }));
history.push(SEND_ROUTE);

// TODO: Remove this once we want to enable all non-EVM Snaps
if (!isMultichainWalletSnap(account.metadata.snap.id as SnapId)) {
throw new Error(
`Non-EVM Snap is not whitelisted: ${account.metadata.snap.id}`,
);
}

try {
// FIXME: We switch the tab before starting the send flow (we
// faced some inconsistencies when changing it after).
await dispatch(setDefaultHomeActiveTabName('activity'));
await sendMultichainTransaction(account.metadata.snap.id, {
account: account.id,
scope: chainId as CaipChainId,
});
} catch {
// Restore the previous tab in case of any error (see FIXME comment above).
await dispatch(setDefaultHomeActiveTabName(currentActivityTabName));
}

// Early return, not to let the non-EVM flow slip into the native send flow.
return;
}
///: END:ONLY_INCLUDE_IF

// Native Send flow
await setCorrectChain();
await dispatch(startNewDraftTransaction({ type: AssetType.native }));
history.push(SEND_ROUTE);
}, [chainId, account, setCorrectChain]);

const handleSwapOnClick = useCallback(async () => {
Expand Down
3 changes: 2 additions & 1 deletion ui/components/app/wallet-overview/non-evm-overview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import useMultiPolling from '../../../hooks/useMultiPolling';
import { BITCOIN_WALLET_SNAP_ID } from '../../../../shared/lib/accounts/bitcoin-wallet-snap';
import NonEvmOverview from './non-evm-overview';

// We need to mock `dispatch` since we use it for `setDefaultHomeActiveTabName`.
Expand Down Expand Up @@ -60,7 +61,7 @@ const mockNonEvmAccount = {
type: 'Snap Keyring',
},
snap: {
id: 'btc-snap-id',
id: BITCOIN_WALLET_SNAP_ID,
name: 'btc-snap-name',
},
},
Expand Down
9 changes: 7 additions & 2 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6035,8 +6035,13 @@ function applyPatches(

export async function sendMultichainTransaction(
snapId: string,
account: string,
scope: string,
{
account,
scope,
}: {
account: string;
scope: string;
},
) {
await handleSnapRequest({
snapId,
Expand Down

0 comments on commit 8b83e99

Please sign in to comment.