diff --git a/.changeset/early-doors-act.md b/.changeset/early-doors-act.md new file mode 100644 index 000000000..38fa05b39 --- /dev/null +++ b/.changeset/early-doors-act.md @@ -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 diff --git a/packages/toolboxes/cosmos/src/thorchainUtils/messages.ts b/packages/toolboxes/cosmos/src/thorchainUtils/messages.ts index e1eb3585e..dcd88031f 100644 --- a/packages/toolboxes/cosmos/src/thorchainUtils/messages.ts +++ b/packages/toolboxes/cosmos/src/thorchainUtils/messages.ts @@ -13,8 +13,10 @@ import type { ThorcahinDepositTxParams, ThorchainTransferTxParams } from "./type type MsgSend = ReturnType; type MsgDeposit = ReturnType; -type MsgSendForBroadcast = ReturnType>; -type MsgDepositForBroadcast = ReturnType>; +type DirectMsgSendForBroadcast = ReturnType>; +type DirectMsgDepositForBroadcast = ReturnType< + typeof parseAminoMessageForDirectSigning +>; export const THORCHAIN_GAS_VALUE = getDefaultChainFee(Chain.THORChain).gas; export const MAYA_GAS_VALUE = getDefaultChainFee(Chain.Maya).gas; @@ -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); @@ -127,6 +128,7 @@ export const buildTransferTx = memo = "", chain, asSignable = true, + asAminoMessage = false, }: ThorchainTransferTxParams) => { const account = await getAccount({ rpcUrl, from }); @@ -138,7 +140,10 @@ export const buildTransferTx = }); const msg = asSignable - ? convertToSignable(prepareMessageForBroadcast(transferMsg), chain) + ? convertToSignable( + asAminoMessage ? transferMsg : parseAminoMessageForDirectSigning(transferMsg), + chain, + ) : transferMsg; const transaction = { @@ -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(depositMsg), chain) + ? convertToSignable( + asAminoMessage ? depositMsg : parseAminoMessageForDirectSigning(depositMsg), + chain, + ) : depositMsg; const transaction = { @@ -176,7 +191,7 @@ export const buildDepositTx = return transaction; }; -export function prepareMessageForBroadcast(msg: T) { +export function parseAminoMessageForDirectSigning(msg: T) { if (msg.type === "thorchain/MsgSend" || msg.type === "mayachain/MsgSend") return msg as MsgSend; return { @@ -212,7 +227,7 @@ export const buildEncodedTxBody = ({ memo, msgs, }: { - msgs: MsgSendForBroadcast[] | MsgDepositForBroadcast[]; + msgs: DirectMsgSendForBroadcast[] | DirectMsgDepositForBroadcast[]; memo: string; chain: Chain.THORChain | Chain.Maya; }) => { diff --git a/packages/toolboxes/cosmos/src/thorchainUtils/types/client-types.ts b/packages/toolboxes/cosmos/src/thorchainUtils/types/client-types.ts index 33a0367db..3a9421621 100644 --- a/packages/toolboxes/cosmos/src/thorchainUtils/types/client-types.ts +++ b/packages/toolboxes/cosmos/src/thorchainUtils/types/client-types.ts @@ -14,7 +14,7 @@ import type { buildEncodedTxBody, buildTransferTx, convertToSignable, - prepareMessageForBroadcast, + parseAminoMessageForDirectSigning, } from "../../index"; import type { Signer, TransferParams } from "../../types"; @@ -97,6 +97,7 @@ export type ThorchainTransferTxParams = { memo?: string; chain: Chain.THORChain | Chain.Maya; asSignable?: boolean; + asAminoMessage?: boolean; }; export type ThorcahinDepositTxParams = Omit; @@ -128,7 +129,9 @@ export type ThorchainToolboxType = BaseCosmosToolboxType & { params: ThorcahinDepositTxParams, ) => ReturnType>; buildEncodedTxBody: typeof buildEncodedTxBody; - prepareMessageForBroadcast: typeof prepareMessageForBroadcast; + // @deprecated - use `parseAminoMessageForDirectSigning` instead + prepareMessageForBroadcast: typeof parseAminoMessageForDirectSigning; + parseAminoMessageForDirectSigning: typeof parseAminoMessageForDirectSigning; createMultisig: (pubKeys: string[], threshold: number) => Promise; importSignature: (signature: string) => Uint8Array; secp256k1HdWalletFromMnemonic: (mnemonic: string, index?: number) => Promise; diff --git a/packages/toolboxes/cosmos/src/toolbox/thorchain.ts b/packages/toolboxes/cosmos/src/toolbox/thorchain.ts index 7f3b926d1..cf9fb2fed 100644 --- a/packages/toolboxes/cosmos/src/toolbox/thorchain.ts +++ b/packages/toolboxes/cosmos/src/toolbox/thorchain.ts @@ -30,7 +30,7 @@ import { convertToSignable, createDefaultAminoTypes, createDefaultRegistry, - prepareMessageForBroadcast, + parseAminoMessageForDirectSigning, } from "../thorchainUtils/index"; import type { DepositParam, @@ -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); } @@ -107,7 +106,7 @@ const signMultisigTx = async ( const bodyBytes = await buildEncodedTxBody({ chain, - msgs: msgs.map(prepareMessageForBroadcast), + msgs: msgs.map(parseAminoMessageForDirectSigning), memo, }); @@ -281,6 +280,7 @@ export const BaseThorchainToolbox = ({ }: Omit & { 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, { @@ -288,10 +288,13 @@ export const BaseThorchainToolbox = ({ 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); @@ -308,7 +311,8 @@ export const BaseThorchainToolbox = ({ buildDepositTx: buildDepositTx(rpcUrl), buildTransferTx: buildTransferTx(rpcUrl), buildEncodedTxBody, - prepareMessageForBroadcast, + prepareMessageForBroadcast: parseAminoMessageForDirectSigning, + parseAminoMessageForDirectSigning, createDefaultAminoTypes: () => createDefaultAminoTypes(chain), createDefaultRegistry, secp256k1HdWalletFromMnemonic: secp256k1HdWalletFromMnemonic({ diff --git a/packages/wallets/keplr/src/index.ts b/packages/wallets/keplr/src/index.ts index 1fb2f4494..4b510a8ab 100644 --- a/packages/wallets/keplr/src/index.ts +++ b/packages/wallets/keplr/src/index.ts @@ -10,7 +10,6 @@ import { filterSupportedChains, setRequestClientConfig, } from "@swapkit/helpers"; -import type { ThorchainToolboxType } from "@swapkit/toolbox-cosmos"; import { chainRegistry } from "./chainRegistry"; declare global { @@ -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, diff --git a/packages/wallets/ledger/src/ledger.ts b/packages/wallets/ledger/src/ledger.ts index ede755205..25a551c13 100644 --- a/packages/wallets/ledger/src/ledger.ts +++ b/packages/wallets/ledger/src/ledger.ts @@ -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 }); @@ -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( diff --git a/packages/wallets/wc/src/walletconnect.ts b/packages/wallets/wc/src/walletconnect.ts index ec6d8acd5..d08f63fea 100644 --- a/packages/wallets/wc/src/walletconnect.ts +++ b/packages/wallets/wc/src/walletconnect.ts @@ -95,7 +95,7 @@ async function getToolbox({ createStargateClient, fromBase64, getDefaultChainFee, - prepareMessageForBroadcast, + parseAminoMessageForDirectSigning, } = await import("@swapkit/toolbox-cosmos"); const toolbox = ThorchainToolbox({ stagenet: false }); @@ -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);