Skip to content

Instantly share code, notes, and snippets.

@tedim52
Last active March 6, 2026 01:28
Show Gist options
  • Select an option

  • Save tedim52/5b5323419010472b78a1096dbf495e3d to your computer and use it in GitHub Desktop.

Select an option

Save tedim52/5b5323419010472b78a1096dbf495e3d to your computer and use it in GitHub Desktop.
Revert 7702 authorization with Privy
import {randomUUID} from 'node:crypto';
import {PrivyClient, type AuthorizationContext} from '@privy-io/node';
import {createPublicClient, http} from 'viem';
import {mainnet} from 'viem/chains'; // replace with your chain
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' as const;
const privy = new PrivyClient({
 appId: process.env.PRIVY_APP_ID!,
 appSecret: process.env.PRIVY_APP_SECRET!,
});
type Input = {
 walletId: string;
 walletAddress: 0x${string};
 chainId: number;
};
export async function maybeRevert7702Delegation({
 walletId,
 walletAddress,
 chainId,
}: Input): Promise<{
 reverted: boolean;
 txHash: 0x${string} | null;
 code: 0x${string};
 authorizedImplementationAddress: 0x${string} | null;
 reason?: string;
}> {
 // Query chain with viem to see if wallets already delegated
 const publicClient = createPublicClient({
   chain: mainnet, // replace with your chain
   transport: http('your RPC URL here'),
 });
 const address = walletAddress; // the EOA address here
 const code = (await publicClient.getCode({address}))?.toLowerCase() ?? '0x';
 const prefixIndex = code.indexOf('0xef0100');
 const authorizedImplementationAddress =
   prefixIndex === -1
     ? null
     : (0x${code.slice(prefixIndex + 8, prefixIndex + 48)} as 0x${string});
 // Only revert if delegation is actually present.
 if (
   authorizedImplementationAddress === null ||
   authorizedImplementationAddress.toLowerCase() === ZERO_ADDRESS
 ) {
   return {
     reverted: false,
     txHash: null,
     code: code as 0x${string},
     authorizedImplementationAddress,
     reason: 'No active EIP-7702 delegation found; skipping revert.',
   };
 }
 // 1) Sign 7702 authorization to zero address (revoke delegation)
 const {authorization} = await privy.wallets().ethereum().sign7702Authorization(walletId, {
   params: {
     contract: ZERO_ADDRESS,
     chain_id: chainId,
   },
   authorization_context: authorizationContext,
   idempotency_key: randomUUID(),
 });
 // 2) Send type-4 tx carrying authorization_list
 const {hash} = await privy.wallets().ethereum().sendTransaction(walletId, {
   caip2: eip155:${chainId},
   params: {
     transaction: {
       from: walletAddress,
       to: walletAddress,
       value: '0x0',
       data: '0x',
       chain_id: chainId,
       type: 4,
       authorization_list: [authorization],
     },
   },
   authorization_context: authorizationContext, // create auth context based on who owns wallet
   idempotency_key: randomUUID(),
 });
return {
   reverted: true,
   txHash: hash as 0x${string},
   code: code as 0x${string},
   authorizedImplementationAddress,
 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment