Skip to content

Commit

Permalink
fix: Correct hdPath for TRON at All Networks (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
devkudasov authored Dec 24, 2024
1 parent b298b64 commit e1607ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
18 changes: 18 additions & 0 deletions src/models/wallet/wallet.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class WalletModel implements IWalletModel {
return this.model.deviceId;
}

// FIXME: Remove it when fully moved to getPath method
get path() {
if (this.isSupportTron && Provider.selectedProvider.isTron) {
switch (this.type) {
Expand All @@ -133,6 +134,23 @@ export class WalletModel implements IWalletModel {
return this.model.path;
}

getPath(provider = Provider.selectedProvider) {
if (this.isSupportTron && provider.isTron) {
switch (this.type) {
case WalletType.hot:
case WalletType.sss:
case WalletType.mnemonic:
return this.model?.path?.replace(ETH_COIN_TYPE, TRON_COIN_TYPE);
// TODO: add support for ledger and keystone
case WalletType.ledgerBt:
case WalletType.keystone:
default:
return '';
}
}
return this.model.path;
}

get rootAddress() {
return this.model.rootAddress;
}
Expand Down
52 changes: 32 additions & 20 deletions src/services/eth-network/eth-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {AddressUtils} from '@app/helpers/address-utils';
import {getRpcProvider} from '@app/helpers/get-rpc-provider';
import {EstimationVariant} from '@app/models/fee';
import {Provider, ProviderModel} from '@app/models/provider';
import {IWalletModel} from '@app/models/wallet';
import {WalletModel} from '@app/models/wallet';
import {getDefaultChainId} from '@app/network';
import {Balance} from '@app/services/balance';
import {getERC1155TransferData} from '@app/services/eth-network/erc1155';
Expand Down Expand Up @@ -335,18 +335,21 @@ export class EthNetwork {
async transferTransaction(
estimate: CalculatedFees,
transport: ProviderInterface,
wallet: IWalletModel,
wallet: WalletModel,
to: string,
value: Balance,
provider = Provider.selectedProvider,
) {
try {
if (provider.isTron) {
const signedTx = await transport.signTransaction(wallet.path!, {
from: wallet.tronAddress,
to: AddressUtils.toTron(to),
value: value.toParsedBalanceNumber(),
});
const signedTx = await transport.signTransaction(
wallet.getPath(provider)!,
{
from: wallet.tronAddress,
to: AddressUtils.toTron(to),
value: value.toParsedBalanceNumber(),
},
);
return await TronNetwork.broadcastTransaction(signedTx, provider);
}
const transaction = await EthNetwork.populateTransaction(
Expand All @@ -359,7 +362,7 @@ export class EthNetwork {
provider,
);
const signedTx = await transport.signTransaction(
wallet.path!,
wallet.getPath(provider)!,
transaction,
);

Expand Down Expand Up @@ -416,20 +419,23 @@ export class EthNetwork {
async transferERC20(
estimate: CalculatedFees,
transport: ProviderInterface,
from: IWalletModel,
from: WalletModel,
to: string,
amount: Balance,
contractAddress: string,
provider = Provider.selectedProvider,
) {
try {
if (provider.isTron) {
const signedTx = await transport.signTransaction(from.path!, {
from: from.tronAddress,
to: AddressUtils.toTron(to),
data: contractAddress,
value: amount.toParsedBalanceNumber(),
});
const signedTx = await transport.signTransaction(
from.getPath(provider)!,
{
from: from.tronAddress,
to: AddressUtils.toTron(to),
data: contractAddress,
value: amount.toParsedBalanceNumber(),
},
);

return await TronNetwork.broadcastTransaction(signedTx, provider);
} else {
Expand All @@ -445,7 +451,7 @@ export class EthNetwork {
provider,
);
const signedTx = await transport.signTransaction(
from.path!,
from.getPath(provider)!,
unsignedTx,
);

Expand All @@ -466,7 +472,7 @@ export class EthNetwork {
async transferERC721(
estimate: CalculatedFees,
transport: ProviderInterface,
from: IWalletModel,
from: WalletModel,
to: string,
tokenId: number,
contractAddress: string,
Expand All @@ -485,7 +491,10 @@ export class EthNetwork {
provider,
);

const signedTx = await transport.signTransaction(from.path!, unsignedTx);
const signedTx = await transport.signTransaction(
from.getPath(provider)!,
unsignedTx,
);

return await EthNetwork.sendTransaction(signedTx, provider);
} catch (error) {
Expand All @@ -503,7 +512,7 @@ export class EthNetwork {
async transferERC1155(
estimate: CalculatedFees,
transport: ProviderInterface,
from: IWalletModel,
from: WalletModel,
to: string,
tokenId: number,
contractAddress: string,
Expand All @@ -522,7 +531,10 @@ export class EthNetwork {
provider,
);

const signedTx = await transport.signTransaction(from.path!, unsignedTx);
const signedTx = await transport.signTransaction(
from.getPath(provider)!,
unsignedTx,
);

return await EthNetwork.sendTransaction(signedTx, provider);
} catch (error) {
Expand Down

0 comments on commit e1607ea

Please sign in to comment.