-
Notifications
You must be signed in to change notification settings - Fork 8
feat: account parser #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: account parser #374
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ec2a25b
chore: develop -> main (#370)
cgilbe27 c2c27e5
chore(github): Add project automation for https://tinyurl.com/25uty9w5
Unique-Divine 470262f
chore(release): 4.5.2
semantic-release-bot de60207
feat: nibiru account parser
CalicoNino 0b44443
Merge branch 'develop' into feat/account-parser
CalicoNino b948462
refactor: throw if baseaccount is undefined
CalicoNino 46ea0ab
test: fixing tests
CalicoNino 7788673
chore: removing unnecessary ?
CalicoNino 2e80b16
refactor: matching cosmjs implementation
CalicoNino 1b81bf6
Merge branch 'develop' into feat/account-parser
cgilbe27 2fa390e
chore: removing t.json
CalicoNino ec9bdca
chore: pr comments
CalicoNino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.