Skip to content

Commit

Permalink
add confidential transfer UpdateMint
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Apr 16, 2024
1 parent fd54300 commit ab8cad1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
28 changes: 28 additions & 0 deletions token/js/src/extensions/confidentialTransfer/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js';
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js';
import { TOKEN_2022_PROGRAM_ID } from '../../constants.js';
import { createConfidentialTransferUpdateMintInstruction } from './instructions.js';
import { PodElGamalPubkey } from 'solana-zk-token-sdk-experimental';

Check failure on line 5 in token/js/src/extensions/confidentialTransfer/actions.ts

View workflow job for this annotation

GitHub Actions / js-test (token)

All imports in the declaration are only used as types. Use `import type`

export async function updateMint(
connection: Connection,
payer: Signer,
mint: PublicKey,
autoApproveNewAccounts: boolean,
auditorElGamalPubkey: PodElGamalPubkey | null,
authority: Signer,
confirmOptions?: ConfirmOptions,
programId = TOKEN_2022_PROGRAM_ID
): Promise<TransactionSignature> {
const transaction = new Transaction().add(
createConfidentialTransferUpdateMintInstruction(
mint,
authority.publicKey,
autoApproveNewAccounts,
auditorElGamalPubkey,
programId
)
);

return await sendAndConfirmTransaction(connection, transaction, [payer, authority], confirmOptions);
}
1 change: 1 addition & 0 deletions token/js/src/extensions/confidentialTransfer/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './actions.js';
export * from './state.js';
export * from './instructions.js';
44 changes: 44 additions & 0 deletions token/js/src/extensions/confidentialTransfer/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { elgamalPublicKey } from './elgamal.js';

export enum ConfidentialTransferInstruction {
InitializeMint = 0,
UpdateMint = 1,
}

export interface InitializeMintData {
Expand Down Expand Up @@ -54,3 +55,46 @@ export function createConfidentialTransferInitializeMintInstruction(

return new TransactionInstruction({ keys, programId, data });
}

export interface UpdateMintData {
instruction: TokenInstruction.ConfidentialTransferExtension;
confidentialTransferInstruction: ConfidentialTransferInstruction.UpdateMint;
autoApproveNewAccounts: boolean;
auditorElGamalPubkey: PodElGamalPubkey | null;
}

export const updateMintData = struct<UpdateMintData>([
u8('instruction'),
u8('confidentialTransferInstruction'),
bool('autoApproveNewAccounts'),
elgamalPublicKey('auditorElGamalPubkey'),
]);

export function createConfidentialTransferUpdateMintInstruction(
mint: PublicKey,
confidentialTransferMintAuthority: PublicKey,
autoApproveNewAccounts: boolean,
auditorElGamalPubkey: PodElGamalPubkey | null,
programId = TOKEN_2022_PROGRAM_ID
): TransactionInstruction {
if (!programSupportsExtensions(programId)) {
throw new TokenUnsupportedInstructionError();
}
const keys = [
{ pubkey: mint, isSigner: false, isWritable: true },
{ pubkey: confidentialTransferMintAuthority, isSigner: true, isWritable: false },
];

const data = Buffer.alloc(updateMintData.span);
updateMintData.encode(
{
instruction: TokenInstruction.ConfidentialTransferExtension,
confidentialTransferInstruction: ConfidentialTransferInstruction.UpdateMint,
autoApproveNewAccounts: autoApproveNewAccounts,
auditorElGamalPubkey: auditorElGamalPubkey ?? PodElGamalPubkey.default(),
},
data
);

return new TransactionInstruction({ keys, programId, data });
}
24 changes: 24 additions & 0 deletions token/js/test/e2e-2022/confidentialTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../commo
import {
createConfidentialTransferInitializeMintInstruction,
getConfidentialTransferMint,
updateMint,
} from '../../src/extensions/confidentialTransfer/index';

const TEST_TOKEN_DECIMALS = 2;
Expand Down Expand Up @@ -85,5 +86,28 @@ describe('confidentialTransfer', () => {
expect(confidentialTransferMint.auditorElGamalPubkey.equals(auditorPubkey)); // TODO: equals?
}
});

it('mint updates', async () => {
const newAutoApproveNewAccounts = false;
const newAuditorElGamalKeypair = ElGamalKeypair.new_rand();
const newAuditorElGamalPubkey = PodElGamalPubkey.encoded(newAuditorElGamalKeypair.pubkey_owned());

await updateMint(
connection,
payer,
mint,
newAutoApproveNewAccounts,
newAuditorElGamalPubkey,
confidentialTransferMintAuthority
);

const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID);
const confidentialTransferMint = getConfidentialTransferMint(mintInfo);
expect(confidentialTransferMint).to.not.be.null;
if (confidentialTransferMint !== null) {
expect(confidentialTransferMint.autoApproveNewAccounts).to.eql(newAutoApproveNewAccounts);
expect(confidentialTransferMint.auditorElGamalPubkey.equals(newAuditorElGamalPubkey));
}
});
});
});

0 comments on commit ab8cad1

Please sign in to comment.