Skip to content

Commit

Permalink
sdk/js: change sei array conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Sep 5, 2023
1 parent fa9c8bf commit ede5c5b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
39 changes: 39 additions & 0 deletions sdk/js/src/utils/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,49 @@ 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);
const lpadCanonical = zeroPad(canonical, 32);
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);
});
20 changes: 13 additions & 7 deletions sdk/js/src/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ede5c5b

Please sign in to comment.