Shield NFTs

Shield one or many ERC-721 tokens into RAILGUN private balances in a single transaction. ERC-1155 not supported at this time.

Gas Estimate

import {
  RailgunNFTAmountRecipient,
  NetworkName,
  NFTTokenType
} from '@railgun-community/shared-models';
import {
  gasEstimateForShield,
  getShieldPrivateKeySignatureMessage,
} from '@railgun-community/wallet';
import { keccak256, Wallet } from 'ethers';

// Formatted NFT amounts and recipients.
const nftAmountRecipients: RailgunNFTAmountRecipient[] = [
  {
    nftAddress: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', 
    tokenSubID: '135', // tokenID of NFT
    amount: 1n, // shield amount - always 1n for ERC-721
    nftTokenType: NFTTokenType.ERC721,
    recipientAddress: '0zk987...654', // RAILGUN address
  },
];

// The shieldPrivateKey enables the sender to decrypt 
// the receiver's address in the future.
const wallet = new Wallet(pKey);
const shieldSignatureMessage = getShieldPrivateKeySignatureMessage();
const shieldPrivateKey = keccak256(
  await wallet.signMessage(shieldSignatureMessage),
);

// Public wallet to shield from.
const fromWalletAddress = '0xab5...c9b';

const { gasEstimate } = await gasEstimateForShield(
  NetworkName.Ethereum,
  shieldPrivateKey,
  [], // tokenAmountRecipients,  
  nftAmountRecipients,
  fromWalletAddress,
);

Populate Transaction

import {
  ...
  EVMGasType,
  TransactionGasDetails,
  getEVMGasTypeForTransaction
} from '@railgun-community/shared-models';
import {
  ...
  populateShield,
} from '@railgun-community/wallet';

...

const sendWithPublicWallet = true; // Always true for Shield transactions
const evmGasType: EVMGasType = getEVMGasTypeForTransaction(
  NetworkName.Ethereum,
  sendWithPublicWallet
);
const gasEstimate = ...; // Output from gasEstimateForShield in above example

let gasDetails: TransactionGasDetails;
switch (evmGasType) {
  case EVMGasType.Type0:
  case EVMGasType.Type1:
    gasDetails = {
      evmGasType,
      gasEstimate,
      gasPrice: BigInt('0x100000'), // Proper calculation of network gasPrice is not covered in this guide
    }
    break;
  case EVMGasType.Type2:
    // Proper calculation of gas Max Fee and gas Max Priority Fee is not covered in this guide. See: https://docs.alchemy.com/docs/how-to-build-a-gas-fee-estimator-using-eip-1559
    const maxFeePerGas: BigInt('0x100000');
    const maxPriorityFeePerGas: BigInt('0x010000');

    gasDetails = {
      evmGasType,
      gasEstimate,
      maxFeePerGas,
      maxPriorityFeePerGas,
    }
    break;
}

const { transaction } = await populateShield(
  NetworkName.Ethereum,
  shieldPrivateKey,
  [], // tokenAmountRecipients
  nftAmountRecipients,
  gasDetails,
);

// Public wallet to shield from.
transaction.from = '0xab5...c9b';

Last updated