# 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

<pre class="language-typescript"><code class="lang-typescript"><strong>async shield(
</strong>  token: Token, // either native or ERC-20 token
  amount: bigint, // in wei
  sendShielderTransaction: SendShielderTransaction,
  from: `0x${string}` // public wallet address
): Promise&#x3C;`0x${string}`> // tx hash
</code></pre>

### 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:

{% tabs %}
{% tab title="src/App.tsx" %}
{% code title="src/App.tsx" %}

```typescript
...

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);
})();

...
```

{% endcode %}
{% endtab %}

{% tab title="src/wallet.ts" %}
{% code title="src/wallet.ts" %}

```typescript
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";

const account = privateKeyToAccount(
  import.meta.env.VITE_PRIVATE_KEY as `0x${string}` // Replace with your private key
);

export const walletClient = createWalletClient({
  chain: baseSepolia,
  transport: http(),
  account,
});

export const userAddress = account.address;

```

{% endcode %}
{% endtab %}
{% endtabs %}

### What Happens Under the Hood

1. Shielder SDK checks if user has an account (creates one if needed).
2. Generates ZK proof client-side using WASM cryptography engine.
3. Encodes and sends a deposit() transaction to the smart contract.
4. Stores new private note in local app storage (or your custom store).
5. 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.common.fi/knowledge-base/shielding-tokens.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
