Skip to content

Commit

Permalink
refactor: matching cosmjs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
CalicoNino committed Oct 4, 2024
1 parent 7788673 commit 2e80b16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
31 changes: 12 additions & 19 deletions src/sdk/tx/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => ({
Expand All @@ -13,37 +14,29 @@ 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({
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([1, 2, 3]),
})

const account = accountFromEthAccount(ethAccount)
const account = accountFromEthAccount(baseAccount)

expect(account.address).toBe("nibi1testaddress")
expect(account.pubkey).toEqual({
Expand Down
14 changes: 6 additions & 8 deletions src/sdk/tx/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ 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.
*
* @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),
Expand All @@ -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)
Expand Down

0 comments on commit 2e80b16

Please sign in to comment.