-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
470262f
commit de60207
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,83 @@ | ||
import { accountFromEthAccount, accountFromNibiru } from "./account" | ||
import { EthAccount } from "src/protojs/eth/types/v1/account" | ||
import { Any } from "cosmjs-types/google/protobuf/any" | ||
import Long from "long" | ||
|
||
describe("accountFromEthAccount", () => { | ||
it("should return valid account information with nibi address and Long types", () => { | ||
const ethAccount: EthAccount = { | ||
baseAccount: { | ||
address: "nibi1234567", // Use nibi Bech32 encoding | ||
pubKey: { | ||
typeUrl: "/cosmos.crypto.secp256k1.PubKey", | ||
value: new Uint8Array([1, 2, 3]), | ||
}, | ||
accountNumber: Long.fromNumber(123), | ||
sequence: Long.fromNumber(1), | ||
}, | ||
codeHash: "", | ||
} | ||
|
||
const account = accountFromEthAccount(ethAccount) | ||
|
||
expect(account.address).toEqual("nibi1234567") | ||
expect(account.pubkey).toEqual({ | ||
typeUrl: "/cosmos.crypto.secp256k1.PubKey", | ||
value: new Uint8Array([1, 2, 3]), | ||
}) | ||
expect(account.accountNumber).toEqual(Long.fromNumber(123)) | ||
expect(account.sequence).toEqual(Long.fromNumber(1)) | ||
}) | ||
|
||
it("should handle null/undefined baseAccount fields and return Long.ZERO", () => { | ||
const ethAccount: EthAccount = { baseAccount: undefined, codeHash: "" } | ||
|
||
const account = accountFromEthAccount(ethAccount) | ||
|
||
expect(account.address).toEqual("") | ||
expect(account.pubkey).toBeNull() | ||
expect(account.accountNumber).toEqual(Long.ZERO) | ||
expect(account.sequence).toEqual(Long.ZERO) | ||
}) | ||
}) | ||
|
||
describe("accountFromNibiru", () => { | ||
it("should parse EthAccount typeUrl", () => { | ||
const input: Any = { | ||
typeUrl: "/eth.types.v1.EthAccount", | ||
value: EthAccount.encode({ | ||
baseAccount: { | ||
address: "nibi1234567", | ||
pubKey: { | ||
typeUrl: "/cosmos.crypto.secp256k1.PubKey", | ||
value: new Uint8Array([4, 5, 6]), | ||
}, | ||
accountNumber: Long.fromNumber(456), | ||
sequence: Long.fromNumber(2), | ||
}, | ||
codeHash: "", | ||
}).finish(), | ||
} | ||
|
||
const account = accountFromNibiru(input) | ||
|
||
expect(account.address).toEqual("nibi1234567") // Ensure the prefix is 'nibi' | ||
expect(account.pubkey).toEqual({ | ||
typeUrl: "/cosmos.crypto.secp256k1.PubKey", | ||
value: new Uint8Array([4, 5, 6]), | ||
}) | ||
expect(account.accountNumber).toEqual(Long.fromNumber(456)) | ||
expect(account.sequence).toEqual(Long.fromNumber(2)) | ||
}) | ||
|
||
it("should call accountFromAny for non-EthAccount typeUrl", () => { | ||
const input: Any = { | ||
typeUrl: "/other.types.v1.Account", | ||
value: new Uint8Array([7, 8, 9]), | ||
} | ||
|
||
const account = accountFromNibiru(input) | ||
|
||
expect(account.address).toBe("nibi1otheraddress") | ||
}) | ||
}) |
This file contains 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,36 @@ | ||
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" | ||
|
||
/** | ||
* 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 => ({ | ||
address: baseAccount?.address ?? "", | ||
pubkey: decodeOptionalPubkey(baseAccount?.pubKey) ?? null, | ||
accountNumber: baseAccount?.accountNumber?.toNumber() ?? 0, | ||
sequence: baseAccount?.sequence?.toNumber() ?? 0, | ||
}) | ||
|
||
/** | ||
* 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 ethAccount = EthAccount.decode(value) | ||
return accountFromEthAccount(ethAccount) | ||
} | ||
|
||
return accountFromAny(input) | ||
} |
This file contains 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
This file contains 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,41 @@ | ||
{ | ||
"chainId": "cataclysm-1", | ||
"chainName": "cataclysm-1", | ||
"rpc": "https://rpc.nibiru.fi", | ||
"rest": "https://lcd.nibiru.fi", | ||
"stakeCurrency": { | ||
"coinDenom": "NIBI", | ||
"coinMinimalDenom": "unibi", | ||
"coinDecimals": 6 | ||
}, | ||
"bip44": { | ||
"coinType": 118 | ||
}, | ||
"bech32Config": { | ||
"bech32PrefixAccAddr": "nibi", | ||
"bech32PrefixAccPub": "nibipub", | ||
"bech32PrefixValAddr": "nibivaloper", | ||
"bech32PrefixValPub": "nibivaloperpub", | ||
"bech32PrefixConsAddr": "nibivalcons", | ||
"bech32PrefixConsPub": "nibivalconspub" | ||
}, | ||
"currencies": [ | ||
{ | ||
"coinDenom": "NIBI", | ||
"coinMinimalDenom": "unibi", | ||
"coinDecimals": 6 | ||
} | ||
], | ||
"feeCurrencies": [ | ||
{ | ||
"coinDenom": "NIBI", | ||
"coinMinimalDenom": "unibi", | ||
"coinDecimals": 6, | ||
"gasPriceStep": { | ||
"low": 0.05, | ||
"average": 0.125, | ||
"high": 0.2 | ||
} | ||
} | ||
] | ||
} |