|
| 1 | +import { struct, u8 } from '@solana/buffer-layout'; |
| 2 | +import { bool, publicKey } from '@solana/buffer-layout-utils'; |
| 3 | +import { PublicKey, TransactionInstruction } from '@solana/web3.js'; |
| 4 | +import { programSupportsExtensions, TOKEN_2022_PROGRAM_ID } from '../../constants.js'; |
| 5 | +import { TokenUnsupportedInstructionError } from '../../errors.js'; |
| 6 | +import { TokenInstruction } from '../../instructions/types.js'; |
| 7 | +import { elgamalPublicKey } from './elgamal.js'; |
| 8 | +import { PodElGamalPubkey } from '@solana/zk-sdk'; |
| 9 | + |
| 10 | +export enum ConfidentialTransferInstruction { |
| 11 | + InitializeMint = 0, |
| 12 | + UpdateMint = 1, |
| 13 | +} |
| 14 | + |
| 15 | +export interface InitializeMintData { |
| 16 | + instruction: TokenInstruction.ConfidentialTransferExtension; |
| 17 | + confidentialTransferInstruction: ConfidentialTransferInstruction.InitializeMint; |
| 18 | + confidentialTransferMintAuthority: PublicKey | null; |
| 19 | + autoApproveNewAccounts: boolean; |
| 20 | + auditorElGamalPubkey: PodElGamalPubkey | null; |
| 21 | +} |
| 22 | + |
| 23 | +export const initializeMintData = struct<InitializeMintData>([ |
| 24 | + u8('instruction'), |
| 25 | + u8('confidentialTransferInstruction'), |
| 26 | + publicKey('confidentialTransferMintAuthority'), |
| 27 | + bool('autoApproveNewAccounts'), |
| 28 | + elgamalPublicKey('auditorElGamalPubkey'), |
| 29 | +]); |
| 30 | + |
| 31 | +/** |
| 32 | + * Construct an InitializeConfidentialTransferInitializeMint instruction |
| 33 | + * |
| 34 | + * @param mint Token mint account |
| 35 | + * @param confidentialTransferMintAuthority Authority that can update confidential transfer mint |
| 36 | + * @param autoApproveNewAccounts Auto-approve account policy |
| 37 | + * @param auditorElGamalPubkey Optional auditor ElGamal public key |
| 38 | + * @param programId SPL Token program account |
| 39 | + * |
| 40 | + * @return Instruction to add to a transaction |
| 41 | + */ |
| 42 | +export function createConfidentialTransferInitializeMintInstruction( |
| 43 | + mint: PublicKey, |
| 44 | + confidentialTransferMintAuthority: PublicKey | null, |
| 45 | + autoApproveNewAccounts: boolean, |
| 46 | + auditorElGamalPubkey: PodElGamalPubkey | null, |
| 47 | + programId = TOKEN_2022_PROGRAM_ID, |
| 48 | +): TransactionInstruction { |
| 49 | + if (!programSupportsExtensions(programId)) { |
| 50 | + throw new TokenUnsupportedInstructionError(); |
| 51 | + } |
| 52 | + const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; |
| 53 | + |
| 54 | + const data = Buffer.alloc(initializeMintData.span); |
| 55 | + |
| 56 | + initializeMintData.encode( |
| 57 | + { |
| 58 | + instruction: TokenInstruction.ConfidentialTransferExtension, |
| 59 | + confidentialTransferInstruction: ConfidentialTransferInstruction.InitializeMint, |
| 60 | + confidentialTransferMintAuthority: confidentialTransferMintAuthority ?? PublicKey.default, |
| 61 | + autoApproveNewAccounts: autoApproveNewAccounts, |
| 62 | + auditorElGamalPubkey: auditorElGamalPubkey ?? PodElGamalPubkey.zeroed(), |
| 63 | + }, |
| 64 | + data, |
| 65 | + ); |
| 66 | + |
| 67 | + return new TransactionInstruction({ keys, programId, data }); |
| 68 | +} |
| 69 | + |
| 70 | +export interface UpdateMintData { |
| 71 | + instruction: TokenInstruction.ConfidentialTransferExtension; |
| 72 | + confidentialTransferInstruction: ConfidentialTransferInstruction.UpdateMint; |
| 73 | + autoApproveNewAccounts: boolean; |
| 74 | + auditorElGamalPubkey: PodElGamalPubkey | null; |
| 75 | +} |
| 76 | + |
| 77 | +export const updateMintData = struct<UpdateMintData>([ |
| 78 | + u8('instruction'), |
| 79 | + u8('confidentialTransferInstruction'), |
| 80 | + bool('autoApproveNewAccounts'), |
| 81 | + elgamalPublicKey('auditorElGamalPubkey'), |
| 82 | +]); |
| 83 | + |
| 84 | +/** |
| 85 | + * Construct an UpdateMint instruction |
| 86 | + * |
| 87 | + * @param mint Token mint account |
| 88 | + * @param confidentialTransferMintAuthority Authority that can update confidential transfer mint |
| 89 | + * @param autoApproveNewAccounts New auto-approve account policy |
| 90 | + * @param auditorElGamalPubkey New auditor ElGamal public key |
| 91 | + * @param programId SPL Token program account |
| 92 | + * |
| 93 | + * @return Instruction to add to a transaction |
| 94 | + */ |
| 95 | +export function createConfidentialTransferUpdateMintInstruction( |
| 96 | + mint: PublicKey, |
| 97 | + confidentialTransferMintAuthority: PublicKey, |
| 98 | + autoApproveNewAccounts: boolean, |
| 99 | + auditorElGamalPubkey: PodElGamalPubkey | null, |
| 100 | + programId = TOKEN_2022_PROGRAM_ID, |
| 101 | +): TransactionInstruction { |
| 102 | + if (!programSupportsExtensions(programId)) { |
| 103 | + throw new TokenUnsupportedInstructionError(); |
| 104 | + } |
| 105 | + const keys = [ |
| 106 | + { pubkey: mint, isSigner: false, isWritable: true }, |
| 107 | + { pubkey: confidentialTransferMintAuthority, isSigner: true, isWritable: false }, |
| 108 | + ]; |
| 109 | + |
| 110 | + const data = Buffer.alloc(updateMintData.span); |
| 111 | + updateMintData.encode( |
| 112 | + { |
| 113 | + instruction: TokenInstruction.ConfidentialTransferExtension, |
| 114 | + confidentialTransferInstruction: ConfidentialTransferInstruction.UpdateMint, |
| 115 | + autoApproveNewAccounts: autoApproveNewAccounts, |
| 116 | + auditorElGamalPubkey: auditorElGamalPubkey ?? PodElGamalPubkey.zeroed(), |
| 117 | + }, |
| 118 | + data, |
| 119 | + ); |
| 120 | + |
| 121 | + return new TransactionInstruction({ keys, programId, data }); |
| 122 | +} |
0 commit comments