Creating Policy Scripts
Comprehensive guide to creating and managing policy scripts for minting tokens on Cardano using the Anvil API.
Last updated
Was this helpful?
Comprehensive guide to creating and managing policy scripts for minting tokens on Cardano using the Anvil API.
Last updated
Was this helpful?
Policy scripts are crucial components when minting tokens on Cardano. They define who has permission to mint or burn assets under a specific policy ID. This guide will walk you through creating policy scripts using the Anvil API's utility endpoints.
The process of creating and using policy scripts with the Anvil API follows these key steps:
Parse Wallet Address - obtain key hash(es) for signature requirements (supports multisig with multiple addresses)
Convert Date to Slot - create a time constraint (optional but recommended)
Create JSON Policy Script - combine key hash(s) and time constraints
Convert to Native Script - use CSL to create a machine-readable format
Generate Policy ID - derive the unique identifier for your assets
Use in Transaction - include the policy when minting tokens
An Anvil API key
A new dedicated private wallet for your token collection (preferred) or an existing wallet address that will be used for signing, minting, and burning transactions
Security Best Practice: When creating policy scripts, it's recommended to generate a dedicated private wallet specifically for each collection rather than using an existing wallet address. Using a wallet address in your policy script means that wallet must sign every minting transaction, which creates security risks if building a minting service as it would require giving the service access to your wallet's signing capabilities.
The Anvil API provides several utility endpoints to assist with creating and managing policy scripts:
/utils/addresses/parse
Parses an address and returns its payment and stake credentials
/utils/native-scripts/get-info
Returns information about a provided native script (e.g., policy ID)
/utils/network/time-to-slot
Converts a date/time to a Cardano slot number
/utils/network/slot-to-time
Converts a Cardano slot number to a date/time
To create a signature-based policy script, you first need to obtain the key hash of the wallet that will sign the minting transactions. You can get this by parsing a wallet address using the address parsing endpoint:
The payment
value from the response is what you'll use as the keyHash
in your policy script.
For NFT collections, adding a time constraint ensures your collection becomes immutable after a certain date. To add a time constraint, you need to convert a future date to a Cardano slot number using the time-to-slot endpoint:
This slot number will be used in your policy script to create a time-limited minting policy. While this step is optional, it's highly recommended for NFT collections to ensure they cannot be minted indefinitely.
Now that you have both the key hash (from Step 1) and optionally a slot number for time constraint (from Step 2), you can create the JSON structure for your policy script.
Policy scripts in the Anvil API follow a specific JSON structure that maps to Cardano's native script format. Each script uses a discriminated union with a type
field that determines the script's behavior.
Here's how to combine the signature requirement and time constraint into a complete policy script:
This JSON structure creates a policy that requires both the signature from the specified key hash AND the transaction to be submitted before the specified slot number.
While the JSON representation is useful for understanding, to actually use the policy script on the Cardano blockchain, you need to convert it to a native script format using the Cardano Serialization Library (CSL).
What is CSL? The Cardano Serialization Library (CSL) is a collection of tools that allows developers to interact with the Cardano blockchain at a low level. It provides functions to create, serialize, and deserialize Cardano data structures, including native scripts used for policy scripts.
This function converts the conceptual JSON policy script from Step 3 into a real native script object. Here's how you would use it with the values obtained in previous steps:
Once you have created the native script, you can generate the policy ID. This unique identifier will be associated with all tokens minted under this policy.
You can get the policy ID by using the Anvil API endpoint:
The final step is to use your policy script when building transactions that mint or burn assets. You'll include the policy script in the preloadedScripts
section of your transaction request:
TODO: Update payload based on changes made to the metadata
This transaction, when signed and submitted, will mint a new asset under the policy ID you generated.
Creating policy scripts is a fundamental step in minting tokens on Cardano. By using the Anvil API utility endpoints, you can easily construct policy scripts with various constraints to match your project's requirements.
Remember to consider security implications when designing your policy scripts:
For NFT collections, using a time-limited policy provides authenticity and immutability.
For fungible tokens that may require future minting, consider using a multi-signature approach without time constraints.
Always keep your signing keys secure, as they are crucial for maintaining control over your minting ability.
Use dedicated policy keys only for signing minting and burning operations, not for other transactions.
Consider creating a separate treasury wallet to collect ADA from sales, keeping it separate from minting operations.
Understanding of and their role in minting tokens