diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 21b65fe4..a1f1d56d 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ -import { RestClient, TxsResponse, unmarshalTx } from "@cosmwasm/sdk"; +import { RestClient, TxsResponse, types, unmarshalTx } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -305,8 +305,19 @@ export class CosmWasmConnection implements BlockchainConnection { response: TxsResponse, chainId: ChainId, ): Promise | FailedTransaction> { - const sender = (response.tx.value as any).msg[0].value.from_address; - const accountForHeight = await this.restClient.authAccounts(sender, response.height); + 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)) { + senderAddress = firstMsg.value.sender; + } else { + throw new Error(`Got unsupported type of message: ${firstMsg.type}`); + } + + const accountForHeight = await this.restClient.authAccounts(senderAddress, response.height); // this is technically not the proper nonce. maybe this causes issues for sig validation? // leaving for now unless it causes issues const sequence = (accountForHeight.result.value.sequence - 1) as Nonce; diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index af83599a..77b3eb30 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -72,21 +72,27 @@ export function decodeAmount(tokens: TokenInfos, coin: types.Coin): Amount { }; } -export function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): SendTransaction { - if (!types.isMsgSend(msg)) { - throw new Error("Unknown message type in transaction"); +export function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): UnsignedTransaction { + if (types.isMsgSend(msg)) { + if (msg.value.amount.length !== 1) { + throw new Error("Only MsgSend with one amount is supported"); + } + const send: SendTransaction = { + kind: "bcp/send", + chainId: chainId, + sender: msg.value.from_address as Address, + recipient: msg.value.to_address as Address, + amount: decodeAmount(tokens, msg.value.amount[0]), + }; + return send; + } else { + // Unknown transaction type + const unknown = { + chainId: chainId, + kind: "bcp/unknown", + }; + return unknown; } - const msgValue = msg.value; - if (msgValue.amount.length !== 1) { - throw new Error("Only MsgSend with one amount is supported"); - } - return { - kind: "bcp/send", - chainId: chainId, - sender: msgValue.from_address as Address, - recipient: msgValue.to_address as Address, - amount: decodeAmount(tokens, msgValue.amount[0]), - }; } export function parseFee(fee: types.StdFee, tokens: TokenInfos): Fee { diff --git a/packages/bcp/types/decode.d.ts b/packages/bcp/types/decode.d.ts index 2fb8dcab..80de6596 100644 --- a/packages/bcp/types/decode.d.ts +++ b/packages/bcp/types/decode.d.ts @@ -7,7 +7,6 @@ import { FullSignature, Nonce, PubkeyBundle, - SendTransaction, SignatureBytes, SignedTransaction, UnsignedTransaction, @@ -19,7 +18,7 @@ export declare function decodeSignature(signature: string): SignatureBytes; export declare function decodeFullSignature(signature: types.StdSignature, nonce: number): FullSignature; export declare function coinToDecimal(tokens: TokenInfos, coin: types.Coin): readonly [Decimal, string]; export declare function decodeAmount(tokens: TokenInfos, coin: types.Coin): Amount; -export declare function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): SendTransaction; +export declare function parseMsg(msg: types.Msg, chainId: ChainId, tokens: TokenInfos): UnsignedTransaction; export declare function parseFee(fee: types.StdFee, tokens: TokenInfos): Fee; export declare function parseTx( txValue: types.StdTx,