How to Recover the Ownership of Your thirdweb Pre-built Contract
Learn how to recover the ownership of your thirdweb pre-built smart contract if your admin wallet address has been compromised.

A Filipino self-taught software engineer, open-source maintainer, cybersecurity researcher, tech community builder, Web3 enthusiast, and technopreneur from the Philippines. ๐ป๐โ
This is useful if the admin of your contract is compromised. For example, if you add a balance to your admin wallet address and it unexpectedly gets transferred to a random address, changing it becomes challenging without gas to assign a new one.
In this guide, you'll learn how to change your admin wallet to a new one without needing to spend gas from your compromised wallet address, using the thirdweb gasless method. Without further ado let's get started! ๐
Setup
Do
npx thirdweb create app --node --javascript --evmRename the
.env.exampleto.envCopy and paste this to your
.env
WALLET_PRIVATE_KEY=
THIRDWEB_SECRET_KEY=
RELAYER_URL=
After that, fill out the information needed.
Get the private key of the compromised wallet and paste it as a value to
WALLET_PRIVATE_KEY=Get the
secret keyfrom this https://thirdweb.com/create-api-keyGet the
relayer_urlfrom this tutorial https://blog.thirdweb.com/guides/setup-gasless-transactionsCopy and paste the code below to your local
index.js
import { config } from "dotenv";
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
config();
const newAdmin = "0x...";
const contractAddress = "0x...";
const main = async () => {
if (!process.env.WALLET_PRIVATE_KEY) {
throw new Error("No private key found!");
}
try {
const sdk = ThirdwebSDK.fromPrivateKey(process.env.WALLET_PRIVATE_KEY, 'polygon', {
gasless: {
openzeppelin: {
relayerUrl: process.env.RELAYER_URL, // Learn more: https://blog.thirdweb.com/guides/setup-gasless-transactions/
},
},
secretKey: process.env.THIRDWEB_SECRET_KEY,
});
const contract = await sdk.getContract(contractAddress);
// run this first so you can set it as owner.
const tx = await contract.roles.grant("admin", newAdmin);
// Uncomment this, and comment the code above ^ after you set the newAdmin as admin.
// const tx = await contract.owner.set(newAdmin);
console.log(tx);
} catch(error) {
console.error("Something went wrong: ", error);
}
};
main();
- Fill in the information for the variables.
const newAdmin = "0x...";
const contractAddress = "0x...";
- Run the code by doing
node index.js
Usage
After the setup, run the script straight.
node index.jsComment on this part by adding
//in front of it.
const tx = await contract.roles.grant("admin", newAdmin);
- Then, uncomment this part. By removing the
//in front of it.
const tx = await contract.owner.set(newAdmin);
- Run again the script.
node index.js
That's it! โจ See you on my next blog... ๐





