Skip to content
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

fix(HW-593): json rpc sign for different networks #2025

Merged
merged 3 commits into from
Aug 14, 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
66 changes: 37 additions & 29 deletions src/components/json-rpc-transaction-info.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useCallback, useMemo, useState} from 'react';

import {ActivityIndicator, ScrollView, View} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';

import {Color} from '@app/colors';
import {
Expand All @@ -19,7 +20,7 @@ import {awaitForFee} from '@app/helpers/await-for-fee';
import {useTypedNavigation} from '@app/hooks';
import {useEffectAsync} from '@app/hooks/use-effect-async';
import {I18N} from '@app/i18n';
import {Fee} from '@app/models/fee';
import {EstimationVariant, Fee} from '@app/models/fee';
import {Provider} from '@app/models/provider';
import {JsonRpcSignPopupStackParamList} from '@app/route-types';
import {EthNetwork} from '@app/services';
Expand Down Expand Up @@ -69,11 +70,10 @@ export const JsonRpcTransactionInfo = ({
);

const provider = useMemo(() => {
const _provider = Provider.getByEthChainId(
chainId || tx?.chainId || app.provider.ethChainId,
return Provider.getByEthChainId(
tx?.chainId ?? chainId ?? app.provider.ethChainId,
);
return _provider!;
}, [chainId]);
}, [chainId, tx]);

const url = useMemo(() => getHostnameFromUrl(metadata?.url), [metadata]);

Expand All @@ -82,21 +82,29 @@ export const JsonRpcTransactionInfo = ({
return Balance.Empty;
}

return new Balance(tx.value);
}, [tx]);
return new Balance(tx.value, provider?.decimals, provider?.denom);
}, [tx, provider]);

const calculateFee = useCallback(async () => {
if (!tx) {
return Balance.Empty;
}

try {
const data = await EthNetwork.estimate({
from: tx.from!,
to: tx.to!,
value: new Balance(tx.value! || Balance.Empty),
data: tx.data,
});
const data = await EthNetwork.estimate(
{
from: tx.from!,
to: tx.to!,
value: new Balance(
tx.value || Balance.Empty,
provider?.decimals,
provider?.denom,
),
data: tx.data,
},
EstimationVariant.average,
provider,
);
setFee(new Fee(data));
return data.expectedFee;
} catch {
Expand Down Expand Up @@ -169,12 +177,17 @@ export const JsonRpcTransactionInfo = ({
fee,
from: tx.from!,
to: tx.to!,
value: new Balance(tx.value! || Balance.Empty),
value: new Balance(
tx.value! || Balance.Empty,
provider?.decimals,
provider?.denom,
),
data: tx.data,
chainId: provider?.ethChainId ? String(provider.ethChainId) : undefined,
});
setFee(result);
}
}, [navigation, tx, fee]);
}, [navigation, tx, fee, provider]);

return (
<View style={styles.container}>
Expand Down Expand Up @@ -260,11 +273,7 @@ export const JsonRpcTransactionInfo = ({
</DataView>
<DataView i18n={I18N.transactionInfoCryptocurrency}>
<Text variant={TextVariant.t11} color={Color.textBase1}>
<Text i18n={I18N.transactionConfirmationIslamicCoin} />{' '}
<Text
color={Color.textBase2}
i18n={I18N.transactionConfirmationISLM}
/>
{`${provider?.coinName} ${provider?.denom}`}
</Text>
</DataView>
{!!provider?.id && (
Expand All @@ -284,15 +293,14 @@ export const JsonRpcTransactionInfo = ({
<DataView i18n={I18N.transactionInfoNetworkFee}>
<First>
{isFeeLoading && <ActivityIndicator />}
<View style={styles.feeContainer}>
<Text
variant={TextVariant.t11}
color={Color.textGreen1}
onPress={onFeePress}>
{fee?.expectedFeeString}
</Text>
<Icon name={IconsName.tune} color={Color.textGreen1} />
</View>
<TouchableWithoutFeedback onPress={onFeePress}>
<View style={styles.feeContainer}>
<Text variant={TextVariant.t11} color={Color.textGreen1}>
{fee?.expectedFeeString}
</Text>
<Icon name={IconsName.tune} color={Color.textGreen1} />
</View>
</TouchableWithoutFeedback>
</First>
</DataView>
</ScrollView>
Expand Down
2 changes: 1 addition & 1 deletion src/event-actions/on-wallet-connect-sign-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function onWalletConnectSignTransaction(
const chainIdFromParams = event?.params?.chainId?.split?.(':')?.[1];

const result = await awaitForJsonRpcSign({
chainId: Number(chainId || chainIdFromParams),
chainId: Number(chainIdFromParams || chainId),
request: event.params?.request,
metadata: {
url: session.peer.metadata.url,
Expand Down
5 changes: 4 additions & 1 deletion src/screens/HomeStack/TransactionStack/fee-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {createTheme} from '@app/helpers';
import {useSumAmount, useTypedNavigation, useTypedRoute} from '@app/hooks';
import {I18N, getText} from '@app/i18n';
import {EstimationVariant, Fee} from '@app/models/fee';
import {Provider} from '@app/models/provider';
import {HomeStackParamList, HomeStackRoutes} from '@app/route-types';
import {EthNetwork} from '@app/services';
import {Balance} from '@app/services/balance';
Expand All @@ -38,7 +39,7 @@ type ResetValues = {

export const FeeSettingsScreen = observer(() => {
const navigation = useTypedNavigation<HomeStackParamList>();
const {fee, from, to, value, data, successEventName} = useTypedRoute<
const {fee, from, to, value, data, successEventName, chainId} = useTypedRoute<
HomeStackParamList,
HomeStackRoutes.FeeSettings
>().params;
Expand Down Expand Up @@ -81,6 +82,7 @@ export const FeeSettingsScreen = observer(() => {
new Balance(amountsMaxPriorityFee.amount).toWei(),
),
},
Provider.getByEthChainId(chainId!),
);

calculatedData && setExpectedFee(calculatedData.expectedFee);
Expand All @@ -95,6 +97,7 @@ export const FeeSettingsScreen = observer(() => {
data,
},
estimationType,
Provider.getByEthChainId(chainId!),
);

setResetValues({
Expand Down
44 changes: 35 additions & 9 deletions src/services/eth-network/eth-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export class EthNetwork {
static async customEstimate(
{from, to, value = Balance.Empty, data = '0x'}: TxEstimationParams,
{gasLimit, maxBaseFee, maxPriorityFee}: TxCustomEstimationParams,
provider = app.provider,
): Promise<CalculatedFees> {
try {
const rpcProvider = await getRpcProvider(app.provider);
const rpcProvider = await getRpcProvider(provider);
const block = await rpcProvider.getBlock('latest');
const estimateGasLimit = await rpcProvider.estimateGas({
from,
Expand All @@ -159,11 +160,25 @@ export class EthNetwork {
}

return {
gasLimit: new Balance(resultGasLimit),
maxBaseFee: new Balance(resultMaxBaseFee),
maxPriorityFee: new Balance(maxPriorityFee),
gasLimit: new Balance(
resultGasLimit,
provider.decimals,
provider.denom,
),
maxBaseFee: new Balance(
resultMaxBaseFee,
provider.decimals,
provider.denom,
),
maxPriorityFee: new Balance(
maxPriorityFee,
provider.decimals,
provider.denom,
),
expectedFee: new Balance(
resultGasLimit.mul(resultMaxBaseFee.add(maxPriorityFee)),
provider.decimals,
provider.denom,
),
};
} catch (error) {
Expand Down Expand Up @@ -191,9 +206,10 @@ export class EthNetwork {
static async estimate(
{from, to, value = Balance.Empty, data = '0x', minGas}: TxEstimationParams,
calculationType: EstimationVariant = EstimationVariant.average,
provider = app.provider,
): Promise<CalculatedFees> {
try {
const rpcProvider = await getRpcProvider(app.provider);
const rpcProvider = await getRpcProvider(provider);
const {maxFeePerGas, maxPriorityFeePerGas} =
await rpcProvider.getFeeData();
const block = await rpcProvider.getBlock('latest');
Expand Down Expand Up @@ -239,10 +255,20 @@ export class EthNetwork {
}

return {
gasLimit: new Balance(gasLimit).max(minGas),
maxBaseFee: new Balance(maxBaseFee),
maxPriorityFee: new Balance(priorityFee),
expectedFee: new Balance(gasLimit.mul(maxBaseFee.add(priorityFee))),
gasLimit: new Balance(gasLimit, provider.decimals, provider.denom).max(
minGas,
),
maxBaseFee: new Balance(maxBaseFee, provider.decimals, provider.denom),
maxPriorityFee: new Balance(
priorityFee,
provider.decimals,
provider.denom,
),
expectedFee: new Balance(
gasLimit.mul(maxBaseFee.add(priorityFee)),
provider.decimals,
provider.denom,
),
};
} catch (error) {
Logger.captureException(error, 'EthNetwork.estimateTransaction error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,6 @@ export const JsonRpcMethodsHandlers: Record<string, JsonRpcMethodHandler> = {
hexSignature = hexSignature.slice(0, 128);
}

Logger.log('account.base_account.pub_key', account.base_account.pub_key);

return {
signature: {
pub_key: account.base_account.pub_key,
Expand Down
1 change: 0 additions & 1 deletion src/services/provider-sss-initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export async function providerSssInitialize(
}

if (socialPrivateKey) {
// Logger.log('socialPrivateKey', socialPrivateKey);
const socialShareIndex = await getMetadataValueWrapped(
options.metadataUrl,
socialPrivateKey,
Expand Down
3 changes: 1 addition & 2 deletions src/services/provider-sss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ export async function onLoginGoogle() {
try {
authState = await getGoogleTokens();
} catch (err) {
// Logger.log('SSS_GOOGLE_ERROR', err);
Logger.log('SSS_GOOGLE_ERROR', err);
}
const authInfo = parseJwt(authState.idToken);

const verifier = RemoteConfig.get('sss_google_provider');

if (!verifier) {
// Logger.log('SSS_GOOGLE_ERROR', 'sss_google is not set');
throw new Error('sss_google is not set');
}

Expand Down
2 changes: 0 additions & 2 deletions src/services/push-notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ export class PushNotifications extends EventEmitter {
super();

messaging().onMessage(remoteMessage => {
// Logger.log('onMessage', remoteMessage);
app.emit(Events.onPushNotification, remoteMessage);
});

messaging().onNotificationOpenedApp(remoteMessage => {
// Logger.log('onNotificationOpenedApp', remoteMessage);
app.emit(Events.onPushNotification, remoteMessage);
});

Expand Down
22 changes: 14 additions & 8 deletions src/services/sign-json-rpc-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {DEBUG_VARS} from '@app/debug-vars';
import {getProviderInstanceForWallet, hideModal} from '@app/helpers';
import {getRpcProvider} from '@app/helpers/get-rpc-provider';
import {I18N, getText} from '@app/i18n';
import {EstimationVariant} from '@app/models/fee';
import {Provider} from '@app/models/provider';
import {Wallet} from '@app/models/wallet';
import {getDefaultNetwork} from '@app/network';
Expand Down Expand Up @@ -105,7 +106,6 @@ export class SignJsonRpcRequest {
}

const provider = Provider.getByEthChainId(chainId!) || app.provider;

const rpcProvider = provider
? await getRpcProvider(provider)
: getDefaultNetwork();
Expand Down Expand Up @@ -170,13 +170,19 @@ export class SignJsonRpcRequest {
const minGas = new Balance(signTransactionRequest.gasLimit ?? 0);

const {gasLimit, maxBaseFee, maxPriorityFee} =
await EthNetwork.estimate({
from: signTransactionRequest.from!,
to: signTransactionRequest.to!,
value: new Balance(signTransactionRequest.value! || Balance.Empty),
data: signTransactionRequest.data?.toString()!,
minGas,
});
await EthNetwork.estimate(
{
from: signTransactionRequest.from!,
to: signTransactionRequest.to!,
value: new Balance(
signTransactionRequest.value! || Balance.Empty,
),
data: signTransactionRequest.data?.toString()!,
minGas,
},
EstimationVariant.average,
provider,
);

const maxPriorityFeePerGasCalculated = maxPriorityFee.max(
signTransactionRequest.maxPriorityFeePerGas || 0,
Expand Down
Loading