l1-lottery-contracts
Version:
This repo contains smart contracts and related scripts for ZkNoid L1 Lottery proposed [here](https://forums.minaprotocol.com/t/zknoid-l1-lottery/6269)
127 lines • 5.47 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Field, Poseidon, Provable, PublicKey, SmartContract, State, Struct, UInt32, method, state, } from 'o1js';
import { BLOCK_PER_ROUND } from '../constants.js';
export class CommitValue extends Struct({
value: Field,
salt: Field,
}) {
hash() {
return Poseidon.hash([this.value, this.salt]);
}
}
// #TODO change to actual address
const firstPartyAddress = PublicKey.fromBase58('B62qryLwDWH5TM4N65Cs9S3jWqyDgC9JYXr5kc87ua8NFB5enpiuT1Y');
const secondPartyAddress = PublicKey.fromBase58('B62qnSztu3Gp49AR6AWAEUERBVSnoWDFJTki5taYUY7ig9hR1ut1a6r');
export class RandomManager extends SmartContract {
constructor() {
super(...arguments);
// Do not change order of storage, as it would affect deployment via factory
this.startSlot = State();
this.firstCommit = State();
this.secondCommit = State();
this.firstValue = State();
this.secondValue = State();
this.result = State();
}
async firstPartyCommit(value) {
this.firstCommit.getAndRequireEquals().assertEquals(Field(0));
value.value.assertGreaterThan(0, 'Value should be > 0');
this.checkPermission(firstPartyAddress);
this.firstCommit.set(value.hash());
}
async secondPartyCommit(value) {
this.secondCommit.getAndRequireEquals().assertEquals(Field(0));
value.value.assertGreaterThan(0, 'Value should be > 0');
this.checkPermission(secondPartyAddress);
this.secondCommit.set(value.hash());
}
async revealFirstCommit(value) {
this.checkRoundPass();
const storedCommit = this.firstCommit.getAndRequireEquals();
storedCommit.assertEquals(value.hash(), 'Reveal failed: Commit does not match stored value.');
this.firstValue.set(value.value);
const secondValue = this.secondValue.getAndRequireEquals();
this.produceResultIfAllRevealed(value.value, secondValue);
}
async revealSecondCommit(value) {
this.checkRoundPass();
const storedCommit = this.secondCommit.getAndRequireEquals();
storedCommit.assertEquals(value.hash(), 'Reveal failed: Commit does not match stored value.');
const firstValue = this.firstValue.getAndRequireEquals();
this.secondValue.set(value.value);
this.produceResultIfAllRevealed(firstValue, value.value);
}
produceResultIfAllRevealed(firstValue, secondValue) {
const allRevealed = firstValue
.greaterThan(Field(0))
.and(secondValue.greaterThan(Field(0)));
const result = Poseidon.hash([firstValue, secondValue]);
const resultToStore = Provable.if(allRevealed, result, Field(0));
this.result.set(resultToStore);
}
checkPermission(targetAddress) {
this.sender.getAndRequireSignatureV2().assertEquals(targetAddress);
}
checkRoundPass() {
const startSlot = this.startSlot.getAndRequireEquals();
this.network.globalSlotSinceGenesis.requireBetween(startSlot.add(BLOCK_PER_ROUND), UInt32.MAXINT());
}
}
__decorate([
state(UInt32),
__metadata("design:type", Object)
], RandomManager.prototype, "startSlot", void 0);
__decorate([
state(Field),
__metadata("design:type", Object)
], RandomManager.prototype, "firstCommit", void 0);
__decorate([
state(Field),
__metadata("design:type", Object)
], RandomManager.prototype, "secondCommit", void 0);
__decorate([
state(Field),
__metadata("design:type", Object)
], RandomManager.prototype, "firstValue", void 0);
__decorate([
state(Field),
__metadata("design:type", Object)
], RandomManager.prototype, "secondValue", void 0);
__decorate([
state(Field),
__metadata("design:type", Object)
], RandomManager.prototype, "result", void 0);
__decorate([
method,
__metadata("design:type", Function),
__metadata("design:paramtypes", [CommitValue]),
__metadata("design:returntype", Promise)
], RandomManager.prototype, "firstPartyCommit", null);
__decorate([
method,
__metadata("design:type", Function),
__metadata("design:paramtypes", [CommitValue]),
__metadata("design:returntype", Promise)
], RandomManager.prototype, "secondPartyCommit", null);
__decorate([
method,
__metadata("design:type", Function),
__metadata("design:paramtypes", [CommitValue]),
__metadata("design:returntype", Promise)
], RandomManager.prototype, "revealFirstCommit", null);
__decorate([
method,
__metadata("design:type", Function),
__metadata("design:paramtypes", [CommitValue]),
__metadata("design:returntype", Promise)
], RandomManager.prototype, "revealSecondCommit", null);
export default RandomManager;
//# sourceMappingURL=RandomManager.js.map