Skip to content

added CAIP-25 to remove the round-trip required for wallet_getCapabilities #540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2024
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
1 change: 1 addition & 0 deletions advanced/dapps/react-dapp-v2/src/components/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const SFullWidthContainer = styled.div`

export const SAccounts = styled(SFullWidthContainer)`
justify-content: space-between;
align-items: start;
& > div {
margin: 12px 0;
flex: 1 0 100%;
Expand Down
42 changes: 31 additions & 11 deletions advanced/dapps/react-dapp-v2/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
DEFAULT_TEZOS_METHODS,
DEFAULT_EIP155_OPTIONAL_METHODS,
DEFAULT_EIP5792_METHODS,
GetCapabilitiesResult,
} from "../constants";
import { AccountAction, setLocaleStorageTestnetFlag } from "../helpers";
import Toggle from "../components/Toggle";
Expand All @@ -45,6 +46,7 @@ import { useChainData } from "../contexts/ChainDataContext";
import Icon from "../components/Icon";
import OriginSimulationDropdown from "../components/OriginSimulationDropdown";
import LoaderModal from "../modals/LoaderModal";
import { numberToHex } from "@walletconnect/encoding";

// Normal import does not work here
const { version } = require("@walletconnect/sign-client/package.json");
Expand Down Expand Up @@ -145,7 +147,7 @@ const Home: NextPage = () => {
});
}

const getEthereumActions = (): AccountAction[] => {
const getEthereumActions = (chainId:string,address:string): AccountAction[] => {
const actions = {
[DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION]: {
method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION,
Expand Down Expand Up @@ -213,18 +215,36 @@ const Home: NextPage = () => {
};

let availableActions: AccountAction[] = [];

const chainIdAsHex = `0x${numberToHex(parseInt(chainId))}`
const capabilitiesJson = session?.sessionProperties?.['capabilities']
const walletCapabilities = capabilitiesJson && JSON.parse(capabilitiesJson)
session?.namespaces?.["eip155"].methods.forEach((methodName) => {
const action: AccountAction | undefined =
actions[methodName as keyof typeof actions];
if (action) {
availableActions.push(action);
const action: AccountAction | undefined = actions[methodName as keyof typeof actions];
// Determine if the method requires additional capability checks
const requiresCapabilityCheck = ["wallet_sendCalls", "wallet_getCallsStatus"].includes(methodName);
// Check capabilities only if the method requires it
if (!requiresCapabilityCheck || hasEIP7592RequiredCapabilities(address, chainIdAsHex, walletCapabilities)) {
availableActions.push(action);
}
}
});
);

return availableActions;
};

const hasEIP7592RequiredCapabilities = (address: string, chainId: string, walletCapabilities: any): boolean => {
if(!walletCapabilities) return false;
const addressCapabilities: GetCapabilitiesResult | undefined = walletCapabilities[address];
if (
addressCapabilities &&
addressCapabilities[chainId] &&
(addressCapabilities[chainId]['atomicBatch']?.supported ||
addressCapabilities[chainId]['paymasterService']?.supported ||
addressCapabilities[chainId]['sessionKey']?.supported)
) return true; // Capabilities are supported
return false; // Capabilities are not supported or not defined
}

const getCosmosActions = (): AccountAction[] => {
const onSignDirect = async (chainId: string, address: string) => {
openRequestModal();
Expand Down Expand Up @@ -425,11 +445,11 @@ const Home: NextPage = () => {
];
};

const getBlockchainActions = (chainId: string) => {
const [namespace] = chainId.split(":");
const getBlockchainActions = (account:string) => {
const [namespace, chainId, address] = account.split(":");
switch (namespace) {
case "eip155":
return getEthereumActions();
return getEthereumActions(chainId,address);
case "cosmos":
return getCosmosActions();
case "solana":
Expand Down Expand Up @@ -547,7 +567,7 @@ const Home: NextPage = () => {
address={address}
chainId={chainId}
balances={balances}
actions={getBlockchainActions(chainId)}
actions={getBlockchainActions(account)}
/>
);
})}
Expand Down
19 changes: 19 additions & 0 deletions advanced/wallets/react-wallet-v2/src/utils/EIP5792WalletUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { eip155Addresses } from './EIP155WalletUtil'
import { GetCapabilitiesResult, supportedEIP5792CapabilitiesForEOA, supportedEIP5792CapabilitiesForSCA } from '@/data/EIP5792Data'

export function getWalletCapabilities(addresses:string[]){
const walletCapabilities:GetCapabilitiesResult = {}
addresses.forEach(address => {
const namespacesAddress = address.split(':');
// address will be the last index element whether
// its a simple address or namespace:chainId:address
const addr = namespacesAddress[namespacesAddress.length - 1 ] ;
if(eip155Addresses.includes(addr)){
// no capabilities support for EOA for now.
return;
}
walletCapabilities[addr] = supportedEIP5792CapabilitiesForSCA

})
return walletCapabilities
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import SettingsStore from '@/store/SettingsStore'
import usePriorityAccounts from '@/hooks/usePriorityAccounts'
import useSmartAccounts from '@/hooks/useSmartAccounts'
import { EIP5792_METHODS } from '@/data/EIP5792Data'
import { getWalletCapabilities } from '@/utils/EIP5792WalletUtil'

const StyledText = styled(Text, {
fontWeight: 400
Expand Down Expand Up @@ -253,13 +254,17 @@ export default function SessionProposalModal() {
setIsLoadingApprove(true)
try {
if (reorderedEip155Accounts.length > 0) {
// we should append the smart accounts to the available eip155 accounts
namespaces.eip155.accounts = namespaces.eip155.accounts.concat(reorderedEip155Accounts)
// reorderedEip155Accounts includes Smart Accounts(if enabled) and EOA's
namespaces.eip155.accounts = reorderedEip155Accounts
}

//get capabilities for all reorderedEip155Accounts in wallet
const capabilities = getWalletCapabilities(reorderedEip155Accounts)
const sessionProperties = { capabilities: JSON.stringify(capabilities) }

await web3wallet.approveSession({
id: proposal.id,
namespaces
namespaces,
sessionProperties
})
SettingsStore.setSessions(Object.values(web3wallet.getActiveSessions()))
} catch (e) {
Expand Down