Skip to content

Commit

Permalink
support ledger again
Browse files Browse the repository at this point in the history
  • Loading branch information
thal0x committed Jul 31, 2023
1 parent f7201a3 commit f231858
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/solve/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import {
getSigningCosmWasmClientForChainID,
getSigningStargateClientForChainID,
getStargateClientForChainID,
isLedger,
signAndBroadcastEvmos,
signAndBroadcastInjective,
} from "@/utils/utils";
import { EncodeObject, OfflineSigner, coin } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { useAssets } from "@/context/assets";
import { useChain } from "@cosmos-kit/react";
import { MsgTransfer } from "@injectivelabs/sdk-ts";
import { MsgTransfer as MsgTransferInjective } from "@injectivelabs/sdk-ts";
import { WalletClient } from "@cosmos-kit/core";
import { useSkipClient } from "./hooks";
import { SkipClient, MsgsRequest } from "./client";
import { trackRoute } from "@/analytics";
import { KeplrClient, KeplrExtensionWallet } from "@cosmos-kit/keplr-extension";
import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx";

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

Expand Down Expand Up @@ -404,10 +407,10 @@ export async function executeRoute(
for (let i = 0; i < msgsResponse.msgs.length; i++) {
const multiHopMsg = msgsResponse.msgs[i];

const account = await getAccount(walletClient, multiHopMsg.chain_id);
const signerIsLedger = await isLedger(walletClient, multiHopMsg.chain_id);

let signer: OfflineSigner;
if (account.isNanoLedger) {
if (signerIsLedger) {
signer = await getOfflineSignerOnlyAmino(
walletClient,
multiHopMsg.chain_id
Expand Down Expand Up @@ -483,7 +486,7 @@ export async function executeRoute(
const tx = await signAndBroadcastInjective(
walletClient,
msgJSON.sender,
MsgTransfer.fromJSON({
MsgTransferInjective.fromJSON({
amount: msgJSON.token,
memo: msgJSON.memo,
sender: msgJSON.sender,
Expand All @@ -500,10 +503,24 @@ export async function executeRoute(

txHash = tx.txHash;
} else {
const tx = await client.signAndBroadcast(msgJSON.sender, [msg], {
amount: [coin(0, feeInfo.denom)],
gas: `${gasNeeded}`,
});
const tx = await client.signAndBroadcast(
msgJSON.sender,
[
{
typeUrl: multiHopMsg.msg_type_url,
value: MsgTransfer.fromJSON({
token: msgJSON.token,
memo: msgJSON.memo,
sender: msgJSON.sender,
sourcePort: msgJSON.source_port,
receiver: msgJSON.receiver,
sourceChannel: msgJSON.source_channel,
timeoutTimestamp: msgJSON.timeout_timestamp,
}),
},
],
"auto"
);
txHash = tx.transactionHash;
}
} else {
Expand Down
28 changes: 28 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import {
DEFAULT_BLOCK_TIMEOUT_HEIGHT,
BigNumberInBase,
} from "@injectivelabs/utils";
import { KeplrClient } from "@cosmos-kit/keplr-extension";
import { CosmostationClient } from "@cosmos-kit/cosmostation-extension/dist/extension/client";
import { LeapClient } from "@cosmos-kit/leap-extension/dist/extension/client";

export function getChainByID(chainID: string) {
return chainRegistry.chains.find(
Expand Down Expand Up @@ -398,3 +401,28 @@ export function getFee(chainID: string) {

return amountNeeded;
}

export async function isLedger(walletClient: WalletClient, chainID: string) {
if (walletClient instanceof KeplrClient && window.keplr) {
const key = await window.keplr.getKey(chainID);
return key.isNanoLedger;
}

if (walletClient instanceof CosmostationClient) {
// @ts-ignore
const account = await window.cosmostation.cosmos.request({
method: "cos_account",
params: { chainName: chainID },
});
return account.isLedger;
}

if (walletClient instanceof LeapClient) {
// @ts-ignore
const key = await window.leap.getKey(chainID);

return key.isNanoLedger;
}

return false;
}

0 comments on commit f231858

Please sign in to comment.