sdk/js: change sei array conversion

This commit is contained in:
Paul Noel 2023-08-31 10:03:08 -05:00 committed by Paul Noel
parent fa9c8bf72e
commit ede5c5bc1e
2 changed files with 52 additions and 7 deletions

View File

@ -33,6 +33,33 @@ test("wormchain address conversion", () => {
expect(tryNativeToHexString(human, "wormchain")).toBe(canonical);
});
test("wormchain address conversion2", () => {
const human =
"wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh";
const canonical =
"aeb534c45c3049d380b9d9b966f9895f53abd4301bfaff407fa09dea8ae7a924";
const native = tryUint8ArrayToNative(
new Uint8Array(Buffer.from(canonical, "hex")),
"wormchain"
);
expect(native).toBe(human);
expect(tryNativeToHexString(human, "wormchain")).toBe(canonical);
});
test("wormchain address conversion no leading 0s", () => {
const human = "wormhole1yre8d0ek4vp0wjlec407525zjctq7t32z930fp";
const canonical = "20f276bf36ab02f74bf9c55fea2a8296160f2e2a";
const native = tryUint8ArrayToNative(
new Uint8Array(Buffer.from(canonical, "hex")),
"wormchain"
);
expect(native).toBe(human);
// Can't do the reverse because the supplied canonical does not have leading 0s
// expect(tryNativeToHexString(human, "wormchain")).toBe(canonical);
});
test("injective address conversion", () => {
const human = "inj180rl9ezc4389t72pc3vvlkxxs5d9jx60w9eeu3";
const canonical = canonicalAddress(human);
@ -40,3 +67,15 @@ test("injective address conversion", () => {
const native = tryUint8ArrayToNative(lpadCanonical, "injective");
expect(native).toBe(human);
});
test("sei address conversion", () => {
const human =
"sei189adguawugk3e55zn63z8r9ll29xrjwca636ra7v7gxuzn98sxyqwzt47l";
const canonical =
"397ad473aee22d1cd2829ea2238cbffa8a61c9d8eea3a1f7ccf20dc14ca78188";
const native = tryUint8ArrayToNative(
new Uint8Array(Buffer.from(canonical, "hex")),
"sei"
);
expect(native).toBe(human);
});

View File

@ -57,7 +57,7 @@ import { isValidSuiType } from "../sui";
*/
export const isHexNativeTerra = (h: string): boolean => h.startsWith("01");
const isLikely20ByteTerra = (h: string): boolean =>
const isLikely20ByteCosmwasm = (h: string): boolean =>
h.startsWith("000000000000000000000000");
export const nativeTerraHexToDenom = (h: string): string =>
@ -93,23 +93,29 @@ export const tryUint8ArrayToNative = (
if (isHexNativeTerra(h)) {
return nativeTerraHexToDenom(h);
} else {
if (chainId === CHAIN_ID_TERRA2 && !isLikely20ByteTerra(h)) {
if (chainId === CHAIN_ID_TERRA2 && !isLikely20ByteCosmwasm(h)) {
// terra 2 has 32 byte addresses for contracts and 20 for wallets
return humanAddress("terra", a);
}
return humanAddress("terra", a.slice(-20));
}
} else if (chainId === CHAIN_ID_INJECTIVE) {
return humanAddress("inj", a.slice(-20));
const h = uint8ArrayToHex(a);
return humanAddress("inj", isLikely20ByteCosmwasm(h) ? a.slice(-20) : a);
} else if (chainId === CHAIN_ID_ALGORAND) {
return uint8ArrayToNativeStringAlgorand(a);
} else if (chainId == CHAIN_ID_WORMCHAIN) {
// wormchain addresses are always 20 bytes.
return humanAddress("wormhole", a.slice(-20));
const h = uint8ArrayToHex(a);
return humanAddress(
"wormhole",
isLikely20ByteCosmwasm(h) ? a.slice(-20) : a
);
} else if (chainId === CHAIN_ID_XPLA) {
return humanAddress("xpla", a.slice(-20));
const h = uint8ArrayToHex(a);
return humanAddress("xpla", isLikely20ByteCosmwasm(h) ? a.slice(-20) : a);
} else if (chainId === CHAIN_ID_SEI) {
return humanAddress("sei", a.slice(-20));
const h = uint8ArrayToHex(a);
return humanAddress("sei", isLikely20ByteCosmwasm(h) ? a.slice(-20) : a);
} else if (chainId === CHAIN_ID_NEAR) {
throw Error("uint8ArrayToNative: Use tryHexToNativeStringNear instead.");
} else if (chainId === CHAIN_ID_OSMOSIS) {