Skip to content

Instantly share code, notes, and snippets.

@warengonzaga
Created September 27, 2023 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warengonzaga/9fd73682bac95d26eb0cacca0910aaaa to your computer and use it in GitHub Desktop.
Save warengonzaga/9fd73682bac95d26eb0cacca0910aaaa to your computer and use it in GitHub Desktop.
Recover Ownership of Contract (thirdweb)

Recover Ownership of Contract

This is useful when your admin for your contract has been compromised. For example, when you add balance to your admin address and suddenly your admin balance goes to random address. It is painful to remove your admin address if you don't have a gas to spend to assign a new admin address.

Setup

  1. Do npx thirdweb create app --node --javascript --evm
  2. Rename the .env.example to .env
  3. Copy and paste this to your .env
WALLET_PRIVATE_KEY=
THIRDWEB_SECRET_KEY=
RELAYER_URL=
  1. After that, fill out the information needed.
  2. Get the private key of compromised wallet and paste it as a value to WALLET_PRIVATE_KEY=
  3. Get the secret key from this https://thirdweb.com/create-api-key
  4. Get the relayer_url from this tutorial https://blog.thirdweb.com/guides/setup-gasless-transactions
  5. Copy and paste the index.js to your local index.js
  6. Complete the info in the variables.
const newAdmin = "0x...";
const contractAddress = "0x...";
  1. Run the code by doing node index.js

Usage

  1. After the setup, run the script straight. node index.js
  2. Comment this part by adding // in front of it.
const tx = await contract.roles.grant("admin", newAdmin);
  1. Then, uncomment this part. By removing the // in front of it.
const tx = await contract.owner.set(newAdmin);
  1. Run again the script. node 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment