A shield moves public tokens into a private note for a 0zk address. A shield needs no proof and no prover.
shield() asks the signer to sign the shield key message, approves the token, sends the shield, and parses the receipt.
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { base } from 'viem/chains'
const signer = createWalletClient({
account: privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`),
chain: base,
transport: http()
})
const result = await client.shield({
tokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
amount: 1_000_000n,
recipient: wallet.railgunAddress,
signer,
approvalMode: 'exact',
onProgress: ({ stage, txHash }) => console.log(stage, txHash)
}, 8453)
await client.recordShield(walletId, 8453, result, new Date())
The note carries the full amount. The contract takes the shield fee on chain.
recordShield adds the shield to the transaction history of the wallet. The
shielded note becomes visible after the next sync().
await client.shield({
tokenAddress: '0x...',
tokenType: 'ERC721',
tokenSubID: 42n,
recipient: wallet.railgunAddress,
signer
}, 8453)
buildShield() returns an unsigned transaction and makes no network call. Derive the shield private key first, then approve, estimate gas, sign, and send with your own tools.
import { deriveShieldPrivateKey } from '@railgun-reloaded/wallet-sdk/node'
const shieldPrivateKey = await deriveShieldPrivateKey(signer)
const { transaction } = await client.buildShield({
tokenAddress: '0x...',
amount: 1_000_000n,
recipient: wallet.railgunAddress,
shieldPrivateKey
}, 8453)
When the signature comes from another system, use shieldPrivateKeyFromSignature() with the signature of "@railgun-reloaded/wallet-sdk/node".SHIELD_PRIVATE_KEY_SIGNATURE_MESSAGE.
A BaseToken shield wraps and shields the native coin in one type-4
transaction through RelayAdapt7702. It needs an ephemeral signer. See
"@railgun-reloaded/wallet-sdk/node".BuildShieldBaseTokenParams and
allocateEphemeralIndex().