Skip to content

Commit

Permalink
feat: improve amino signer tc tx handling (#1187)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
towanTG and github-actions[bot] authored Feb 12, 2025
1 parent ade8288 commit 5e765df
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 42 deletions.
8 changes: 8 additions & 0 deletions .changeset/early-doors-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@swapkit/toolbox-cosmos": minor
"@swapkit/wallet-ledger": minor
"@swapkit/wallet-keplr": minor
"@swapkit/wallet-wc": minor
---

Improves message formatting for amino and direct signer for TC like messages and fixes keplr tc implementation
33 changes: 24 additions & 9 deletions packages/toolboxes/cosmos/src/thorchainUtils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import type { ThorcahinDepositTxParams, ThorchainTransferTxParams } from "./type

type MsgSend = ReturnType<typeof transferMsgAmino>;
type MsgDeposit = ReturnType<typeof depositMsgAmino>;
type MsgSendForBroadcast = ReturnType<typeof prepareMessageForBroadcast<MsgSend>>;
type MsgDepositForBroadcast = ReturnType<typeof prepareMessageForBroadcast<MsgDeposit>>;
type DirectMsgSendForBroadcast = ReturnType<typeof parseAminoMessageForDirectSigning<MsgSend>>;
type DirectMsgDepositForBroadcast = ReturnType<
typeof parseAminoMessageForDirectSigning<MsgDeposit>
>;

export const THORCHAIN_GAS_VALUE = getDefaultChainFee(Chain.THORChain).gas;
export const MAYA_GAS_VALUE = getDefaultChainFee(Chain.Maya).gas;
Expand Down Expand Up @@ -90,9 +92,8 @@ export const buildAminoMsg = ({
return msg;
};

// TODO I think the msg typing is wrong it should be not prepared for broadcast
export const convertToSignable = (
msg: MsgDepositForBroadcast | MsgSendForBroadcast,
msg: DirectMsgDepositForBroadcast | DirectMsgSendForBroadcast | MsgSend | MsgDeposit,
chain: Chain.THORChain | Chain.Maya,
) => {
const aminoTypes = createDefaultAminoTypes(chain);
Expand Down Expand Up @@ -127,6 +128,7 @@ export const buildTransferTx =
memo = "",
chain,
asSignable = true,
asAminoMessage = false,
}: ThorchainTransferTxParams) => {
const account = await getAccount({ rpcUrl, from });

Expand All @@ -138,7 +140,10 @@ export const buildTransferTx =
});

const msg = asSignable
? convertToSignable(prepareMessageForBroadcast(transferMsg), chain)
? convertToSignable(
asAminoMessage ? transferMsg : parseAminoMessageForDirectSigning(transferMsg),
chain,
)
: transferMsg;

const transaction = {
Expand All @@ -155,13 +160,23 @@ export const buildTransferTx =

export const buildDepositTx =
(rpcUrl: string) =>
async ({ from, assetValue, memo = "", chain, asSignable = true }: ThorcahinDepositTxParams) => {
async ({
from,
assetValue,
memo = "",
chain,
asSignable = true,
asAminoMessage = false,
}: ThorcahinDepositTxParams) => {
const account = await getAccount({ rpcUrl, from });

const depositMsg = depositMsgAmino({ from, assetValue, memo, chain });

const msg = asSignable
? convertToSignable(prepareMessageForBroadcast<MsgDeposit>(depositMsg), chain)
? convertToSignable(
asAminoMessage ? depositMsg : parseAminoMessageForDirectSigning<MsgDeposit>(depositMsg),
chain,
)
: depositMsg;

const transaction = {
Expand All @@ -176,7 +191,7 @@ export const buildDepositTx =
return transaction;
};

export function prepareMessageForBroadcast<T extends MsgDeposit | MsgSend>(msg: T) {
export function parseAminoMessageForDirectSigning<T extends MsgDeposit | MsgSend>(msg: T) {
if (msg.type === "thorchain/MsgSend" || msg.type === "mayachain/MsgSend") return msg as MsgSend;

return {
Expand Down Expand Up @@ -212,7 +227,7 @@ export const buildEncodedTxBody = ({
memo,
msgs,
}: {
msgs: MsgSendForBroadcast[] | MsgDepositForBroadcast[];
msgs: DirectMsgSendForBroadcast[] | DirectMsgDepositForBroadcast[];
memo: string;
chain: Chain.THORChain | Chain.Maya;
}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
buildEncodedTxBody,
buildTransferTx,
convertToSignable,
prepareMessageForBroadcast,
parseAminoMessageForDirectSigning,
} from "../../index";
import type { Signer, TransferParams } from "../../types";

Expand Down Expand Up @@ -97,6 +97,7 @@ export type ThorchainTransferTxParams = {
memo?: string;
chain: Chain.THORChain | Chain.Maya;
asSignable?: boolean;
asAminoMessage?: boolean;
};

export type ThorcahinDepositTxParams = Omit<ThorchainTransferTxParams, "recipient">;
Expand Down Expand Up @@ -128,7 +129,9 @@ export type ThorchainToolboxType = BaseCosmosToolboxType & {
params: ThorcahinDepositTxParams,
) => ReturnType<ReturnType<typeof buildDepositTx>>;
buildEncodedTxBody: typeof buildEncodedTxBody;
prepareMessageForBroadcast: typeof prepareMessageForBroadcast;
// @deprecated - use `parseAminoMessageForDirectSigning` instead
prepareMessageForBroadcast: typeof parseAminoMessageForDirectSigning;
parseAminoMessageForDirectSigning: typeof parseAminoMessageForDirectSigning;
createMultisig: (pubKeys: string[], threshold: number) => Promise<MultisigThresholdPubkey>;
importSignature: (signature: string) => Uint8Array;
secp256k1HdWalletFromMnemonic: (mnemonic: string, index?: number) => Promise<Secp256k1HdWallet>;
Expand Down
20 changes: 12 additions & 8 deletions packages/toolboxes/cosmos/src/toolbox/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
convertToSignable,
createDefaultAminoTypes,
createDefaultRegistry,
prepareMessageForBroadcast,
parseAminoMessageForDirectSigning,
} from "../thorchainUtils/index";
import type {
DepositParam,
Expand Down Expand Up @@ -92,7 +92,6 @@ const signMultisigTx = async (
const msgForSigning = [];

for (const msg of msgs) {
// @ts-expect-error wrong typing of convertToSignable - investigation needed
const signMsg = await convertToSignable(msg, chain);
msgForSigning.push(signMsg);
}
Expand All @@ -107,7 +106,7 @@ const signMultisigTx = async (

const bodyBytes = await buildEncodedTxBody({
chain,
msgs: msgs.map(prepareMessageForBroadcast),
msgs: msgs.map(parseAminoMessageForDirectSigning),
memo,
});

Expand Down Expand Up @@ -281,17 +280,21 @@ export const BaseThorchainToolbox = ({
}: Omit<TransferParams, "recipient"> & { recipient?: string }) => {
if (!signer) throw new Error("Signer not defined");

const isAminoSigner = "signAmino" in signer;
const registry = createDefaultRegistry();
const aminoTypes = createDefaultAminoTypes(chain);
const signingClient = await createSigningStargateClient(rpcUrl, signer, {
registry,
aminoTypes,
});

const msgSign = convertToSignable(
prepareMessageForBroadcast(buildAminoMsg({ assetValue, from, recipient, memo, chain })),
chain,
);
const aminoMessage = buildAminoMsg({ assetValue, from, recipient, memo, chain });

const preparedMessage = isAminoSigner
? aminoMessage
: parseAminoMessageForDirectSigning(aminoMessage);

const msgSign = convertToSignable(preparedMessage, chain);

const txResponse = await signingClient.signAndBroadcast(from, [msgSign], defaultFee, memo);

Expand All @@ -308,7 +311,8 @@ export const BaseThorchainToolbox = ({
buildDepositTx: buildDepositTx(rpcUrl),
buildTransferTx: buildTransferTx(rpcUrl),
buildEncodedTxBody,
prepareMessageForBroadcast,
prepareMessageForBroadcast: parseAminoMessageForDirectSigning,
parseAminoMessageForDirectSigning,
createDefaultAminoTypes: () => createDefaultAminoTypes(chain),
createDefaultRegistry,
secp256k1HdWalletFromMnemonic: secp256k1HdWalletFromMnemonic({
Expand Down
19 changes: 0 additions & 19 deletions packages/wallets/keplr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
filterSupportedChains,
setRequestClientConfig,
} from "@swapkit/helpers";
import type { ThorchainToolboxType } from "@swapkit/toolbox-cosmos";
import { chainRegistry } from "./chainRegistry";

declare global {
Expand Down Expand Up @@ -74,26 +73,8 @@ function connectKeplr({
from: params.from || address,
});

const deposit =
chain === Chain.THORChain
? {
deposit: (params: {
from?: string;
assetValue: AssetValue;
memo?: string;
}) =>
(toolbox as ThorchainToolboxType).deposit({
...params,
signer: offlineSigner,
from: params.from || address,
memo: params.memo || "",
}),
}
: {};

addChain({
...toolbox,
...deposit,
chain,
transfer,
address,
Expand Down
4 changes: 2 additions & 2 deletions packages/wallets/ledger/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const getToolbox = async ({
buildAminoMsg,
getDefaultChainFee,
fromBase64,
prepareMessageForBroadcast,
parseAminoMessageForDirectSigning,
} = await import("@swapkit/toolbox-cosmos");
const toolbox = ThorchainToolbox({ stagenet: false });
const signer = await getLedgerClient({ chain, derivationPath });
Expand Down Expand Up @@ -223,7 +223,7 @@ const getToolbox = async ({
if (!signatures) throw new Error("tx signing failed");

const pubkey = encodePubkey({ type: "tendermint/PubKeySecp256k1", value });
const msgs = orderedMessages.map(prepareMessageForBroadcast);
const msgs = orderedMessages.map(parseAminoMessageForDirectSigning);
const bodyBytes = buildEncodedTxBody({ msgs, chain, memo });

const authInfoBytes = makeAuthInfoBytes(
Expand Down
4 changes: 2 additions & 2 deletions packages/wallets/wc/src/walletconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function getToolbox({
createStargateClient,
fromBase64,
getDefaultChainFee,
prepareMessageForBroadcast,
parseAminoMessageForDirectSigning,
} = await import("@swapkit/toolbox-cosmos");
const toolbox = ThorchainToolbox({ stagenet: false });

Expand Down Expand Up @@ -149,7 +149,7 @@ async function getToolbox({

const bodyBytes = buildEncodedTxBody({
chain: Chain.THORChain,
msgs: msgs.map(prepareMessageForBroadcast),
msgs: msgs.map(parseAminoMessageForDirectSigning),
memo: memo || "",
});
const pubkey = encodePubkey(account.pubkey);
Expand Down

0 comments on commit 5e765df

Please sign in to comment.