From a2a61df0293a29ba9855efea692681dbd530a2bd Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Thu, 19 Dec 2024 12:53:34 +1300 Subject: [PATCH] fix: Add validation for getContract params (#5780) Co-authored-by: MananTank --- .changeset/shy-bats-eat.md | 5 +++++ packages/thirdweb/src/contract/contract.ts | 16 ++++++++++++++++ .../src/wallets/smart/smart-wallet-dev.test.ts | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/shy-bats-eat.md diff --git a/.changeset/shy-bats-eat.md b/.changeset/shy-bats-eat.md new file mode 100644 index 00000000000..243e7f0b8a2 --- /dev/null +++ b/.changeset/shy-bats-eat.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Validate getContract params diff --git a/packages/thirdweb/src/contract/contract.ts b/packages/thirdweb/src/contract/contract.ts index af8258a2976..bfe8d7a1720 100644 --- a/packages/thirdweb/src/contract/contract.ts +++ b/packages/thirdweb/src/contract/contract.ts @@ -1,6 +1,7 @@ import type { Abi } from "abitype"; import type { Chain } from "../chains/types.js"; import type { ThirdwebClient } from "../client/client.js"; +import { isAddress } from "../utils/address.js"; /** * @contract @@ -42,5 +43,20 @@ export type ThirdwebContract = Readonly< export function getContract( options: ContractOptions, ): ThirdwebContract { + if (!options.client) { + throw new Error( + `getContract validation error - invalid client: ${options.client}`, + ); + } + if (!isAddress(options.address)) { + throw new Error( + `getContract validation error - invalid address: ${options.address}`, + ); + } + if (!options.chain || !options.chain.id) { + throw new Error( + `getContract validation error - invalid chain: ${options.chain}`, + ); + } return options; } diff --git a/packages/thirdweb/src/wallets/smart/smart-wallet-dev.test.ts b/packages/thirdweb/src/wallets/smart/smart-wallet-dev.test.ts index 6b5e69a7fb2..8733f1330ee 100644 --- a/packages/thirdweb/src/wallets/smart/smart-wallet-dev.test.ts +++ b/packages/thirdweb/src/wallets/smart/smart-wallet-dev.test.ts @@ -1,5 +1,6 @@ import { beforeAll, describe, expect, it } from "vitest"; import { TEST_CLIENT } from "../../../test/src/test-clients.js"; +import { arbitrumSepolia } from "../../chains/chain-definitions/arbitrum-sepolia.js"; import { type ThirdwebContract, getContract } from "../../contract/contract.js"; import { balanceOf } from "../../extensions/erc1155/__generated__/IERC1155/read/balanceOf.js"; import { claimTo } from "../../extensions/erc1155/drops/write/claimTo.js"; @@ -13,7 +14,6 @@ import type { Account, Wallet } from "../interfaces/wallet.js"; import { generateAccount } from "../utils/generateAccount.js"; import { smartWallet } from "./smart-wallet.js"; let wallet: Wallet; -import { arbitrumSepolia } from "../../chains/chain-definitions/arbitrum-sepolia.js"; let smartAccount: Account; let smartWalletAddress: Address;