diff --git a/packages/bcp/src/cosmwasmconnection.spec.ts b/packages/bcp/src/cosmwasmconnection.spec.ts index 993201bf..3e1c6857 100644 --- a/packages/bcp/src/cosmwasmconnection.spec.ts +++ b/packages/bcp/src/cosmwasmconnection.spec.ts @@ -306,7 +306,7 @@ describe("CosmWasmConnection", () => { expect(transaction).toEqual(unsigned); expect(signatures.length).toEqual(1); expect(signatures[0]).toEqual({ - nonce: -1, // Unfortunately this information is unavailable as previous implementation attempt is broken. See https://github.com/iov-one/iov-core/pull/1390 + nonce: signed.signatures[0].nonce, pubkey: { algo: signed.signatures[0].pubkey.algo, data: Secp256k1.compressPubkey(signed.signatures[0].pubkey.data), diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 2dcf49a4..3f1c21ce 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { CosmosAddressBech32Prefix, CosmWasmClient, RestClient, TxsResponse } from "@cosmwasm/sdk"; +import { CosmosAddressBech32Prefix, CosmWasmClient, RestClient, TxsResponse, types } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -359,10 +359,25 @@ export class CosmWasmConnection implements BlockchainConnection { private async parseAndPopulateTxResponseSigned( response: TxsResponse, ): Promise | FailedTransaction> { - // There is no known way to get the nonce that was used for signing a transaction. - // This information is nesessary for signature validation. - // TODO: fix - const nonce = -1 as Nonce; + const firstMsg = response.tx.value.msg.find(() => true); + if (!firstMsg) throw new Error("Got transaction without a first message. What is going on here?"); + + let senderAddress: string; + if (types.isMsgSend(firstMsg)) { + senderAddress = firstMsg.value.from_address; + } else if ( + types.isMsgStoreCode(firstMsg) || + types.isMsgInstantiateContract(firstMsg) || + types.isMsgExecuteContract(firstMsg) + ) { + senderAddress = firstMsg.value.sender; + } else { + throw new Error(`Got unsupported type of message: ${firstMsg.type}`); + } + + const { accountNumber, sequence: currentSequence } = await this.cosmWasmClient.getNonce(senderAddress); + + const nonce = accountToNonce(accountNumber, currentSequence - 1); return parseTxsResponseSigned( this.chainId,