Skip to content
Draft
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
5 changes: 5 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"viem": "2.x"
},
"dependencies": {
"@chain-registry/v2": "^1.71.203",
"@cosmjs/amino": "0.33.1",
"@cosmjs/cosmwasm-stargate": "0.33.1",
"@cosmjs/encoding": "0.33.1",
Expand All @@ -61,6 +62,10 @@
"@cosmjs/stargate": "0.33.1",
"@cosmjs/tendermint-rpc": "0.33.1",
"@injectivelabs/sdk-ts": "1.15.3",
"@interchainjs/auth": "^1.11.5",
"@interchainjs/cosmos": "^1.11.5",
"@interchainjs/ethereum": "^1.11.5",
"@interchainjs/injective": "^1.11.5",
"@keplr-wallet/unit": "^0.12.143",
"@solana/wallet-adapter-base": "^0.9.23",
"bech32": "^2.0.0",
Expand Down
4 changes: 1 addition & 3 deletions packages/client/src/amino/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { toBase64 } from "@cosmjs/encoding";

import { EthSecp256k1Pubkey, pubkeyType } from "./pubkey";

export function encodeEthSecp256k1Pubkey(
pubkey: Uint8Array,
): EthSecp256k1Pubkey {
export function encodeEthSecp256k1Pubkey(pubkey: Uint8Array): EthSecp256k1Pubkey {
if (pubkey.length !== 33 || (pubkey[0] !== 0x02 && pubkey[0] !== 0x03)) {
throw new Error(
"Public key must be compressed secp256k1, i.e. 33 bytes starting with 0x02 or 0x03",
Expand Down
14 changes: 4 additions & 10 deletions packages/client/src/amino/pubkey.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
// https://github.com/archmage-live/archmage-x/blob/develop/lib/network/cosm/amino/pubkey.ts

import {
Pubkey,
SinglePubkey,
pubkeyType as cosmPubkeyType,
} from "@cosmjs/amino";
import { Pubkey, SinglePubkey, pubkeyType as cosmPubkeyType } from "@cosmjs/amino";

export interface EthSecp256k1Pubkey extends SinglePubkey {
export type EthSecp256k1Pubkey = {
readonly type: "ethermint/PubKeyEthSecp256k1";
readonly value: string;
}
} & SinglePubkey;

export function isEthSecp256k1Pubkey(
pubkey: Pubkey,
): pubkey is EthSecp256k1Pubkey {
export function isEthSecp256k1Pubkey(pubkey: Pubkey): pubkey is EthSecp256k1Pubkey {
return (pubkey as EthSecp256k1Pubkey).type === "ethermint/PubKeyEthSecp256k1";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { isOfflineDirectSigner } from "@cosmjs/proto-signing/build/signer";
import { signCosmosMessageDirect } from "./signCosmosMessageDirect";
import { signCosmosMessageAmino } from "./signCosmosMessageAmino";
import { ExecuteRouteOptions } from "src/public-functions/executeRoute";
import { SigningClient } from "@interchainjs/cosmos/signing-client";
import { getEncodeObjectFromCosmosMessage } from "./getEncodeObjectFromCosmosMessage";
import { getRpcEndpointForChain } from "../getRpcEndpointForChain";
import { AminoConverter } from "@cosmjs/stargate";
import { toCamel } from "src/utils/convert";
import { ICosmosGenericOfflineSigner } from "@interchainjs/cosmos/types/wallet";

type ExecuteCosmosTransactionProps = {
tx?: {
Expand Down Expand Up @@ -51,23 +57,24 @@ export const executeCosmosTransaction = async ({
throw new Error("no messages found for tx");
}

const { stargateClient, signer } = await getSigningStargateClient({
chainId: chainId,
getOfflineSigner: options?.getCosmosSigner,
});
// const { stargateClient, signer } = await getSigningStargateClient({
// chainId: chainId,
// getOfflineSigner: options?.getCosmosSigner,
// });

if (!currentUserAddress) {
throw new Error(
`executeCosmosTransaction error: invalid address for chain '${tx.cosmosTx?.chainId}'`,
);
}

const accounts = await signer.getAccounts();
const accountFromSigner = accounts.find((account) => account.address === currentUserAddress);
// const accounts = await signer.getAccounts();

if (!accountFromSigner) {
throw new Error("executeCosmosTransaction error: failed to retrieve account from signer");
}
// const accountFromSigner = accounts.find((account) => account.address === currentUserAddress);

// if (!accountFromSigner) {
// throw new Error("executeCosmosTransaction error: failed to retrieve account from signer");
// }

const fee = gasUsed?.fee;

Expand All @@ -94,22 +101,84 @@ export const executeCosmosTransaction = async ({
},
};

if (isOfflineDirectSigner(signer)) {
rawTx = await signCosmosMessageDirect({
...commonRawTxBody,
signer,
});
} else {
rawTx = await signCosmosMessageAmino({ ...commonRawTxBody, signer });
}
const cosmosSigner = (await options?.getCosmosSigner?.(chainId)) as ICosmosGenericOfflineSigner;

const endpoint = await getRpcEndpointForChain(chainId);

console.log(cosmosSigner);

const signer = await SigningClient.connectWithSigner(endpoint, cosmosSigner);

const registry = Object.assign({}, ClientState.registry) as any;
const encoders = Array.from(registry.types).map((registry) => registry?.[1]);
signer.addEncoders(encoders);

const aminoTypes = Object.assign({}, ClientState.aminoTypes) as any;
console.log(aminoTypes);
const converters = Object.entries(aminoTypes.register).map(([typeUrl, converter]) => ({
typeUrl,
...(converter as AminoConverter),
}));

console.log(converters);
signer.addConverters(converters);

// if (message.msgTypeUrl === "/circle.cctp.v1.MsgDepositForBurnWithCaller") {
// return {
// typeUrl: message.msgTypeUrl,
// value: MsgDepositForBurnWithCaller.fromAmino(msgJson),
// };
// }

console.log("got signer", signer);

// const encodeObjectMessages = messages.map((cosmosMsg) => ({
// typeUrl: cosmosMsg.msgTypeUrl ?? "",
// value: toCamel(JSON.parse(cosmosMsg.msg ?? "")),
// }));

const encodeObjectMessages = messages.map((cosmosMsg) =>
getEncodeObjectFromCosmosMessage(cosmosMsg),
);

console.log(currentUserAddress, encodeObjectMessages);

const txResponse = await signer.signAndBroadcast(
currentUserAddress,
encodeObjectMessages,
"auto",
);

// if (isOfflineDirectSigner(signer)) {
// rawTx = await signCosmosMessageDirect({
// ...commonRawTxBody,
// signer,
// });
// } else {
// const cosmosSigner = await options?.getCosmosSigner?.(tx.cosmosTx?.chainId ?? "");
// if (cosmosSigner) {
// const signDirect = cosmosSigner as OfflineDirectSigner;
// const signAmino = cosmosSigner as OfflineAminoSigner;
// if (signDirect?.signDirect !== undefined) {
// signDirect.sign = signDirect.signDirect;
// }
// if (signer?.signAmino) {
// signer.sign = signer.signAmino;
// }

// console.log(client);
// }

// rawTx = await signCosmosMessageAmino({ ...commonRawTxBody, signer });
// }

options?.onTransactionSigned?.({
chainId,
});

const txBytes = TxRaw.encode(rawTx).finish();
// const txBytes = TxRaw.encode(rawTx).finish();

const txResponse = await stargateClient.broadcastTx(txBytes);
// const txResponse = await stargateClient.broadcastTx(txBytes);

return {
chainId: tx?.cosmosTx?.chainId ?? "",
Expand Down
13 changes: 4 additions & 9 deletions packages/client/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { encodeSecp256k1Pubkey } from "@cosmjs/amino";
export const accountParser: AccountParser = (acc) => {
switch (acc.typeUrl) {
case "/stride.vesting.StridePeriodicVestingAccount": {
const baseAccount = StridePeriodicVestingAccount.decode(acc.value)
.baseVestingAccount?.baseAccount;
const baseAccount = StridePeriodicVestingAccount.decode(acc.value).baseVestingAccount
?.baseAccount;
assertDefinedAndNotNull(baseAccount);

return {
Expand All @@ -22,17 +22,12 @@ export const accountParser: AccountParser = (acc) => {
};
}
case "/injective.types.v1beta1.EthAccount":
return accountEthParser(
acc,
"/injective.crypto.v1beta1.ethsecp256k1.PubKey",
);
return accountEthParser(acc, "/injective.crypto.v1beta1.ethsecp256k1.PubKey");
case "/ethermint.types.v1.EthAccount":
return accountEthParser(acc, "/ethermint.crypto.v1.ethsecp256k1.PubKey");
default: {
if (acc.typeUrl === "/cosmos.auth.v1beta1.BaseAccount") {
const { address, pubKey, accountNumber, sequence } = BaseAccount.decode(
acc.value,
);
const { address, pubKey, accountNumber, sequence } = BaseAccount.decode(acc.value);
if (pubKey?.typeUrl === "/initia.crypto.v1beta1.ethsecp256k1.PubKey") {
const { key } = PubKey.decode(pubKey.value);
const pk = encodeSecp256k1Pubkey(key);
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeneratedType, Registry } from "@cosmjs/proto-signing";
import { GeneratedType } from "@cosmjs/proto-signing";
import { AminoConverters, AminoTypes, SigningStargateClient } from "@cosmjs/stargate";
import { Chain, Asset, ChainAffiliates } from "./types/swaggerTypes";
import { ApiResponse } from "./utils/generateApi";
Expand All @@ -7,6 +7,7 @@ import { getMainnetAndTestnetAssets } from "./private-functions/getMainnetAndTes
import { balances } from "./api/postBalances";
import { EndpointOptions, SignerGetters, ValidateGasResult } from "src/types/client-types";
import { SkipApiOptions } from "./utils/fetchClient";
import { Registry } from "@interchainjs/cosmos/types/signing-client";

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class ClientState {
Expand Down
7 changes: 2 additions & 5 deletions packages/client/src/types/client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WalletClient } from "viem";

import { Adapter } from "@solana/wallet-adapter-base";
import { ChainType, CosmosMsg, FeeAsset } from "./swaggerTypes";
import { ICosmosGenericOfflineSigner } from "@interchainjs/cosmos/types/wallet";

/** Common Types */
export type UserAddress = {
Expand All @@ -27,11 +28,7 @@ export type ValidateGasResult = {
/** Signer Getters */
export type SignerGetters = {
getEvmSigner?: (chainId: string) => Promise<WalletClient>;
getCosmosSigner?: (
chainId: string,
) => Promise<
(OfflineAminoSigner & OfflineDirectSigner) | OfflineAminoSigner | OfflineDirectSigner
>;
getCosmosSigner?: (chainId: string) => Promise<ICosmosGenericOfflineSigner>;
getSvmSigner?: () => Promise<Adapter>;
};

Expand Down
1 change: 1 addition & 0 deletions packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"@cosmjs/tendermint-rpc": "0.31.3",
"@ebay/nice-modal-react": "^1.2.13",
"@eslint/compat": "^1.1.1",
"@interchainjs/cosmos": "^1.11.5",
"@leapwallet/cosmos-social-login-capsule-provider": "^0.0.44",
"@penumbra-zone/bech32m": "^13.0.0",
"@penumbra-zone/client": "^24.0.0",
Expand Down
28 changes: 25 additions & 3 deletions packages/widget/src/state/swapExecutionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import { config } from "@/constants/wagmi";
import { WalletClient } from "viem";
import { getWalletClient } from "@wagmi/core";
import { SIGN_MODE, AccountData, StdSignDoc, SignDoc } from "@interchainjs/types";

Check failure on line 39 in packages/widget/src/state/swapExecutionPage.ts

View workflow job for this annotation

GitHub Actions / eslint

'AccountData' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 39 in packages/widget/src/state/swapExecutionPage.ts

View workflow job for this annotation

GitHub Actions / eslint

'SignDoc' is defined but never used. Allowed unused vars must match /^_/u
import { OfflineDirectSigner } from "@interchainjs/cosmos/types/wallet";

Check failure on line 40 in packages/widget/src/state/swapExecutionPage.ts

View workflow job for this annotation

GitHub Actions / eslint

'OfflineDirectSigner' is defined but never used. Allowed unused vars must match /^_/u

type ValidatingGasBalanceData = {
chainId?: string;
Expand Down Expand Up @@ -368,7 +370,7 @@
export const skipSubmitSwapExecutionAtom = atomWithMutation((get) => {
const { route, userAddresses, transactionDetailsArray } = get(swapExecutionStateAtom);
const submitSwapExecutionCallbacks = get(submitSwapExecutionCallbacksAtom);
const getFallbackGasAmount = get(fallbackGasAmountFnAtom);

Check failure on line 373 in packages/widget/src/state/swapExecutionPage.ts

View workflow job for this annotation

GitHub Actions / eslint

'getFallbackGasAmount' is assigned a value but never used. Allowed unused vars must match /^_/u
const simulateTx = get(simulateTxAtom);
const swapSettings = get(swapSettingsAtom);
const getSigners = get(getConnectedSignersAtom);
Expand Down Expand Up @@ -421,9 +423,29 @@
}
const key = await wallet.getKey(chainId);

return key.isNanoLedger
? wallet.getOfflineSignerOnlyAmino(chainId)
: wallet.getOfflineSigner(chainId);
const aminoSigner = wallet.getOfflineSignerOnlyAmino(chainId);

const offlineDirectSigner = wallet.getOfflineSigner(chainId);

const offlineSigner = key.isNanoLedger ? aminoSigner : offlineDirectSigner;

return {
getAccounts: () => offlineSigner.getAccounts(),
// getAccounts: () => new Promise((resolve) => resolve(response as AccountData[])),
signMode: key.isNanoLedger ? SIGN_MODE.AMINO : SIGN_MODE.DIRECT,
sign: ({
signerAddress,
signDoc,
}: {
signerAddress: string;
signDoc: StdSignDoc;
}) => {
if (key.isNanoLedger) {
return aminoSigner.signAmino?.(signerAddress, signDoc);
}
return offlineDirectSigner.signDirect?.(signerAddress, signDoc);
},
};
},
getEvmSigner: async (chainId) => {
if (getSigners?.getEvmSigner) {
Expand Down
Loading
Loading