A Custom Token in Leo
Summary
A transparent & shielded custom token in Leo.
How to Run
Follow the Leo Installation Instructions.
This token program can be run using the following bash script. Locally, it will execute Leo program functions to mint and transfer tokens publicly and privately.
The .env file contains a private key and address. This is the account that will be used to sign transactions and is checked for record ownership. When executing programs as different parties, be sure to set the private_key field in .env to the appropriate value. You can check out how we’ve set things up in ./run.sh for a full example of how to run the program as different parties.
Walkthrough
- Step 0: Public Mint
- Step 1: Private Mint
- Step 2: Public Transfer
- Step 3: Private Transfer
- Step 4: Public to Private Transfer
- Step 5: Private to Public Transfer
We’ll be conducting a transfer between two parties.
Public Mint
Let’s play Alice. Swap in her private key and publicly mint 100 tokens.
You can see the output of mint_public, which takes the arguments Alice’s address and the amount of tokens to mint publicly. This information is shown on-chain and can be queried on a network.
Private Mint
Now let’s privately mint 100 tokens for Bob. Switch to Bob’s private key and privately mint 100 tokens for Bob.
The output is a private record.
Public Transfer
Let’s publicly transfer 10 tokens from Alice to Bob. Swap the private key back to Alice and call the public transfer transition.
Again, we see the arguments used for the finzalize function of transfer_public – Alice’s address, Bob’s address, and the amount to transfer. The public mapping will be queryable on-chain.
Private Transfer
Let’s privately transfer 20 tokens from Bob to Alice. Switch to Bob’s private key and call the private transfer transition.
The output of transfer_private is a record owned by Bob less the 20 tokens he privately transferred to Alice, and a record owned by Alice with the 20 tokens Bob transferred to Alice.
Public to Private Transfer
Let’s convert 30 of Alice’s public tokens into 30 private tokens for Bob. Switch the private key back to Alice.
Calling transfer_public_to_private, outputs a Future, which indicates code to be run on-chain, along with its associated inputs.
Private to Public Transfer
Let’s convert 40 of Bob’s private tokens into 40 public tokens for Alice. Switch the private key back to Bob.
When we call transfer_private_to_public, we take Bob’s private record that contains 110 tokens, and outputs a record owned by Bob with 70 tokens, and then outputs a Future which will be run on-chain.