diff --git a/src/sdk/tx/account.test.ts b/src/sdk/tx/account.test.ts index 562bccc5..d392b404 100644 --- a/src/sdk/tx/account.test.ts +++ b/src/sdk/tx/account.test.ts @@ -4,6 +4,7 @@ import { Any } from "src/protojs/google/protobuf/any" import Long from "long" import * as cosmjs from "@cosmjs/stargate" import { decodeOptionalPubkey } from "@cosmjs/proto-signing" +import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" // Mock decodeOptionalPubkey jest.mock("@cosmjs/proto-signing", () => ({ @@ -13,29 +14,21 @@ jest.mock("@cosmjs/proto-signing", () => ({ const mockedDecodeOptionalPubkey = decodeOptionalPubkey as jest.Mock describe("accountFromEthAccount", () => { - it("should throw an error when baseAccount is undefined", () => { - const ethAccount: EthAccount = { - baseAccount: undefined, // Simulating undefined baseAccount - codeHash: "", - } + it("should throw an error if baseAccount is undefined", () => { + const baseAccount: BaseAccount = undefined as unknown as BaseAccount - expect(() => accountFromEthAccount(ethAccount)).toThrowError( - "baseAccount is undefined in EthAccount" - ) + expect(() => accountFromEthAccount(baseAccount)).toThrow() }) it("should return a valid account when baseAccount is defined", () => { - const ethAccount: EthAccount = { - baseAccount: { - address: "nibi1testaddress", - pubKey: { - typeUrl: "/cosmos.crypto.secp256k1.PubKey", - value: new Uint8Array([1, 2, 3]), - }, - accountNumber: Long.fromNumber(123), - sequence: Long.fromNumber(1), + const baseAccount: BaseAccount = { + address: "nibi1testaddress", + pubKey: { + typeUrl: "/cosmos.crypto.secp256k1.PubKey", + value: new Uint8Array([1, 2, 3]), }, - codeHash: "", + accountNumber: Long.fromNumber(123), + sequence: Long.fromNumber(1), } mockedDecodeOptionalPubkey.mockReturnValue({ @@ -43,7 +36,7 @@ describe("accountFromEthAccount", () => { value: new Uint8Array([1, 2, 3]), }) - const account = accountFromEthAccount(ethAccount) + const account = accountFromEthAccount(baseAccount) expect(account.address).toBe("nibi1testaddress") expect(account.pubkey).toEqual({ diff --git a/src/sdk/tx/account.ts b/src/sdk/tx/account.ts index b0410e71..cf3a2f40 100644 --- a/src/sdk/tx/account.ts +++ b/src/sdk/tx/account.ts @@ -2,6 +2,8 @@ import { decodeOptionalPubkey } from "@cosmjs/proto-signing" import { Account, accountFromAny, AccountParser } from "@cosmjs/stargate" import { EthAccount } from "src/protojs/eth/types/v1/account" import { Any } from "src/protojs/google/protobuf/any" +import { assert } from "@cosmjs/utils" +import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" /** * Converts an EthAccount to a general Cosmos Account object. @@ -9,13 +11,8 @@ import { Any } from "src/protojs/google/protobuf/any" * @param {EthAccount} ethAccount - The EthAccount object containing the account's base information. * @returns {Account} The Cosmos account object. */ -export const accountFromEthAccount = ({ baseAccount }: EthAccount): Account => { - if (!baseAccount) { - throw new Error("baseAccount is undefined in EthAccount") - } - +export const accountFromEthAccount = (baseAccount: BaseAccount): Account => { const { address, pubKey, accountNumber, sequence } = baseAccount - return { address, pubkey: decodeOptionalPubkey(pubKey), @@ -34,8 +31,9 @@ export const accountFromNibiru: AccountParser = (input: Any): Account => { const { typeUrl, value } = input if (typeUrl === "/eth.types.v1.EthAccount") { - const ethAccount = EthAccount.decode(value) - return accountFromEthAccount(ethAccount) + const baseAccount = EthAccount.decode(value).baseAccount + assert(baseAccount) + return accountFromEthAccount(baseAccount) } return accountFromAny(input)