forked from cerc-io/laconic-wallet
Update namespace after adding network (#81)
* Update namespace * Add eth accounts * Add isSubmitted * Send namespace to dApp based on selected network while pairing * Send transactions on configured chain * Fix modal UI * Use namespace by default for chainId * Use ethereum mainnet as default chain * Use method names from constants file * Combine required and optional namespaces * Fix chainId * Fix networksData * Remove cosmos denom * Remove todo * Use lowercase denom --------- Co-authored-by: Shreerang Kale <shreerangkale@gmail.com>
This commit is contained in:
parent
e4fe88939c
commit
23fa5415ae
@ -1,6 +1,7 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { ScrollView, TouchableOpacity, View } from 'react-native';
|
import { ScrollView, TouchableOpacity, View } from 'react-native';
|
||||||
import { Button, List, Text, useTheme } from 'react-native-paper';
|
import { Button, List, Text, useTheme } from 'react-native-paper';
|
||||||
|
import mergeWith from 'lodash/mergeWith';
|
||||||
|
|
||||||
import { useNavigation } from '@react-navigation/native';
|
import { useNavigation } from '@react-navigation/native';
|
||||||
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
||||||
@ -12,7 +13,8 @@ import HDPathDialog from './HDPathDialog';
|
|||||||
import AccountDetails from './AccountDetails';
|
import AccountDetails from './AccountDetails';
|
||||||
import { useAccounts } from '../context/AccountsContext';
|
import { useAccounts } from '../context/AccountsContext';
|
||||||
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
||||||
import { usePrevious } from '../hooks/usePrevious';
|
import { EIP155_SIGNING_METHODS } from '../utils/wallet-connect/EIP155Data';
|
||||||
|
import { COSMOS_METHODS } from '../utils/wallet-connect/COSMOSData';
|
||||||
|
|
||||||
const Accounts = ({
|
const Accounts = ({
|
||||||
network,
|
network,
|
||||||
@ -22,9 +24,7 @@ const Accounts = ({
|
|||||||
const navigation =
|
const navigation =
|
||||||
useNavigation<NativeStackNavigationProp<StackParamsList>>();
|
useNavigation<NativeStackNavigationProp<StackParamsList>>();
|
||||||
|
|
||||||
const { accounts, setAccounts } = useAccounts();
|
const { accounts, setAccounts, networksData, currentChainId } = useAccounts();
|
||||||
const prevEthAccountsRef = usePrevious(accounts.ethAccounts);
|
|
||||||
const prevCosmosAccountsRef = usePrevious(accounts.cosmosAccounts);
|
|
||||||
const [expanded, setExpanded] = useState(false);
|
const [expanded, setExpanded] = useState(false);
|
||||||
const [isAccountCreating, setIsAccountCreating] = useState(false);
|
const [isAccountCreating, setIsAccountCreating] = useState(false);
|
||||||
const [hdDialog, setHdDialog] = useState(false);
|
const [hdDialog, setHdDialog] = useState(false);
|
||||||
@ -59,40 +59,76 @@ const Accounts = ({
|
|||||||
|
|
||||||
for (const topic in sessions) {
|
for (const topic in sessions) {
|
||||||
const session = sessions[topic];
|
const session = sessions[topic];
|
||||||
const requiredNamespaces = session.requiredNamespaces;
|
const combinedNamespaces = mergeWith(
|
||||||
const namespaces = session.namespaces;
|
session.requiredNamespaces,
|
||||||
|
session.optionalNamespaces,
|
||||||
|
(obj, src) =>
|
||||||
|
Array.isArray(obj) && Array.isArray(src)
|
||||||
|
? [...src, ...obj]
|
||||||
|
: undefined,
|
||||||
|
);
|
||||||
|
|
||||||
// Check if EIP155 namespace exists and Ethereum accounts have changed
|
const currentNetwork = networksData.find(
|
||||||
if (
|
networkData => networkData.chainId === currentChainId,
|
||||||
namespaces.hasOwnProperty('eip155') &&
|
);
|
||||||
prevEthAccountsRef !== accounts.ethAccounts
|
|
||||||
) {
|
let updatedNamespaces;
|
||||||
// Iterate through each chain ID in required EIP155 namespaces
|
switch (currentNetwork?.networkType) {
|
||||||
requiredNamespaces.eip155.chains?.forEach(chainId => {
|
case 'eth':
|
||||||
// Update Ethereum accounts in namespaces with chain prefix
|
updatedNamespaces = {
|
||||||
namespaces.eip155.accounts = accounts.ethAccounts.map(
|
eip155: {
|
||||||
ethAccount => `${chainId}:${ethAccount.address}`,
|
chains: [currentNetwork.chainId],
|
||||||
);
|
// TODO: Debug optional namespace methods and events being required for approval
|
||||||
});
|
methods: [
|
||||||
// update session with modified namespace
|
...Object.values(EIP155_SIGNING_METHODS),
|
||||||
await web3wallet!.updateSession({ topic, namespaces });
|
...(combinedNamespaces.eip155?.methods ?? []),
|
||||||
|
],
|
||||||
|
events: [...(combinedNamespaces.eip155?.events ?? [])],
|
||||||
|
accounts: accounts.ethAccounts.map(ethAccount => {
|
||||||
|
return `${currentChainId}:${ethAccount.address}`;
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
cosmos: {
|
||||||
|
chains: [],
|
||||||
|
methods: [],
|
||||||
|
events: [],
|
||||||
|
accounts: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case 'cosmos':
|
||||||
|
updatedNamespaces = {
|
||||||
|
cosmos: {
|
||||||
|
chains: [currentNetwork.chainId],
|
||||||
|
methods: [
|
||||||
|
...Object.values(COSMOS_METHODS),
|
||||||
|
...(combinedNamespaces.cosmos?.methods ?? []),
|
||||||
|
],
|
||||||
|
events: [...(combinedNamespaces.cosmos?.events ?? [])],
|
||||||
|
accounts: accounts.cosmosAccounts.map(cosmosAccount => {
|
||||||
|
return `${currentChainId}:${cosmosAccount.address}`;
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
eip155: {
|
||||||
|
chains: [],
|
||||||
|
methods: [],
|
||||||
|
events: [],
|
||||||
|
accounts: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if Cosmos namespace exists and Cosmos accounts have changed
|
if (!updatedNamespaces) {
|
||||||
if (
|
return;
|
||||||
namespaces.hasOwnProperty('cosmos') &&
|
|
||||||
prevCosmosAccountsRef !== accounts.cosmosAccounts
|
|
||||||
) {
|
|
||||||
// Iterate through each chain ID in required Cosmos namespaces
|
|
||||||
requiredNamespaces?.cosmos.chains?.forEach(chainId => {
|
|
||||||
// Iterate through each chain ID in required Cosmos namespaces
|
|
||||||
namespaces.cosmos.accounts = accounts.cosmosAccounts.map(
|
|
||||||
cosmosAccount => `${chainId}:${cosmosAccount.address}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
// update session with modified namespace
|
|
||||||
await web3wallet!.updateSession({ topic, namespaces });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await web3wallet!.updateSession({
|
||||||
|
topic,
|
||||||
|
namespaces: updatedNamespaces,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Call the updateSessions function when the 'accounts' dependency changes
|
// Call the updateSessions function when the 'accounts' dependency changes
|
||||||
|
@ -11,8 +11,8 @@ import styles from '../styles/stylesheet';
|
|||||||
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
||||||
import { useAccounts } from '../context/AccountsContext';
|
import { useAccounts } from '../context/AccountsContext';
|
||||||
import { useWalletConnect } from '../context/WalletConnectContext';
|
import { useWalletConnect } from '../context/WalletConnectContext';
|
||||||
import { EIP155_CHAINS } from '../utils/wallet-connect/EIP155Data';
|
import { EIP155_SIGNING_METHODS } from '../utils/wallet-connect/EIP155Data';
|
||||||
import { COSMOS_CHAINS } from '../utils/wallet-connect/COSMOSData';
|
import { COSMOS_METHODS } from '../utils/wallet-connect/COSMOSData';
|
||||||
|
|
||||||
const PairingModal = ({
|
const PairingModal = ({
|
||||||
visible,
|
visible,
|
||||||
@ -21,7 +21,8 @@ const PairingModal = ({
|
|||||||
setModalVisible,
|
setModalVisible,
|
||||||
setToastVisible,
|
setToastVisible,
|
||||||
}: PairingModalProps) => {
|
}: PairingModalProps) => {
|
||||||
const { accounts, currentIndex } = useAccounts();
|
const { accounts, networksData, currentChainId, currentIndex } =
|
||||||
|
useAccounts();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const dappName = currentProposal?.params?.proposer?.metadata.name;
|
const dappName = currentProposal?.params?.proposer?.metadata.name;
|
||||||
@ -57,6 +58,7 @@ const PairingModal = ({
|
|||||||
(obj, src) =>
|
(obj, src) =>
|
||||||
Array.isArray(obj) && Array.isArray(src) ? [...src, ...obj] : undefined,
|
Array.isArray(obj) && Array.isArray(src) ? [...src, ...obj] : undefined,
|
||||||
);
|
);
|
||||||
|
|
||||||
Object.keys(combinedNamespaces).forEach(key => {
|
Object.keys(combinedNamespaces).forEach(key => {
|
||||||
const { methods, events, chains } = combinedNamespaces[key];
|
const { methods, events, chains } = combinedNamespaces[key];
|
||||||
|
|
||||||
@ -79,12 +81,6 @@ const PairingModal = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eip155
|
|
||||||
const eip155Chains = Object.keys(EIP155_CHAINS);
|
|
||||||
|
|
||||||
// cosmos
|
|
||||||
const cosmosChains = Object.keys(COSMOS_CHAINS);
|
|
||||||
|
|
||||||
// Set selected account as the first account in supported namespaces
|
// Set selected account as the first account in supported namespaces
|
||||||
const sortedAccounts = Object.entries(accounts).reduce(
|
const sortedAccounts = Object.entries(accounts).reduce(
|
||||||
(acc: AccountsState, [key, value]) => {
|
(acc: AccountsState, [key, value]) => {
|
||||||
@ -105,49 +101,67 @@ const PairingModal = ({
|
|||||||
{ ethAccounts: [], cosmosAccounts: [] },
|
{ ethAccounts: [], cosmosAccounts: [] },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const currentNetwork = networksData.find(
|
||||||
|
networkData => networkData.chainId === currentChainId,
|
||||||
|
);
|
||||||
|
|
||||||
const { optionalNamespaces, requiredNamespaces } = currentProposal.params;
|
const { optionalNamespaces, requiredNamespaces } = currentProposal.params;
|
||||||
|
|
||||||
return {
|
// TODO: Check with other dApps
|
||||||
eip155: {
|
switch (currentNetwork?.networkType) {
|
||||||
chains: eip155Chains,
|
case 'eth':
|
||||||
// TODO: Debug optional namespace methods and events being required for approval
|
return {
|
||||||
methods: [
|
eip155: {
|
||||||
...(optionalNamespaces.eip155?.methods ?? []),
|
chains: [currentNetwork.chainId],
|
||||||
...(requiredNamespaces.eip155?.methods ?? []),
|
// TODO: Debug optional namespace methods and events being required for approval
|
||||||
],
|
methods: [
|
||||||
events: [
|
...Object.values(EIP155_SIGNING_METHODS),
|
||||||
...(optionalNamespaces.eip155?.events ?? []),
|
...(optionalNamespaces.eip155?.methods ?? []),
|
||||||
...(requiredNamespaces.eip155?.events ?? []),
|
...(requiredNamespaces.eip155?.methods ?? []),
|
||||||
],
|
],
|
||||||
|
events: [
|
||||||
accounts: eip155Chains
|
...(optionalNamespaces.eip155?.events ?? []),
|
||||||
.map(chain =>
|
...(requiredNamespaces.eip155?.events ?? []),
|
||||||
sortedAccounts.ethAccounts.map(
|
],
|
||||||
account => `${chain}:${account.address}`,
|
accounts: sortedAccounts.ethAccounts.map(ethAccount => {
|
||||||
),
|
return `${currentChainId}:${ethAccount.address}`;
|
||||||
)
|
}),
|
||||||
.flat(),
|
},
|
||||||
},
|
cosmos: {
|
||||||
cosmos: {
|
chains: [],
|
||||||
chains: cosmosChains,
|
methods: [],
|
||||||
methods: [
|
events: [],
|
||||||
...(optionalNamespaces.cosmos?.methods ?? []),
|
accounts: [],
|
||||||
...(requiredNamespaces.cosmos?.methods ?? []),
|
},
|
||||||
],
|
};
|
||||||
events: [
|
case 'cosmos':
|
||||||
...(optionalNamespaces.cosmos?.events ?? []),
|
return {
|
||||||
...(requiredNamespaces.cosmos?.events ?? []),
|
cosmos: {
|
||||||
],
|
chains: [currentNetwork.chainId],
|
||||||
accounts: cosmosChains
|
methods: [
|
||||||
.map(chain =>
|
...Object.values(COSMOS_METHODS),
|
||||||
sortedAccounts.cosmosAccounts.map(
|
...(optionalNamespaces.cosmos?.methods ?? []),
|
||||||
account => `${chain}:${account.address}`,
|
...(requiredNamespaces.cosmos?.methods ?? []),
|
||||||
),
|
],
|
||||||
)
|
events: [
|
||||||
.flat(),
|
...(optionalNamespaces.cosmos?.events ?? []),
|
||||||
},
|
...(requiredNamespaces.cosmos?.events ?? []),
|
||||||
};
|
],
|
||||||
}, [currentIndex, accounts, currentProposal]);
|
accounts: sortedAccounts.cosmosAccounts.map(cosmosAccount => {
|
||||||
|
return `${currentChainId}:${cosmosAccount.address}`;
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
eip155: {
|
||||||
|
chains: [],
|
||||||
|
methods: [],
|
||||||
|
events: [],
|
||||||
|
accounts: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}, [accounts, currentProposal, networksData, currentChainId, currentIndex]);
|
||||||
|
|
||||||
const namespaces = useMemo(() => {
|
const namespaces = useMemo(() => {
|
||||||
return (
|
return (
|
||||||
@ -229,29 +243,39 @@ const PairingModal = ({
|
|||||||
<Text variant="bodyMedium">{url}</Text>
|
<Text variant="bodyMedium">{url}</Text>
|
||||||
<View style={styles.marginVertical8} />
|
<View style={styles.marginVertical8} />
|
||||||
<Text variant="titleMedium">Connect to this site?</Text>
|
<Text variant="titleMedium">Connect to this site?</Text>
|
||||||
<Text variant="titleMedium">Chains:</Text>
|
|
||||||
{walletConnectData.walletConnectChains.map(chain => (
|
|
||||||
<Text style={styles.centerText} key={chain}>
|
|
||||||
{chain}
|
|
||||||
</Text>
|
|
||||||
))}
|
|
||||||
<View style={styles.marginVertical8}>
|
|
||||||
<Text variant="titleMedium">Methods Requested:</Text>
|
|
||||||
{walletConnectData.walletConnectMethods.map(method => (
|
|
||||||
<Text style={styles.centerText} key={method}>
|
|
||||||
{method}
|
|
||||||
</Text>
|
|
||||||
))}
|
|
||||||
</View>
|
|
||||||
|
|
||||||
<View style={styles.marginVertical8}>
|
{walletConnectData.walletConnectMethods.length > 0 && (
|
||||||
<Text variant="titleMedium">Events Requested:</Text>
|
<View>
|
||||||
{walletConnectData.walletConnectEvents.map(event => (
|
<Text variant="titleMedium">Chains:</Text>
|
||||||
<Text style={styles.centerText} key={event}>
|
{walletConnectData.walletConnectChains.map(chain => (
|
||||||
{event}
|
<Text style={styles.centerText} key={chain}>
|
||||||
</Text>
|
{chain}
|
||||||
))}
|
</Text>
|
||||||
</View>
|
))}
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{walletConnectData.walletConnectMethods.length > 0 && (
|
||||||
|
<View style={styles.marginVertical8}>
|
||||||
|
<Text variant="titleMedium">Methods Requested:</Text>
|
||||||
|
{walletConnectData.walletConnectMethods.map(method => (
|
||||||
|
<Text style={styles.centerText} key={method}>
|
||||||
|
{method}
|
||||||
|
</Text>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{walletConnectData.walletConnectEvents.length > 0 && (
|
||||||
|
<View style={styles.marginVertical8}>
|
||||||
|
<Text variant="titleMedium">Events Requested:</Text>
|
||||||
|
{walletConnectData.walletConnectEvents.map(event => (
|
||||||
|
<Text style={styles.centerText} key={event}>
|
||||||
|
{event}
|
||||||
|
</Text>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@ const AccountsContext = createContext<{
|
|||||||
setNetworksData: (networksDataArray: NetworksDataState[]) => void;
|
setNetworksData: (networksDataArray: NetworksDataState[]) => void;
|
||||||
networkType: string;
|
networkType: string;
|
||||||
setNetworkType: (networkType: string) => void;
|
setNetworkType: (networkType: string) => void;
|
||||||
|
currentChainId: string;
|
||||||
|
setCurrentChainId: (currentChainId: string) => void;
|
||||||
}>({
|
}>({
|
||||||
accounts: { ethAccounts: [], cosmosAccounts: [] },
|
accounts: { ethAccounts: [], cosmosAccounts: [] },
|
||||||
setAccounts: () => {},
|
setAccounts: () => {},
|
||||||
@ -22,6 +24,8 @@ const AccountsContext = createContext<{
|
|||||||
setNetworksData: () => {},
|
setNetworksData: () => {},
|
||||||
networkType: '',
|
networkType: '',
|
||||||
setNetworkType: () => {},
|
setNetworkType: () => {},
|
||||||
|
currentChainId: '',
|
||||||
|
setCurrentChainId: () => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
const useAccounts = () => {
|
const useAccounts = () => {
|
||||||
@ -34,13 +38,12 @@ const AccountsProvider = ({ children }: { children: any }) => {
|
|||||||
ethAccounts: [],
|
ethAccounts: [],
|
||||||
cosmosAccounts: [],
|
cosmosAccounts: [],
|
||||||
});
|
});
|
||||||
// TODO: Replace chainId values with testnet chainIds
|
|
||||||
const [networksData, setNetworksData] = useState<NetworksDataState[]>([
|
const [networksData, setNetworksData] = useState<NetworksDataState[]>([
|
||||||
{
|
{
|
||||||
chainId: 'eip155:11155111',
|
chainId: 'eip155:1',
|
||||||
networkName: EIP155_CHAINS['eip155:11155111'].name,
|
networkName: EIP155_CHAINS['eip155:1'].name,
|
||||||
networkType: 'eth',
|
networkType: 'eth',
|
||||||
rpcUrl: EIP155_CHAINS['eip155:11155111'].rpc,
|
rpcUrl: EIP155_CHAINS['eip155:1'].rpc,
|
||||||
currencySymbol: 'ETH',
|
currencySymbol: 'ETH',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -48,13 +51,17 @@ const AccountsProvider = ({ children }: { children: any }) => {
|
|||||||
networkName: COSMOS_TESTNET_CHAINS['cosmos:theta-testnet-001'].name,
|
networkName: COSMOS_TESTNET_CHAINS['cosmos:theta-testnet-001'].name,
|
||||||
networkType: 'cosmos',
|
networkType: 'cosmos',
|
||||||
rpcUrl: COSMOS_TESTNET_CHAINS['cosmos:theta-testnet-001'].rpc,
|
rpcUrl: COSMOS_TESTNET_CHAINS['cosmos:theta-testnet-001'].rpc,
|
||||||
nativeDenom: 'ATOM',
|
nativeDenom: 'uatom',
|
||||||
addressPrefix: 'cosmos',
|
addressPrefix: 'cosmos',
|
||||||
coinType: '118',
|
coinType: '118',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const [currentIndex, setCurrentIndex] = useState<number>(0);
|
const [currentIndex, setCurrentIndex] = useState<number>(0);
|
||||||
const [networkType, setNetworkType] = useState<string>('eth');
|
const [networkType, setNetworkType] = useState<string>('eth');
|
||||||
|
const [currentChainId, setCurrentChainId] = useState<string>(
|
||||||
|
networksData[0].chainId,
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AccountsContext.Provider
|
<AccountsContext.Provider
|
||||||
value={{
|
value={{
|
||||||
@ -66,6 +73,8 @@ const AccountsProvider = ({ children }: { children: any }) => {
|
|||||||
setNetworksData,
|
setNetworksData,
|
||||||
networkType,
|
networkType,
|
||||||
setNetworkType,
|
setNetworkType,
|
||||||
|
currentChainId,
|
||||||
|
setCurrentChainId,
|
||||||
}}>
|
}}>
|
||||||
{children}
|
{children}
|
||||||
</AccountsContext.Provider>
|
</AccountsContext.Provider>
|
||||||
|
@ -28,15 +28,16 @@ const AddNetwork = () => {
|
|||||||
|
|
||||||
const [networkType, setNetworkType] = useState<string>('eth');
|
const [networkType, setNetworkType] = useState<string>('eth');
|
||||||
|
|
||||||
// TODO: Update session when new network is added with updated addresses
|
|
||||||
const updateNetworkType = (newNetworkType: string) => {
|
const updateNetworkType = (newNetworkType: string) => {
|
||||||
setNetworkType(newNetworkType);
|
setNetworkType(newNetworkType);
|
||||||
};
|
};
|
||||||
|
|
||||||
const submit = useCallback(
|
const submit = useCallback(
|
||||||
async (data: NetworksDataState) => {
|
async (data: NetworksDataState) => {
|
||||||
|
const namespace = networkType === 'eth' ? 'eip155:' : 'cosmos:';
|
||||||
const updatedData = {
|
const updatedData = {
|
||||||
...data,
|
...data,
|
||||||
|
chainId: `${namespace}${data.chainId}`,
|
||||||
networkType,
|
networkType,
|
||||||
};
|
};
|
||||||
setNetworksData([...networksData, updatedData]);
|
setNetworksData([...networksData, updatedData]);
|
||||||
@ -45,9 +46,10 @@ const AddNetwork = () => {
|
|||||||
},
|
},
|
||||||
[navigation, networkType, networksData, setNetworksData],
|
[navigation, networkType, networksData, setNetworksData],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
// TODO: get form data from json file
|
// TODO: get form data from json file
|
||||||
<ScrollView contentContainerStyle={styles.addNetwork}>
|
<ScrollView contentContainerStyle={styles.signPage}>
|
||||||
<Controller
|
<Controller
|
||||||
control={control}
|
control={control}
|
||||||
defaultValue=""
|
defaultValue=""
|
||||||
|
@ -27,8 +27,6 @@ import {
|
|||||||
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils';
|
||||||
import DataBox from '../components/DataBox';
|
import DataBox from '../components/DataBox';
|
||||||
import { getPathKey } from '../utils/misc';
|
import { getPathKey } from '../utils/misc';
|
||||||
import { COSMOS_TESTNET_CHAINS } from '../utils/wallet-connect/COSMOSData';
|
|
||||||
import { COSMOS_DENOM } from '../utils/constants';
|
|
||||||
import { useAccounts } from '../context/AccountsContext';
|
import { useAccounts } from '../context/AccountsContext';
|
||||||
|
|
||||||
type SignRequestProps = NativeStackScreenProps<
|
type SignRequestProps = NativeStackScreenProps<
|
||||||
@ -114,6 +112,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await approveWalletConnectRequest(
|
const response = await approveWalletConnectRequest(
|
||||||
|
networksData,
|
||||||
requestEvent,
|
requestEvent,
|
||||||
account,
|
account,
|
||||||
network,
|
network,
|
||||||
@ -149,7 +148,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
} else {
|
} else {
|
||||||
const cosmosBalance = await cosmosStargateClient?.getBalance(
|
const cosmosBalance = await cosmosStargateClient?.getBalance(
|
||||||
account.address,
|
account.address,
|
||||||
COSMOS_DENOM,
|
requestedChain!.nativeDenom!.toLowerCase(),
|
||||||
);
|
);
|
||||||
|
|
||||||
setBalance(cosmosBalance?.amount!);
|
setBalance(cosmosBalance?.amount!);
|
||||||
@ -159,7 +158,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
if (account) {
|
if (account) {
|
||||||
getAccountBalance(account);
|
getAccountBalance(account);
|
||||||
}
|
}
|
||||||
}, [account, provider, network, cosmosStargateClient]);
|
}, [account, provider, network, cosmosStargateClient, requestedChain]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
navigation.setOptions({
|
navigation.setOptions({
|
||||||
@ -204,7 +203,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const client = await SigningStargateClient.connectWithSigner(
|
const client = await SigningStargateClient.connectWithSigner(
|
||||||
COSMOS_TESTNET_CHAINS[chainId as string].rpc,
|
requestedChain?.rpcUrl!,
|
||||||
sender,
|
sender,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -240,9 +239,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
<DataBox
|
<DataBox
|
||||||
label={`Balance ${
|
label={`Balance ${requestedChain!.nativeDenom!}`}
|
||||||
network === 'eth' ? '(Wei)' : `(${COSMOS_DENOM})`
|
|
||||||
}`}
|
|
||||||
data={
|
data={
|
||||||
balance === '' || balance === undefined
|
balance === '' || balance === undefined
|
||||||
? 'Loading balance...'
|
? 'Loading balance...'
|
||||||
@ -253,17 +250,13 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
<View style={styles.approveTransaction}>
|
<View style={styles.approveTransaction}>
|
||||||
<DataBox label="To" data={transaction.to!} />
|
<DataBox label="To" data={transaction.to!} />
|
||||||
<DataBox
|
<DataBox
|
||||||
label={`Amount ${
|
label={`Amount ${requestedChain!.nativeDenom!}`}
|
||||||
network === 'eth' ? '(Wei)' : `(${COSMOS_DENOM})`
|
|
||||||
}`}
|
|
||||||
data={BigNumber.from(
|
data={BigNumber.from(
|
||||||
transaction.value?.toString(),
|
transaction.value?.toString(),
|
||||||
).toString()}
|
).toString()}
|
||||||
/>
|
/>
|
||||||
<DataBox
|
<DataBox
|
||||||
label={`Gas Fees ${
|
label={`Gas Fees ${requestedChain!.nativeDenom!}`}
|
||||||
network === 'eth' ? '(Wei)' : `(${COSMOS_DENOM})`
|
|
||||||
}`}
|
|
||||||
data={gasFees!}
|
data={gasFees!}
|
||||||
/>
|
/>
|
||||||
{network === 'eth' && (
|
{network === 'eth' && (
|
||||||
|
@ -36,6 +36,8 @@ const HomeScreen = () => {
|
|||||||
networkType,
|
networkType,
|
||||||
setNetworkType,
|
setNetworkType,
|
||||||
networksData,
|
networksData,
|
||||||
|
currentChainId,
|
||||||
|
setCurrentChainId,
|
||||||
} = useAccounts();
|
} = useAccounts();
|
||||||
const { setActiveSessions } = useWalletConnect();
|
const { setActiveSessions } = useWalletConnect();
|
||||||
|
|
||||||
@ -60,9 +62,6 @@ const HomeScreen = () => {
|
|||||||
const [isWalletCreated, setIsWalletCreated] = useState<boolean>(false);
|
const [isWalletCreated, setIsWalletCreated] = useState<boolean>(false);
|
||||||
const [isWalletCreating, setIsWalletCreating] = useState<boolean>(false);
|
const [isWalletCreating, setIsWalletCreating] = useState<boolean>(false);
|
||||||
const [walletDialog, setWalletDialog] = useState<boolean>(false);
|
const [walletDialog, setWalletDialog] = useState<boolean>(false);
|
||||||
const [currentChainId, setCurrentChainId] = useState<string>(
|
|
||||||
networksData[0].chainId,
|
|
||||||
);
|
|
||||||
const [resetWalletDialog, setResetWalletDialog] = useState<boolean>(false);
|
const [resetWalletDialog, setResetWalletDialog] = useState<boolean>(false);
|
||||||
const [isAccountsFetched, setIsAccountsFetched] = useState<boolean>(false);
|
const [isAccountsFetched, setIsAccountsFetched] = useState<boolean>(false);
|
||||||
const [phrase, setPhrase] = useState('');
|
const [phrase, setPhrase] = useState('');
|
||||||
|
@ -1 +0,0 @@
|
|||||||
export const COSMOS_DENOM = 'uatom';
|
|
@ -140,11 +140,5 @@ export const EIP155_CHAINS = {
|
|||||||
*/
|
*/
|
||||||
export const EIP155_SIGNING_METHODS = {
|
export const EIP155_SIGNING_METHODS = {
|
||||||
PERSONAL_SIGN: 'personal_sign',
|
PERSONAL_SIGN: 'personal_sign',
|
||||||
ETH_SIGN: 'eth_sign',
|
|
||||||
ETH_SIGN_TRANSACTION: 'eth_signTransaction',
|
|
||||||
ETH_SIGN_TYPED_DATA: 'eth_signTypedData',
|
|
||||||
ETH_SIGN_TYPED_DATA_V3: 'eth_signTypedData_v3',
|
|
||||||
ETH_SIGN_TYPED_DATA_V4: 'eth_signTypedData_v4',
|
|
||||||
ETH_SEND_RAW_TRANSACTION: 'eth_sendRawTransaction',
|
|
||||||
ETH_SEND_TRANSACTION: 'eth_sendTransaction',
|
ETH_SEND_TRANSACTION: 'eth_sendTransaction',
|
||||||
};
|
};
|
||||||
|
@ -13,12 +13,12 @@ import {
|
|||||||
|
|
||||||
import { EIP155_SIGNING_METHODS } from './EIP155Data';
|
import { EIP155_SIGNING_METHODS } from './EIP155Data';
|
||||||
import { signDirectMessage, signEthMessage } from '../sign-message';
|
import { signDirectMessage, signEthMessage } from '../sign-message';
|
||||||
import { Account } from '../../types';
|
import { Account, NetworksDataState } from '../../types';
|
||||||
import { getMnemonic, getPathKey } from '../misc';
|
import { getMnemonic, getPathKey } from '../misc';
|
||||||
import { getCosmosAccounts } from '../accounts';
|
import { getCosmosAccounts } from '../accounts';
|
||||||
import { COSMOS_DENOM } from '../constants';
|
|
||||||
|
|
||||||
export async function approveWalletConnectRequest(
|
export async function approveWalletConnectRequest(
|
||||||
|
networksData: NetworksDataState[],
|
||||||
requestEvent: SignClientTypes.EventArguments['session_request'],
|
requestEvent: SignClientTypes.EventArguments['session_request'],
|
||||||
account: Account,
|
account: Account,
|
||||||
network: string,
|
network: string,
|
||||||
@ -28,10 +28,15 @@ export async function approveWalletConnectRequest(
|
|||||||
const { params, id } = requestEvent;
|
const { params, id } = requestEvent;
|
||||||
const { request } = params;
|
const { request } = params;
|
||||||
|
|
||||||
|
const chainId = requestEvent.params.chainId;
|
||||||
|
const requestedChain = networksData.find(
|
||||||
|
networkData => networkData.chainId === chainId,
|
||||||
|
);
|
||||||
|
|
||||||
const path = (await getPathKey(network, account.counterId)).path;
|
const path = (await getPathKey(network, account.counterId)).path;
|
||||||
const mnemonic = await getMnemonic();
|
const mnemonic = await getMnemonic();
|
||||||
const cosmosAccount = await getCosmosAccounts(mnemonic, path);
|
const cosmosAccount = await getCosmosAccounts(mnemonic, path);
|
||||||
const address = cosmosAccount.data.address;
|
const address = account.address;
|
||||||
|
|
||||||
switch (request.method) {
|
switch (request.method) {
|
||||||
case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
|
case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
|
||||||
@ -101,7 +106,7 @@ export async function approveWalletConnectRequest(
|
|||||||
});
|
});
|
||||||
|
|
||||||
case 'cosmos_sendTokens':
|
case 'cosmos_sendTokens':
|
||||||
const amount = coins(request.params[0].value, COSMOS_DENOM);
|
const amount = coins(request.params[0].value, requestedChain!.chainId);
|
||||||
const gasPrice = GasPrice.fromString(
|
const gasPrice = GasPrice.fromString(
|
||||||
request.params[0].gasPrice.toString(),
|
request.params[0].gasPrice.toString(),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user