diff --git a/src/ZKLottoGame.ts b/src/ZKLottoGame.ts index de10c6c..02c3f6f 100644 --- a/src/ZKLottoGame.ts +++ b/src/ZKLottoGame.ts @@ -85,9 +85,7 @@ class ZKLottoGame extends SmartContract { //Lotto Winning numbers Hash @state(Field) LottoWinHash = State(); - - - // @state(Field) storageTreeRoot = State(); + @state(Field) storageTreeRoot = State(); init() { @@ -98,9 +96,8 @@ class ZKLottoGame extends SmartContract { this.currentGameTimeStart.set(UInt64.from(0)); this.currentGameTimeEnd.set(this.network.timestamp.get()); this.gameduration.set(UInt64.from(518400)); //game duration is 6 days, winning lotto numbers generated on day 7 - //initiate gameRoot - // const emptyTreeRoot = new MerkleTree(8).getRoot(); - // this.storageTreeRoot.set(emptyTreeRoot); + const emptyTreeRoot = new MerkleTree(8).getRoot(); + this.storageTreeRoot.set(emptyTreeRoot); } @method async startLottoWeek() { @@ -178,12 +175,20 @@ class ZKLottoGame extends SmartContract { // const lottoEntryHash = lottoEntry.hash(); signature.verify(pubkey, [week_, lottoEntryHash]).assertTrue(); - /*TO-DO - add user's lotto numbers entry to merkleTree for the Game week - */ - + // retrieve the current Merkle root + const currentRoot = this.storageTreeRoot.get(); + this.storageTreeRoot.requireEquals(currentRoot); + // generate a new Merkle leaf for this entry + const leaf = lottoEntryHash; + // create a Merkle Tree and update it with the new leaf + let tree = new MerkleTree(8); + tree.setLeaf(week_.toBigInt() % BigInt(256), leaf); + + // compute the new root and update the state + const newRoot = tree.getRoot(); + this.storageTreeRoot.set(newRoot); } @method.returns(Bool) async ClaimWinning( @@ -191,16 +196,17 @@ class ZKLottoGame extends SmartContract { signature: Signature, week_: Field, winningNums: LottoNumbers, + witness: MerkleWitness8, ) { - //proof user is winner of the claim week's lotto - const winninglottoEntryHash = this.LottoWinHash.get(); - this.LottoWinHash.requireEquals(this.LottoWinHash.get()); - const winningLeaf = [week_].concat(winninglottoEntryHash.toFields()); - signature.verify(pubkey, winningLeaf).assertTrue(); + const currentRoot = this.storageTreeRoot.get(); + this.storageTreeRoot.requireEquals(currentRoot); + const leaf = winningNums.hash(); + const isValid = witness.calculateRoot(leaf).equals(currentRoot); + isValid.assertTrue("Invalid proof"); + + signature.verify(pubkey, [week_, leaf]).assertTrue(); - //transfer winnings to user after successful proof verification return Bool(true); - } -} \ No newline at end of file +}