Typescript
Token2022 avec Web3.js

Token2022 avec Web3.js

L'Extension Cpi Guard

L'extension CpiGuard (garde CPI) est une extension de compte de Token qui interdit certaines actions dans les Invocations de Programme Croisé, protégeant ainsi les utilisateurs contre les programmes malveillants qui pourraient tenter de manipuler leurs Comptes de Jeton sans leur consentement explicite.

Initialisation du Compte de Jeton

Pour initialiser l'extension CpiGuard sur un compte de Token nous allons avoir besoin de la fonction enableCpiGuard().

Voici comment créer un compte de jeton avec l'extension CpiGuard :

ts
import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeAccountInstruction,
    createEnableCpiGuardInstruction,
    getAccountLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
 
const tokenAccount = Keypair.generate();
 
// Calculate the size needed for a Token account with Cpi Guard extension
const accountLen = getAccountLen([ExtensionType.CpiGuard]);
 
// Calculate minimum lamports required for rent exemption
const lamports = await connection.getMinimumBalanceForRentExemption(accountLen);
 
// Create the account with the correct size and owner
const createAccountInstruction = SystemProgram.createAccount({
    fromPubkey: keypair.publicKey,
    newAccountPubkey: tokenAccount.publicKey,
    space: accountLen,
    lamports,
    programId: TOKEN_2022_PROGRAM_ID,
});
 
// Initialize the Cpi Guard extension
const enableCpiGuardInstruction = createEnableCpiGuardInstruction(
    tokenAccount.publicKey,
    keypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
// Initialize the Token account itself
const initializeAccountInstruction = createInitializeAccountInstruction(
    tokenAccount.publicKey,
    mint.publicKey,
    keypair.publicKey,
    TOKEN_2022_PROGRAM_ID,
);
 
const transaction = new Transaction().add(
    createAccountInstruction,
    initializeAccountInstruction,
    enableCpiGuardInstruction
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, tokenAccount], {skipPreflight: false});
 
console.log(`Token accounts created! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Désactivation du CPI Guard

Lorsque nous voulons activer certains des comportements bloqués par le Cpi guard, nous pouvons facilement le désactiver à l'aide de l'instruction disableCpiGuard comme ceci :

ts
const disableCpiGuardInstruction = createDisableCpiGuardInstruction(
    tokenAccount,
    keypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);

Et lorsque nous avons terminé et que nous voulons réactiver la couche de sécurité, nous pouvons le faire à l'aide de l'instruction enableCpiGuard comme ceci :

ts
const enableCpiGuardInstruction = createEnableCpiGuardInstruction(
    tokenAccount,
    keypair.publicKey,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
Blueshift © 2025Commit: 6d01265