sdk/js: Move address functions to cosmos package
`canonicalAddress` and `humanAddress` are useful functions for all cosmos chains so move it into a new cosmos package. This also means we need to stop hardcoding "terra" in `humanAddress` and take the human readable part as a parameter instead.
This commit is contained in:
parent
e0fd3e788f
commit
13a18d2f7e
|
@ -0,0 +1,8 @@
|
||||||
|
import { bech32 } from "bech32";
|
||||||
|
|
||||||
|
export function canonicalAddress(humanAddress: string) {
|
||||||
|
return new Uint8Array(bech32.fromWords(bech32.decode(humanAddress).words));
|
||||||
|
}
|
||||||
|
export function humanAddress(hrp: string, canonicalAddress: Uint8Array) {
|
||||||
|
return bech32.encode(hrp, bech32.toWords(canonicalAddress));
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
export * from "./address";
|
|
@ -1,3 +1,4 @@
|
||||||
|
export * from "./cosmos";
|
||||||
export * from "./ethers-contracts";
|
export * from "./ethers-contracts";
|
||||||
export * from "./solana";
|
export * from "./solana";
|
||||||
export * from "./terra";
|
export * from "./terra";
|
||||||
|
@ -6,6 +7,7 @@ export * from "./utils";
|
||||||
export * from "./bridge";
|
export * from "./bridge";
|
||||||
export * from "./token_bridge";
|
export * from "./token_bridge";
|
||||||
|
|
||||||
|
export * as cosmos from "./cosmos";
|
||||||
export * as ethers_contracts from "./ethers-contracts";
|
export * as ethers_contracts from "./ethers-contracts";
|
||||||
export * as solana from "./solana";
|
export * as solana from "./solana";
|
||||||
export * as terra from "./terra";
|
export * as terra from "./terra";
|
||||||
|
|
|
@ -1,12 +1,4 @@
|
||||||
import { zeroPad } from "@ethersproject/bytes";
|
import { zeroPad } from "@ethersproject/bytes";
|
||||||
import { bech32 } from "bech32";
|
|
||||||
|
|
||||||
export function canonicalAddress(humanAddress: string) {
|
|
||||||
return new Uint8Array(bech32.fromWords(bech32.decode(humanAddress).words));
|
|
||||||
}
|
|
||||||
export function humanAddress(canonicalAddress: Uint8Array) {
|
|
||||||
return bech32.encode("terra", bech32.toWords(canonicalAddress));
|
|
||||||
}
|
|
||||||
|
|
||||||
// from https://github.com/terra-money/station/blob/dca7de43958ce075c6e46605622203b9859b0e14/src/lib/utils/is.ts#L12
|
// from https://github.com/terra-money/station/blob/dca7de43958ce075c6e46605622203b9859b0e14/src/lib/utils/is.ts#L12
|
||||||
export const isNativeTerra = (string = "") =>
|
export const isNativeTerra = (string = "") =>
|
||||||
|
|
|
@ -7,7 +7,8 @@ import { decodeLocalState } from "../algorand";
|
||||||
import { buildTokenId } from "../cosmwasm/address";
|
import { buildTokenId } from "../cosmwasm/address";
|
||||||
import { TokenImplementation__factory } from "../ethers-contracts";
|
import { TokenImplementation__factory } from "../ethers-contracts";
|
||||||
import { importTokenWasm } from "../solana/wasm";
|
import { importTokenWasm } from "../solana/wasm";
|
||||||
import { buildNativeId, canonicalAddress, isNativeDenom } from "../terra";
|
import { buildNativeId, isNativeDenom } from "../terra";
|
||||||
|
import { canonicalAddress } from "../cosmos";
|
||||||
import {
|
import {
|
||||||
ChainId,
|
ChainId,
|
||||||
ChainName,
|
ChainName,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { zeroPad } from "ethers/lib/utils";
|
import { zeroPad } from "ethers/lib/utils";
|
||||||
import { canonicalAddress } from "../terra";
|
|
||||||
import { tryUint8ArrayToNative } from "./array";
|
import { tryUint8ArrayToNative } from "./array";
|
||||||
|
import { canonicalAddress } from "../cosmos";
|
||||||
|
|
||||||
test("terra address conversion", () => {
|
test("terra address conversion", () => {
|
||||||
const human = "terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v";
|
const human = "terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v";
|
||||||
|
|
|
@ -6,8 +6,9 @@ import {
|
||||||
nativeStringToHexAlgorand,
|
nativeStringToHexAlgorand,
|
||||||
uint8ArrayToNativeStringAlgorand,
|
uint8ArrayToNativeStringAlgorand,
|
||||||
} from "../algorand";
|
} from "../algorand";
|
||||||
|
import { canonicalAddress, humanAddress } from "../cosmos"
|
||||||
import { buildTokenId } from "../cosmwasm/address";
|
import { buildTokenId } from "../cosmwasm/address";
|
||||||
import { canonicalAddress, humanAddress, isNativeDenom } from "../terra";
|
import { isNativeDenom } from "../terra";
|
||||||
import {
|
import {
|
||||||
ChainId,
|
ChainId,
|
||||||
ChainName,
|
ChainName,
|
||||||
|
@ -82,9 +83,9 @@ export const tryUint8ArrayToNative = (
|
||||||
} else {
|
} else {
|
||||||
if (chainId === CHAIN_ID_TERRA2 && !isLikely20ByteTerra(h)) {
|
if (chainId === CHAIN_ID_TERRA2 && !isLikely20ByteTerra(h)) {
|
||||||
// terra 2 has 32 byte addresses for contracts and 20 for wallets
|
// terra 2 has 32 byte addresses for contracts and 20 for wallets
|
||||||
return humanAddress(a);
|
return humanAddress("terra", a);
|
||||||
}
|
}
|
||||||
return humanAddress(a.slice(-20));
|
return humanAddress("terra", a.slice(-20));
|
||||||
}
|
}
|
||||||
} else if (chainId === CHAIN_ID_ALGORAND) {
|
} else if (chainId === CHAIN_ID_ALGORAND) {
|
||||||
return uint8ArrayToNativeStringAlgorand(a);
|
return uint8ArrayToNativeStringAlgorand(a);
|
||||||
|
|
Loading…
Reference in New Issue