|
| 1 | +import { |
| 2 | + abimethod, |
| 3 | + Account, |
| 4 | + arc4, |
| 5 | + assert, |
| 6 | + BigUint, |
| 7 | + Bytes, |
| 8 | + ensureBudget, |
| 9 | + Global, |
| 10 | + itxn, |
| 11 | + LocalState, |
| 12 | + op, |
| 13 | + OpUpFeeSource, |
| 14 | + TemplateVar, |
| 15 | + Txn, |
| 16 | + uint64, |
| 17 | +} from '@algorandfoundation/algorand-typescript' |
| 18 | + |
| 19 | +const curveMod = 21888242871839275222246405745257275088548364400416034343698204186575808495617n |
| 20 | +const verifierBudget = 145000 |
| 21 | + |
| 22 | +export default class ZkWhitelistContract extends arc4.Contract { |
| 23 | + appName: arc4.Str | undefined |
| 24 | + whiteList = LocalState<boolean>() |
| 25 | + |
| 26 | + @abimethod({ onCreate: 'require' }) |
| 27 | + create(name: arc4.Str) { |
| 28 | + // Create the application |
| 29 | + this.appName = name |
| 30 | + } |
| 31 | + |
| 32 | + @abimethod({ allowActions: ['UpdateApplication', 'DeleteApplication'] }) |
| 33 | + update() { |
| 34 | + // Update the application if it is mutable (manager only) |
| 35 | + assert(Global.creatorAddress === Txn.sender) |
| 36 | + } |
| 37 | + |
| 38 | + @abimethod({ allowActions: ['OptIn', 'CloseOut'] }) |
| 39 | + optInOrOut() { |
| 40 | + // Opt in or out of the application |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + @abimethod() |
| 45 | + addAddressToWhitelist(address: arc4.Address, proof: arc4.DynamicArray<arc4.Address>): arc4.Str { |
| 46 | + /* |
| 47 | + Add caller to the whitelist if the zk proof is valid. |
| 48 | + On success, will return an empty string. Otherwise, will return an error |
| 49 | + message. |
| 50 | + */ |
| 51 | + ensureBudget(verifierBudget, OpUpFeeSource.GroupCredit) |
| 52 | + // The verifier expects public inputs to be in the curve field, but an |
| 53 | + // Algorand address might represent a number larger than the field |
| 54 | + // modulus, so to be safe we take the address modulo the field modulus |
| 55 | + const addressMod = arc4.interpretAsArc4<arc4.Address>(op.bzero(32).bitwiseOr(Bytes(BigUint(address.bytes) % curveMod))) |
| 56 | + // Verify the proof by calling the deposit verifier app |
| 57 | + const verified = this.verifyProof(TemplateVar<uint64>('VERIFIER_APP_ID'), proof, new arc4.DynamicArray(addressMod)) |
| 58 | + if (!verified.native) { |
| 59 | + return new arc4.Str('Proof verification failed') |
| 60 | + } |
| 61 | + // if successful, add the sender to the whitelist by setting local state |
| 62 | + const account = Account(address.bytes) |
| 63 | + if (Txn.sender !== account) { |
| 64 | + return new arc4.Str('Sender address does not match authorized address') |
| 65 | + } |
| 66 | + this.whiteList(account).value = true |
| 67 | + return new arc4.Str('') |
| 68 | + } |
| 69 | + |
| 70 | + @abimethod() |
| 71 | + isOnWhitelist(address: arc4.Address): arc4.Bool { |
| 72 | + // Check if an address is on the whitelist |
| 73 | + const account = address.native |
| 74 | + const optedIn = op.appOptedIn(account, Global.currentApplicationId) |
| 75 | + if (!optedIn) { |
| 76 | + return new arc4.Bool(false) |
| 77 | + } |
| 78 | + const whitelisted = this.whiteList(account).value |
| 79 | + return new arc4.Bool(whitelisted) |
| 80 | + } |
| 81 | + |
| 82 | + verifyProof(appId: uint64, proof: arc4.DynamicArray<arc4.Address>, publicInputs: arc4.DynamicArray<arc4.Address>): arc4.Bool { |
| 83 | + // Verify a proof using the verifier app. |
| 84 | + const verified = itxn |
| 85 | + .applicationCall({ |
| 86 | + appId: appId, |
| 87 | + fee: 0, |
| 88 | + appArgs: [arc4.methodSelector('verify(byte[32][],byte[32][])bool'), proof.copy(), publicInputs.copy()], |
| 89 | + onCompletion: arc4.OnCompleteAction.NoOp, |
| 90 | + }) |
| 91 | + .submit().lastLog |
| 92 | + return arc4.interpretAsArc4<arc4.Bool>(verified, 'log') |
| 93 | + } |
| 94 | +} |
0 commit comments