|
| 1 | +use starknet::ContractAddress; |
| 2 | + |
| 3 | +#[starknet::interface] |
| 4 | +trait IZkERC20Token<TContractState> { |
| 5 | + fn mint_with_proof(ref self: TContractState, full_proof: Span<felt252>); |
| 6 | + fn has_user_minted(self: @TContractState, address: ContractAddress) -> bool; |
| 7 | +} |
| 8 | + |
| 9 | +#[starknet::interface] |
| 10 | +trait IGroth16VerifierBN254<TContractState> { |
| 11 | + fn verify_groth16_proof_bn254( |
| 12 | + self: @TContractState, full_proof_with_hints: Span<felt252>, |
| 13 | + ) -> Option<Span<u256>>; |
| 14 | +} |
| 15 | + |
| 16 | +mod errors { |
| 17 | + pub const ALREADY_MINTED: felt252 = 'User has already minted tokens'; |
| 18 | + pub const PROOF_NOT_VERIFIED: felt252 = 'Proof is not correct'; |
| 19 | + pub const PROOF_ALREADY_USED: felt252 = 'Generate a proof unique to you'; |
| 20 | +} |
| 21 | + |
| 22 | +#[starknet::contract] |
| 23 | +pub mod ZkERC20Token { |
| 24 | + use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl}; |
| 25 | + use starknet::{ContractAddress, get_caller_address}; |
| 26 | + use super::{errors, IGroth16VerifierBN254Dispatcher, IGroth16VerifierBN254DispatcherTrait}; |
| 27 | + use starknet::storage::{ |
| 28 | + Map, StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, |
| 29 | + }; |
| 30 | + |
| 31 | + component!(path: ERC20Component, storage: erc20, event: ERC20Event); |
| 32 | + |
| 33 | + #[abi(embed_v0)] |
| 34 | + impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>; |
| 35 | + |
| 36 | + impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>; |
| 37 | + |
| 38 | + const MINT_WITH_PROOF_TOKEN_REWARD: u8 = 100; |
| 39 | + // used in the front end to generate the proof |
| 40 | + const PASSWORD_HASH: u256 = |
| 41 | + 16260938803047823847354854419633652218467975114284208787981985448019235110758; |
| 42 | + |
| 43 | + #[storage] |
| 44 | + struct Storage { |
| 45 | + #[substorage(v0)] |
| 46 | + erc20: ERC20Component::Storage, |
| 47 | + verifier_contract: IGroth16VerifierBN254Dispatcher, |
| 48 | + users_who_minted: Map<ContractAddress, bool>, |
| 49 | + } |
| 50 | + |
| 51 | + #[event] |
| 52 | + #[derive(Drop, starknet::Event)] |
| 53 | + enum Event { |
| 54 | + #[flat] |
| 55 | + ERC20Event: ERC20Component::Event, |
| 56 | + } |
| 57 | + |
| 58 | + #[constructor] |
| 59 | + fn constructor( |
| 60 | + ref self: ContractState, |
| 61 | + initial_supply: u256, |
| 62 | + recipient: ContractAddress, |
| 63 | + name: ByteArray, |
| 64 | + symbol: ByteArray, |
| 65 | + proof_verifier_address: ContractAddress, |
| 66 | + ) { |
| 67 | + self.erc20.initializer(name, symbol); |
| 68 | + self.erc20.mint(recipient, initial_supply); |
| 69 | + |
| 70 | + self |
| 71 | + .verifier_contract |
| 72 | + .write(IGroth16VerifierBN254Dispatcher { contract_address: proof_verifier_address }); |
| 73 | + } |
| 74 | + |
| 75 | + #[abi(embed_v0)] |
| 76 | + impl ZkERC20TokenImpl of super::IZkERC20Token<ContractState> { |
| 77 | + fn mint_with_proof(ref self: ContractState, full_proof: Span<felt252>) { |
| 78 | + let caller = get_caller_address(); |
| 79 | + // Prevent a user from receiving tokens twice |
| 80 | + assert(!self.users_who_minted.entry(caller).read(), errors::ALREADY_MINTED); |
| 81 | + |
| 82 | + // Verify the correctness of the proof by calling the verifier contract |
| 83 | + // If incorrect, execution of the verifier will fail or return an Option::None |
| 84 | + let proof_public_inputs = self |
| 85 | + .verifier_contract |
| 86 | + .read() |
| 87 | + .verify_groth16_proof_bn254(full_proof); |
| 88 | + assert( |
| 89 | + proof_public_inputs.is_some() && proof_public_inputs.unwrap().len() == 3, |
| 90 | + errors::PROOF_NOT_VERIFIED, |
| 91 | + ); |
| 92 | + |
| 93 | + // Verify the proof has been generated by the user calling this smart contract |
| 94 | + let user_address_dec: u256 = *proof_public_inputs.unwrap().at(1); |
| 95 | + let address_felt252: felt252 = caller.into(); |
| 96 | + assert(address_felt252.into() == user_address_dec, errors::PROOF_ALREADY_USED); |
| 97 | + |
| 98 | + // Mint tokens only if the proof is valid and has been generated by the user |
| 99 | + self.erc20.mint(caller, MINT_WITH_PROOF_TOKEN_REWARD.into()); |
| 100 | + |
| 101 | + self.users_who_minted.entry(caller).write(true); |
| 102 | + } |
| 103 | + |
| 104 | + fn has_user_minted(self: @ContractState, address: ContractAddress) -> bool { |
| 105 | + self.users_who_minted.entry(address).read() |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments