Skip to content

Commit

Permalink
feat: account parser (#374)
Browse files Browse the repository at this point in the history
* chore: develop -> main (#370)

* revert: cosmos submodule only (#362)

* revert: cosmos submodule only

* fix: rem

* fix: rem

* fix: update

* feat: add msg client

* fix: paths

* fix: try chaosnet ibc

* fix: path again

* fix: try hm

* fix: fixes to pass

* feat: eth protos (#366)

* fix: eth protos

* fix: client

* fix: fixes

* fix: try older nibiru

* fix: index

* fix: mainnet

* fix: import

* revert: build change

* chore: tests (#367)

* fix: all query tests

* chore: final tests

* fix: buf

* fix: fix

* fix: pull latest

* fix: build

* fix: build

* refactor(faucet)!: set default tokens (#369)

* chore: develop -> main (#368)

* revert: cosmos submodule only (#362)

* revert: cosmos submodule only

* fix: rem

* fix: rem

* fix: update

* feat: add msg client

* fix: paths

* fix: try chaosnet ibc

* fix: path again

* fix: try hm

* fix: fixes to pass

* feat: eth protos (#366)

* fix: eth protos

* fix: client

* fix: fixes

* fix: try older nibiru

* fix: index

* fix: mainnet

* fix: import

* revert: build change

* chore: tests (#367)

* fix: all query tests

* chore: final tests

* fix: buf

* fix: fix

* fix: pull latest

* fix: build

* fix: build

* chore(release): 4.5.1

### [4.5.1](v4.5.0...v4.5.1) (2024-08-09)

### Miscellaneous Chores

* develop -> main ([#368](#368)) ([c6d6570](c6d6570)), closes [#362](#362) [#366](#366) [#367](#367)

 [skip ci]

* fix(faucet): remove unused tokens from default faucet request

* fix: bump test

---------

Co-authored-by: Cameron Gilbert <gilbertjcameron@gmail.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>

---------

Co-authored-by: Kevin Yang <5478483+k-yang@users.noreply.github.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>

* chore(github): Add project automation for https://tinyurl.com/25uty9w5

* chore(release): 4.5.2

### [4.5.2](v4.5.1...v4.5.2) (2024-09-24)

### Miscellaneous Chores

* develop -> main ([#370](#370)) ([ec2a25b](ec2a25b)), closes [#362](#362) [#366](#366) [#367](#367) [#369](#369) [#368](#368) [#362](#362) [#366](#366) [#367](#367) [#362](#362) [#366](#366) [#367](#367)
* **github:** Add project automation for https://tinyurl.com/25uty9w5 ([c2c27e5](c2c27e5))

 [skip ci]

* feat: nibiru account parser

* refactor: throw if baseaccount is undefined

* test: fixing tests

* chore: removing unnecessary ?

* refactor: matching cosmjs implementation

* chore: removing t.json

* chore: pr comments

---------

Co-authored-by: Cameron Gilbert <gilbertjcameron@gmail.com>
Co-authored-by: Kevin Yang <5478483+k-yang@users.noreply.github.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
  • Loading branch information
5 people authored Oct 9, 2024
1 parent 49b0766 commit d7e324d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/sdk/tx/account.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { accountFromEthAccount, accountFromNibiru } from "./account"
import { EthAccount } from "src/protojs/eth/types/v1/account"
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", () => ({
decodeOptionalPubkey: jest.fn(),
}))

const mockedDecodeOptionalPubkey = decodeOptionalPubkey as jest.Mock

describe("accountFromEthAccount", () => {
it("should throw an error if baseAccount is undefined", () => {
const baseAccount: BaseAccount = undefined as unknown as BaseAccount

expect(() => accountFromEthAccount(baseAccount)).toThrow()
})

it("should return a valid account when baseAccount is defined", () => {
const baseAccount: BaseAccount = {
address: "nibi1testaddress",
pubKey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([1, 2, 3]),
},
accountNumber: Long.fromNumber(123),
sequence: Long.fromNumber(1),
}

mockedDecodeOptionalPubkey.mockReturnValue({
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([1, 2, 3]),
})

const account = accountFromEthAccount(baseAccount)

expect(account.address).toBe("nibi1testaddress")
expect(account.pubkey).toEqual({
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([1, 2, 3]),
})
expect(account.accountNumber).toEqual(123)
expect(account.sequence).toEqual(1)
})
})

describe("accountFromNibiru", () => {
it("should parse EthAccount typeUrl and return valid account", () => {
const input: Any = {
typeUrl: "/eth.types.v1.EthAccount",
value: EthAccount.encode({
baseAccount: {
address: "nibi1testaddress",
pubKey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([4, 5, 6]),
},
accountNumber: Long.fromNumber(456),
sequence: Long.fromNumber(2),
},
codeHash: "",
}).finish(),
}

mockedDecodeOptionalPubkey.mockReturnValue({
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([4, 5, 6]),
})

const account = accountFromNibiru(input)

expect(account.address).toBe("nibi1testaddress")
expect(account.pubkey).toEqual({
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: new Uint8Array([4, 5, 6]),
})
expect(account.accountNumber).toEqual(456)
expect(account.sequence).toEqual(2)
})

it("should handle non-EthAccount typeUrl by calling accountFromAny", () => {
const mockAccountFromAny = jest
.spyOn(cosmjs, "accountFromAny")
.mockReturnValue({
address: "nibi1otheraddress",
pubkey: null,
accountNumber: 789,
sequence: 3,
})

const input: Any = {
typeUrl: "/other.types.v1.Account",
value: new Uint8Array([7, 8, 9]),
}

const account = accountFromNibiru(input)

expect(account.address).toBe("nibi1otheraddress")
expect(mockAccountFromAny).toHaveBeenCalledWith(input)
})
})
42 changes: 42 additions & 0 deletions src/sdk/tx/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 = ({
address,
pubKey,
accountNumber,
sequence,
}: BaseAccount): Account => ({
address,
pubkey: decodeOptionalPubkey(pubKey),
accountNumber: accountNumber.toNumber(),
sequence: sequence.toNumber(),
})

/**
* Parses an account input into a Cosmos account. Handles both EthAccount and other standard accounts.
*
* @param {Any} input - The input account information, containing the typeUrl and value.
* @returns {Account} Parsed account object.
*/
export const accountFromNibiru: AccountParser = (input: Any): Account => {
const { typeUrl, value } = input

if (typeUrl === "/eth.types.v1.EthAccount") {
const baseAccount = EthAccount.decode(value).baseAccount
assert(baseAccount)
return accountFromEthAccount(baseAccount)
}

return accountFromAny(input)
}
2 changes: 2 additions & 0 deletions src/sdk/tx/txClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setupWasmExtension,
} from "@cosmjs/cosmwasm-stargate"
import { NibiruExtensions, setupNibiruExtension } from ".."
import { accountFromNibiru } from "./account"

export const nibiruRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
...defaultRegistryTypes,
Expand Down Expand Up @@ -69,6 +70,7 @@ export class NibiruTxClient extends SigningStargateClient {
registry: new Registry(nibiruRegistryTypes),
gasPrice: GasPrice.fromString("0.025unibi"),
broadcastPollIntervalMs: 1_000, // 1 second poll times
accountParser: accountFromNibiru,
...options,
},
wasmClient
Expand Down

0 comments on commit d7e324d

Please sign in to comment.