Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests enhancements #1047

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/connect/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ export default {
coverageProvider: "v8",
verbose: true,
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "html"],
testMatch: ["<rootDir>/**/*.test.ts", "<rootDir>/**/*.test.tsx"],
testMatch: ["<rootDir>/src/**/*.test.ts", "<rootDir>/src/**/*.test.tsx"],
maxWorkers: "50%",
setupFilesAfterEnv: ["<rootDir>/jest.setup.tsx"],
testEnvironment: "jsdom",
transform: {
".(ts|tsx)": "ts-jest",
"^.+\\.(t|j)sx?$": "@swc/jest",
},
moduleNameMapper: {
"@env": "<rootDir>/src/env/index.ts",
"uuid": require.resolve('uuid'),
uuid: require.resolve("uuid"),
},
collectCoverageFrom: [
"<rootDir>/src/**/*.{ts,tsx,js,jsx}",
Expand Down
4 changes: 3 additions & 1 deletion apps/connect/jest.setup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "@testing-library/jest-dom";

import { TextEncoder, TextDecoder } from "util";
import React from "react";

window.React = global.React = React;

Object.assign(global, {
TextDecoder,
Expand Down
30 changes: 30 additions & 0 deletions apps/connect/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"react-router-dom": "^6.24.0"
},
"devDependencies": {
"@swc/jest": "^0.2.36",
"@testing-library/dom": "^10.3.1",
"@testing-library/jest-dom": "^6.4.6",
"@testing-library/react": "^16.0.0",
Expand Down
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);
});
});
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
2 changes: 1 addition & 1 deletion apps/connect/src/utils/transferVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ValidateTransferResult,
} from "node_modules/@wormhole-foundation/wormhole-connect/lib/src/config/types";
import { ChainName } from "@certusone/wormhole-sdk";
import { isValidAddress } from "./validAddress";
import { isValidAddress } from "./isValidAddress";
import { isSanctionedAddress } from "../../src/providers/sanctions";

export const validateTransfer = async (
Expand Down
Loading