diff --git a/packages/stargate/src/encoding.ts b/packages/stargate/src/encoding.ts index 8b238a79..71ffe880 100644 --- a/packages/stargate/src/encoding.ts +++ b/packages/stargate/src/encoding.ts @@ -1,27 +1,36 @@ +const typeRegister: Record = { + "/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend", + "/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend", + "/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant", + "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress": "cosmos-sdk/MsgSetWithdrawAddress", + "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": "cosmos-sdk/MsgWithdrawDelegatorReward", + "/cosmos.distribution.v1beta1.MsgWithdrawValidatorComission": "cosmos-sdk/MsgWithdrawValidatorComission", + "/cosmos.distribution.v1beta1.MsgFundCommunityPool": "cosmos-sdk/MsgFundCommunityPool", + "/cosmos.evidence.v1beta1.MsgSubmitEvidence": "cosmos-sdk/MsgSubmitEvidence", + "/cosmos.gov.v1beta1.MsgSubmitProposal": "cosmos-sdk/MsgSubmitProposal", + "/cosmos.gov.v1beta1.MsgVote": "cosmos-sdk/MsgVote", + "/cosmos.gov.v1beta1.MsgDeposit": "cosmos-sdk/MsgDeposit", + "/cosmos.slashing.v1beta1.MsgUnjail": "cosmos-sdk/MsgUnjail", + "/cosmos.staking.v1beta1.MsgCreateValidator": "cosmos-sdk/MsgCreateValidator", + "/cosmos.staking.v1beta1.MsgEditValidator": "cosmos-sdk/MsgEditValidator", + "/cosmos.staking.v1beta1.MsgDelegate": "cosmos-sdk/MsgDelegate", + "/cosmos.staking.v1beta1.MsgBeginRedelegate": "cosmos-sdk/MsgBeginRedelegate", + "/cosmos.staking.v1beta1.MsgUndelegate": "cosmos-sdk/MsgUndelegate", + "/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount", +}; + export function getMsgType(typeUrl: string): string { - const typeRegister: Record = { - "/cosmos.bank.v1beta1.MsgSend": "cosmos-sdk/MsgSend", - "/cosmos.bank.v1beta1.MsgMultiSend": "cosmos-sdk/MsgMultiSend", - "/cosmos.crisis.v1beta1.MsgVerifyInvariant": "cosmos-sdk/MsgVerifyInvariant", - "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress": "cosmos-sdk/MsgSetWithdrawAddress", - "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward": "cosmos-sdk/MsgWithdrawDelegatorReward", - "/cosmos.distribution.v1beta1.MsgWithdrawValidatorComission": "cosmos-sdk/MsgWithdrawValidatorComission", - "/cosmos.distribution.v1beta1.MsgFundCommunityPool": "cosmos-sdk/MsgFundCommunityPool", - "/cosmos.evidence.v1beta1.MsgSubmitEvidence": "cosmos-sdk/MsgSubmitEvidence", - "/cosmos.gov.v1beta1.MsgSubmitProposal": "cosmos-sdk/MsgSubmitProposal", - "/cosmos.gov.v1beta1.MsgVote": "cosmos-sdk/MsgVote", - "/cosmos.gov.v1beta1.MsgDeposit": "cosmos-sdk/MsgDeposit", - "/cosmos.slashing.v1beta1.MsgUnjail": "cosmos-sdk/MsgUnjail", - "/cosmos.staking.v1beta1.MsgCreateValidator": "cosmos-sdk/MsgCreateValidator", - "/cosmos.staking.v1beta1.MsgEditValidator": "cosmos-sdk/MsgEditValidator", - "/cosmos.staking.v1beta1.MsgDelegate": "cosmos-sdk/MsgDelegate", - "/cosmos.staking.v1beta1.MsgBeginRedelegate": "cosmos-sdk/MsgBeginRedelegate", - "/cosmos.staking.v1beta1.MsgUndelegate": "cosmos-sdk/MsgUndelegate", - "/cosmos.vesting.v1beta1.MsgCreateVestingAccount": "cosmos-sdk/MsgCreateVestingAccount", - }; const type = typeRegister[typeUrl]; if (!type) { throw new Error("Type URL not known"); } return type; } + +export function getMsgTypeUrl(type: string): string { + const [typeUrl] = Object.entries(typeRegister).find(([_typeUrl, value]) => value === type) ?? []; + if (!typeUrl) { + throw new Error("Type not known"); + } + return typeUrl; +} diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index c93c822b..67a84ab5 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -24,7 +24,7 @@ import { import { adaptor34, Client as TendermintClient } from "@cosmjs/tendermint-rpc"; import { cosmos } from "./codec"; -import { getMsgType } from "./encoding"; +import { getMsgType, getMsgTypeUrl } from "./encoding"; import { BroadcastTxResponse, StargateClient } from "./stargateclient"; const { TxRaw } = cosmos.tx.v1beta1; @@ -123,10 +123,10 @@ export class SigningStargateClient extends StargateClient { if (isOfflineDirectSigner(this.signer)) { const authInfoBytes = makeAuthInfoBytes([pubkeyAny], fee.amount, gasLimit, sequence); const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); - const { signature } = await this.signer.signDirect(address, signDoc); + const { signature, signed } = await this.signer.signDirect(address, signDoc); const txRaw = TxRaw.create({ - bodyBytes: txBodyBytes, - authInfoBytes: authInfoBytes, + bodyBytes: signed.bodyBytes, + authInfoBytes: signed.authInfoBytes, signatures: [fromBase64(signature.signature)], }); const signedTx = Uint8Array.from(TxRaw.encode(txRaw).finish()); @@ -135,17 +135,36 @@ export class SigningStargateClient extends StargateClient { // Amino signer const signMode = cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_LEGACY_AMINO_JSON; - const authInfoBytes = makeAuthInfoBytes([pubkeyAny], fee.amount, gasLimit, sequence, signMode); const msgs = messages.map((msg) => ({ type: getMsgType(msg.typeUrl), value: msg.value, })); const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence); - const signResponse = await this.signer.signAmino(address, signDoc); + const { signature, signed } = await this.signer.signAmino(address, signDoc); + const signedTxBody = { + messages: signed.msgs.map((msg) => ({ + typeUrl: getMsgTypeUrl(msg.type), + value: msg.value, + })), + memo: signed.memo, + }; + const signedTxBodyBytes = this.registry.encode({ + typeUrl: "/cosmos.tx.v1beta1.TxBody", + value: signedTxBody, + }); + const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber(); + const signedSequence = Int53.fromString(signed.sequence).toNumber(); + const signedAuthInfoBytes = makeAuthInfoBytes( + [pubkeyAny], + signed.fee.amount, + signedGasLimit, + signedSequence, + signMode, + ); const txRaw = TxRaw.create({ - bodyBytes: txBodyBytes, - authInfoBytes: authInfoBytes, - signatures: [fromBase64(signResponse.signature.signature)], + bodyBytes: signedTxBodyBytes, + authInfoBytes: signedAuthInfoBytes, + signatures: [fromBase64(signature.signature)], }); const signedTx = Uint8Array.from(TxRaw.encode(txRaw).finish()); return this.broadcastTx(signedTx); diff --git a/packages/stargate/types/encoding.d.ts b/packages/stargate/types/encoding.d.ts index 6404c1ca..af3b9e44 100644 --- a/packages/stargate/types/encoding.d.ts +++ b/packages/stargate/types/encoding.d.ts @@ -1 +1,2 @@ export declare function getMsgType(typeUrl: string): string; +export declare function getMsgTypeUrl(type: string): string;