import {
CLOAK_PROGRAM_ID,
NATIVE_SOL_MINT,
createUtxo,
createZeroUtxo,
fullWithdraw,
generateUtxoKeypair,
partialWithdraw,
transact,
} from "@cloak.dev/sdk";
import { Connection, Keypair } from "@solana/web3.js";
const connection = new Connection(
"https://api.mainnet-beta.solana.com",
"confirmed",
);
const signer = Keypair.fromSecretKey(/* Uint8Array secret key */);
const amount = 1_000_000_000n; // 1 SOL
const owner = await generateUtxoKeypair();
const depositOutput = await createUtxo(amount, owner, NATIVE_SOL_MINT);
const deposited = await transact(
{
inputUtxos: [await createZeroUtxo(NATIVE_SOL_MINT)],
outputUtxos: [depositOutput],
externalAmount: amount,
depositor: signer.publicKey,
},
{
connection,
programId: CLOAK_PROGRAM_ID,
depositorKeypair: signer,
walletPublicKey: signer.publicKey,
},
);
const recipient = Keypair.generate().publicKey;
// Full withdrawal: sends all spendable value externally.
await fullWithdraw(deposited.outputUtxos, recipient, {
connection,
programId: CLOAK_PROGRAM_ID,
depositorKeypair: signer,
walletPublicKey: signer.publicKey,
cachedMerkleTree: deposited.merkleTree,
});
// OR partial withdrawal: withdraw a portion, keep private change in shielded state.
// await partialWithdraw(deposited.outputUtxos, recipient, 200_000_000n, {
// connection,
// programId: CLOAK_PROGRAM_ID,
// depositorKeypair: signer,
// walletPublicKey: signer.publicKey,
// cachedMerkleTree: deposited.merkleTree,
// });