Shielding Tokens
The shield(...)
method allows a user to privately deposit (a.k.a. “shield”) tokens into their Shielder account. This is the entry point to private asset management.
When to Use
Call shield()
when a user wants to move tokens from a public wallet to a private Shielder account. This works for both native tokens and supported ERC-20s.
Method Signature
async shield(
token: Token, // either native or ERC-20 token
amount: bigint, // in wei
sendShielderTransaction: SendShielderTransaction,
from: `0x${string}` // public wallet address
): Promise<`0x${string}`> // tx hash
What it does
Computes and generates a zero-knowledge proof of deposit.
Sends a deposit transaction to the Shielder smart contract.
Stores your shielded note client-side and syncs local state.
Emits SDK callbacks like
onCalldataGenerated
,onCalldataSent
,onNewTransaction
etc.
Example: Shielding Native Token:
...
import { initializeShielderClient } from "./shielder";
import { nativeToken } from "@cardinal-cryptography/shielder-sdk";
import { userAddress, walletClient } from "./wallet";
const amountToShield = 1_000_000_000_000_000n; // 0.001 ETH in wei
(async () => {
const shielder = await initializeShielderClient();
const token = nativeToken();
const txHash = await shielder.shield(
token,
amountToShield,
async (txRequest) => {
// send transaction via viem or wagmi
return walletClient.sendTransaction(txRequest);
},
userAddress
);
console.log("Shield tx hash:", txHash);
})();
...
What Happens Under the Hood
Shielder SDK checks if user has an account (creates one if needed).
Generates ZK proof client-side using WASM cryptography engine.
Encodes and sends a deposit() transaction to the smart contract.
Stores new private note in local app storage (or your custom store).
Triggers callbacks like
onNewTransaction
.
Tips
Ensure the user approves token spending if shielding ERC-20s.
Always call await shielder.syncShielder() before or after to stay up to date.
Handle errors and show transaction progress in the UI via SDK callbacks.
Last updated
Was this helpful?