Skip to content

Commit

Permalink
added tests for isValidAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
danielisaacgeslin committed Aug 30, 2024
1 parent cb760f6 commit fda34b1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 33 deletions.
89 changes: 89 additions & 0 deletions apps/connect/src/utils/isValidAddress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { isValidAddress } from "./isValidAddress";

jest.mock("aptos", () => {
class AptosClient {
getAccount = jest
.fn()
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(false)
.mockRejectedValue("error");
}
return { AptosClient };
});

describe("isValidAddress", () => {
it("should check solana addresses", async () => {
expect(
await isValidAddress(
"FuX2KgSnaPDyQMfCi6pdGYYrWQTkTtfC5obykSTDU4pS",
"solana"
)
).toEqual(true);
expect(await isValidAddress("invalid", "solana")).toEqual(false);
});

it("should check aptos addresses", async () => {
expect(
await isValidAddress(
"FuX2KgSnaPDyQMfCi6pdGYYrWQTkTtfC5obykSTDU4pS",
"solana"
)
).toEqual(true);
expect(await isValidAddress("valid", "aptos")).toEqual(true);
expect(await isValidAddress("invalid", "aptos")).toEqual(false);
expect(await isValidAddress("error", "aptos")).toEqual(false);
});

it("should check sui addresses", async () => {
expect(
await isValidAddress(
"0x02a212de6a9dfa3a69e22387acfbafbb1a9e591bd9d636e7895dcfc8de05f331",
"sui"
)
).toEqual(true);
expect(
await isValidAddress(
"0x02a212de6a9dfa3a69e22387acfbafbb1a9e591bd9d636e7895dcfc8de05f3",
"sui"
)
).toEqual(false);
expect(await isValidAddress("invalid", "sui")).toEqual(false);
});

it("should check cosmos addresses", async () => {
expect(await isValidAddress("0x02a21223de05f3", "evmos")).toEqual(false);
expect(
await isValidAddress(
"0x00726B9BfE72607E1A94492eA50814e073df2BC3",
"evmos"
)
).toEqual(true);
expect(await isValidAddress("invalid", "injective")).toEqual(false);
expect(
await isValidAddress(
"inj10n6q5cxrjperasknfsh77ge3fkpsamfzw4jhdq",
"injective"
)
).toEqual(true);
});

it("should check evm addresses", async () => {
expect(await isValidAddress("0x02a21223de05f3", "ethereum")).toEqual(false);
expect(
await isValidAddress(
"0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326",
"ethereum"
)
).toEqual(true);
expect(await isValidAddress(undefined as any, "ethereum")).toEqual(false);
});

it("should check unknown chains addresses", async () => {
expect(
await isValidAddress(
"FuX2KgSnaPDyQMfCi6pdGYYrWQTkTtfC5obykSTDU4pS",
"unknown" as any
)
).toEqual(false);
});
});
51 changes: 18 additions & 33 deletions apps/connect/src/utils/isValidAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ export const isValidAddress = async (
address: string,
chain: ChainName
): Promise<boolean> => {
if (isEVMChain(chain)) {
return isValidEthereumAddress(address);
} else if (chain === "solana") {
return isValidSolanaAddress(address);
} else if (chain === "aptos") {
return isValidAptosAddress(address);
} else if (chain === "sui") {
return isValidSuiAddress(address);
} else if (isCosmWasmChain(chain)) {
return isValidCosmosAddress(address, chain);
}
if (isEVMChain(chain)) return isValidEthereumAddress(address);
if (chain === "solana") return isValidSolanaAddress(address);
if (chain === "aptos") return isValidAptosAddress(address);
if (chain === "sui") return isValidSuiAddress(address);
if (isCosmWasmChain(chain)) return isValidCosmosAddress(address, chain);

return false;
};

Expand All @@ -34,28 +29,19 @@ const isValidEthereumAddress = (address: string, strict = false): boolean => {
// We need to ensure the address contains the checksum
try {
const addressWithChecksum = getEthereumAddressWithChecksum(address);
if (strict) {
return address === addressWithChecksum;
}
if (strict) return address === addressWithChecksum;
return address.toLowerCase() === addressWithChecksum.toLocaleLowerCase();
} catch (e) {
const typedError = e as { reason?: string };
if (
typedError.reason === "invalid address" ||
typedError.reason === "bad address checksum" ||
typedError.reason === "bad icap checksum"
) {
return false;
}
return !/^(invalid address|bad address checksum|bad icap checksum)$/.test(
(e as { reason: string }).reason
);
}
return false;
};

// Solana Validation
const isValidSolanaAddress = (address: string): boolean => {
try {
const decoded = base58.decode(address);
return decoded.length === 32;
return base58.decode(address).length === 32;
} catch (e) {
return false;
}
Expand All @@ -82,14 +68,13 @@ const isValidCosmosAddress = (address: string, chain: ChainName) => {
if (chain === "evmos" && address.startsWith("0x")) {
// For Evmos hex address case https://docs.evmos.org/protocol/concepts/accounts#address-formats-for-clients
return isValidEthereumAddress(address);
} else {
// For Beach32 encode case https://docs.cosmos.network/v0.47/build/spec/addresses/bech32
try {
const decoded = bech32.decode(address);
return PREFIXES[chain] === decoded.prefix && !!decoded.words?.length;
} catch {
return false;
}
}
// For Beach32 encode case https://docs.cosmos.network/v0.47/build/spec/addresses/bech32
try {
const decoded = bech32.decode(address);
return PREFIXES[chain] === decoded.prefix && !!decoded.words?.length;
} catch {
return false;
}
};

Expand Down

0 comments on commit fda34b1

Please sign in to comment.