Minting Contract
This section will guide you through implementing a component for minting tokens with user input validation and signature verification.
Objective
This tutorial aims to create a minting dApp that utilizes Concordium's ID layer. Users must prove they are older than 18 to mint a token.
Approach
Instead of directly accessing ID proofs from the smart contract, you'll implement the logic using the verifier backend server. This approach ensures that users can only mint tokens if they provide proof of being older than 18 through the verifier backend server.
Here's how it will work:
Owner Setup:
The owner of the verifier backend server and the smart contract instance (dApp owner) are assumed to be the same.
The owner uses an account's sign and verify keys when setting up the verifier backend server.
Contract Initialization:
When creating a new contract instance, the owner sends their verify key (public key), which the contract stores in its state to verify signatures.
Minting Process:
When a user wants to mint a token, the dApp requests a challenge and a statement from the verifier backend.
Using the challenge, the dApp prompts the user to accept the information from their wallet.
The proof is shared with the verifier backend. If verified, the verifier signs the user's public address with the sign key provided during server startup.
This signature becomes the input for the minting function.
The smart contract verifies the signature using the stored public key in its state to ensure it's from the verifier and valid.
The mint() function is executed if verification passes, allowing the user to mint tokens.
Follow the steps below to implement this approach.
Step 1: Create the Smart Contract Project
Create a New Project: The following command initializes a new Rust project named "cis2-multi" for the smart contract:
Add Dependencies: Update the "Cargo.toml" file with the necessary dependencies:
This is how your "Cargo.toml" file will look:
Step 2: Modify the State Struct
Create
lib.rs
File: Copy and paste the following code by doing the following:Start modification with the State struct
Add a variable for the verify_key to the state's empty() function.
Note: Unlike a regular empty() function, this one takes the verify_key as a parameter.
Add Verify Key to State: Update the
State
struct inlib.rs
to include a variable for the verify key and add it to theempty()
function.
Step 3: Create Structs for Input Parameters
Create
InitParams
Struct: Define a struct to receive the verify key as an input parameter during contract initialization:
Add Signature to Mint Parameters: Modify the
MintParams
struct to include the signature for verification during minting.
Step 4: Update the Mint Function
Update Mint Function:
Modify the
contract_mint
function to include signature verification and minting logic based on the provided parameters.
By following these steps, you can update the cis2-multi
smart contract to incorporate the minting functionality using Concordium's ID layer and verifier backend server.
Last updated