|
| 1 | +import { accountFromEthAccount, accountFromNibiru } from "./account" |
| 2 | +import { EthAccount } from "src/protojs/eth/types/v1/account" |
| 3 | +import { Any } from "src/protojs/google/protobuf/any" |
| 4 | +import Long from "long" |
| 5 | +import * as cosmjs from "@cosmjs/stargate" |
| 6 | +import { decodeOptionalPubkey } from "@cosmjs/proto-signing" |
| 7 | +import { BaseAccount } from "src/protojs/cosmos/auth/v1beta1/auth" |
| 8 | + |
| 9 | +// Mock decodeOptionalPubkey |
| 10 | +jest.mock("@cosmjs/proto-signing", () => ({ |
| 11 | + decodeOptionalPubkey: jest.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +const mockedDecodeOptionalPubkey = decodeOptionalPubkey as jest.Mock |
| 15 | + |
| 16 | +describe("accountFromEthAccount", () => { |
| 17 | + it("should throw an error if baseAccount is undefined", () => { |
| 18 | + const baseAccount: BaseAccount = undefined as unknown as BaseAccount |
| 19 | + |
| 20 | + expect(() => accountFromEthAccount(baseAccount)).toThrow() |
| 21 | + }) |
| 22 | + |
| 23 | + it("should return a valid account when baseAccount is defined", () => { |
| 24 | + const baseAccount: BaseAccount = { |
| 25 | + address: "nibi1testaddress", |
| 26 | + pubKey: { |
| 27 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 28 | + value: new Uint8Array([1, 2, 3]), |
| 29 | + }, |
| 30 | + accountNumber: Long.fromNumber(123), |
| 31 | + sequence: Long.fromNumber(1), |
| 32 | + } |
| 33 | + |
| 34 | + mockedDecodeOptionalPubkey.mockReturnValue({ |
| 35 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 36 | + value: new Uint8Array([1, 2, 3]), |
| 37 | + }) |
| 38 | + |
| 39 | + const account = accountFromEthAccount(baseAccount) |
| 40 | + |
| 41 | + expect(account.address).toBe("nibi1testaddress") |
| 42 | + expect(account.pubkey).toEqual({ |
| 43 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 44 | + value: new Uint8Array([1, 2, 3]), |
| 45 | + }) |
| 46 | + expect(account.accountNumber).toEqual(123) |
| 47 | + expect(account.sequence).toEqual(1) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +describe("accountFromNibiru", () => { |
| 52 | + it("should parse EthAccount typeUrl and return valid account", () => { |
| 53 | + const input: Any = { |
| 54 | + typeUrl: "/eth.types.v1.EthAccount", |
| 55 | + value: EthAccount.encode({ |
| 56 | + baseAccount: { |
| 57 | + address: "nibi1testaddress", |
| 58 | + pubKey: { |
| 59 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 60 | + value: new Uint8Array([4, 5, 6]), |
| 61 | + }, |
| 62 | + accountNumber: Long.fromNumber(456), |
| 63 | + sequence: Long.fromNumber(2), |
| 64 | + }, |
| 65 | + codeHash: "", |
| 66 | + }).finish(), |
| 67 | + } |
| 68 | + |
| 69 | + mockedDecodeOptionalPubkey.mockReturnValue({ |
| 70 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 71 | + value: new Uint8Array([4, 5, 6]), |
| 72 | + }) |
| 73 | + |
| 74 | + const account = accountFromNibiru(input) |
| 75 | + |
| 76 | + expect(account.address).toBe("nibi1testaddress") |
| 77 | + expect(account.pubkey).toEqual({ |
| 78 | + typeUrl: "/cosmos.crypto.secp256k1.PubKey", |
| 79 | + value: new Uint8Array([4, 5, 6]), |
| 80 | + }) |
| 81 | + expect(account.accountNumber).toEqual(456) |
| 82 | + expect(account.sequence).toEqual(2) |
| 83 | + }) |
| 84 | + |
| 85 | + it("should handle non-EthAccount typeUrl by calling accountFromAny", () => { |
| 86 | + const mockAccountFromAny = jest |
| 87 | + .spyOn(cosmjs, "accountFromAny") |
| 88 | + .mockReturnValue({ |
| 89 | + address: "nibi1otheraddress", |
| 90 | + pubkey: null, |
| 91 | + accountNumber: 789, |
| 92 | + sequence: 3, |
| 93 | + }) |
| 94 | + |
| 95 | + const input: Any = { |
| 96 | + typeUrl: "/other.types.v1.Account", |
| 97 | + value: new Uint8Array([7, 8, 9]), |
| 98 | + } |
| 99 | + |
| 100 | + const account = accountFromNibiru(input) |
| 101 | + |
| 102 | + expect(account.address).toBe("nibi1otheraddress") |
| 103 | + expect(mockAccountFromAny).toHaveBeenCalledWith(input) |
| 104 | + }) |
| 105 | +}) |
0 commit comments