Skip to content

Commit

Permalink
feat: update O1JS code to v1.1.0, addressing the breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iluxonchik committed May 16, 2024
1 parent 5c99f33 commit be3f2ee
Show file tree
Hide file tree
Showing 34 changed files with 251 additions and 393 deletions.
50 changes: 0 additions & 50 deletions contracts/src/Polygon.ts

This file was deleted.

25 changes: 0 additions & 25 deletions contracts/src/Recursion.ts

This file was deleted.

20 changes: 0 additions & 20 deletions contracts/src/SimpleStateUpdate.ts

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/src/api/proofs/ZKExactGeoPointCircuitProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ZKExactGeoPointCircuitProof extends ZKLocusProof<ExactGeoPointCircu
* @param jsonProof The JSON representation of the proof.
* @returns An instance of ZKExactGeoPointCircuitProof.
*/
static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return ExactGeoPointCircuitProof.fromJSON(jsonProof);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ZKExactGeolocationMetadataCircuitProof extends ZKLocusProof<ExactGe
this._proof = proof;
}

static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return ExactGeolocationMetadataCircuitProof.fromJSON(jsonProof);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ZKGeoPointInOrOutOfPolygonCircuitProof extends ZKLocusProof<GeoPoin
super();
}

static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return GeoPointInOrOutOfPolygonCircuitProof.fromJSON(jsonProof);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/api/proofs/ZKGeoPointInPolygonProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class ZKGeoPointInPolygonProof extends ZKLocusProof<GeoPointInPolygonCirc
return Poseidon.hash([leftPolygonHash, rightPolygonHash]);
}

static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return GeoPointInPolygonCircuitProof.fromJSON(jsonProof);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/api/proofs/ZKGeoPointProviderCircuitProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ZKGeoPointProviderCircuitProof extends ZKLocusProof<GeoPointProvide
this._proof = proof;
}

static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return GeoPointProviderCircuitProof.fromJSON(jsonProof);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ZKOracleGeoPointProviderCircuitProof extends ZKLocusProof<OracleGeo
this._proof = proof;
}

static fromJSON(jsonProof: JsonProof): IO1JSProof {
static async fromJSON(jsonProof: JsonProof): Promise<IO1JSProof> {
return OracleGeoPointProviderCircuitProof.fromJSON(jsonProof);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bool } from "o1js/dist/node/lib/bool";
import { Bool } from "o1js";
import { ZKLocusProof } from "../ZKLocusProof";

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/api/sha3/SHA3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bytes, Field, Hash, Poseidon} from "o1js";
import { ProvablePureExtended } from "o1js/dist/node/lib/circuit-value";
import { ProvablePureExtended } from "o1js/dist/node/lib/provable/types/struct";


/**
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/blockchain/contracts/RandoMinaContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const RandomNumberObservationCircuit = ZkProgram({
methods: {
generateRandomNumber: {
privateInputs: [Field],
method: (parameters: PublicPRNGParameters, nonce: Field): Field => {
async method(parameters: PublicPRNGParameters, nonce: Field){
return Poseidon.hash(
[
parameters.networkState,
Expand Down Expand Up @@ -66,9 +66,9 @@ export class RandomNumberObservationCircuitProof extends ZkProgram.Proof(RandomN
*/
export class RandoMinaContract extends SmartContract {

@method verifyRandomNumber(observationProof: RandomNumberObservationCircuitProof): void {
@method async verifyRandomNumber(observationProof: RandomNumberObservationCircuitProof){
const claimedSender: Field = observationProof.publicInput.sender;
claimedSender.assertEquals(Poseidon.hash(this.sender.toFields()));
claimedSender.assertEquals(Poseidon.hash(this.sender.getUnconstrained().toFields()));

const claimedNetworkState: Field = observationProof.publicInput.networkState;
this.network.stakingEpochData.ledger.hash.requireEquals(claimedNetworkState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
Bool,
} from 'o1js';
import { ZKLContract } from '../tokens/zkl/ZKLContract';
import { Account } from 'o1js/dist/node/lib/precondition';

/*
Merkle Witness Mental Model Glossary:
Expand All @@ -28,11 +27,8 @@ import { Account } from 'o1js/dist/node/lib/precondition';


type GroupHash = {
x: Field;
y: {
x0: Field;
x1: Field;
};
x: Field,
y: { x0: Field },
}


Expand All @@ -50,14 +46,12 @@ type GroupHash = {
* in a set of hierarchical interfaces and implementaions.
*/
export class BountyBulletinBoardContract extends TokenContract {
// Root of the Merkle Map containing the bounty funding information.
@state(PublicKey) bountyMapRoot = State<PublicKey>();

// @method initState(intialBountyMapRoot: Field) {
// this.bountyMapRoot.set(intialBountyMapRoot);
// }

@method approveBase(forest: AccountUpdateForest) {
@method async approveBase(forest: AccountUpdateForest) {
//this.checkZeroBalanceChange(forest);
forest.isEmpty().assertFalse();
}
Expand All @@ -81,30 +75,30 @@ export class BountyBulletinBoardContract extends TokenContract {
// which contains the bounty $ZKL amount

// https://github.com/o1-labs/o1js/blob/0ec1c9da8c714298e6088ffa88178abb3961d200/src/lib/nullifier.ts#L64
const newGroupHash: GroupHash = Poseidon.hashToGroup([...funderAddress.toFields(), ...bountyId.toFields()]);
const group: Group = new Group({ x: newGroupHash.x, y: newGroupHash.y.x0 });
const group: Group = Poseidon.hashToGroup([...funderAddress.toFields(), ...bountyId.toFields()]);
const bountyPublicKey: PublicKey = PublicKey.fromGroup(group);

return bountyPublicKey;
}

@method deriveBountyAccountAddressMethod(
@method.returns(PublicKey)
async deriveBountyAccountAddressMethod(
funderAddress: PublicKey,
bountyId: UInt64, // the size of the tree will be fixed, but a self-replication mechanism will be implemented
): PublicKey {
){

// 1. Derive the a PublicKey, which will represent the address of the account
// which contains the bounty $ZKL amount

// https://github.com/o1-labs/o1js/blob/0ec1c9da8c714298e6088ffa88178abb3961d200/src/lib/nullifier.ts#L64
const newGroupHash: GroupHash = Poseidon.hashToGroup([...funderAddress.toFields(), ...bountyId.toFields()]);
const group: Group = new Group({ x: newGroupHash.x, y: newGroupHash.y.x0 });
const group: Group = Poseidon.hashToGroup([...funderAddress.toFields(), ...bountyId.toFields()]);
const bountyPublicKey: PublicKey = PublicKey.fromGroup(group);

return bountyPublicKey;
}

@method sendFromTo(
@method
async sendFromTo(
senderAddress: PublicKey,
receiverAddress: PublicKey,
amount: UInt64
Expand All @@ -121,15 +115,19 @@ export class BountyBulletinBoardContract extends TokenContract {
this.approveAccountUpdate(accountUpdate);
}

@method fundBounty(

// TODO: next step: implement the fund and the claim bounty methods using the "peove that I deployed constract X" approach
@method.returns(PublicKey)
async fundBounty(

funderAddress: PublicKey,
bountyId: UInt64, // the size of the tree will be fixed, but a self-replication mechanism will be implemented
bountyId: UInt64, // TODO: for now, a single bounty ID is supported
bbAmountIncrement: UInt64,
): PublicKey {
){

// 1. Derive the a PublicKey, which will represent the address of the account
// which contains the bounty $ZKL amount
const bountyPublicKey: PublicKey = this.deriveBountyAccountAddressMethod(funderAddress, bountyId);
const bountyPublicKey: PublicKey = await this.deriveBountyAccountAddressMethod(funderAddress, bountyId);

// NOTE: funding of new account may be required, if the "bountyPublicKey" is a new account.
// The sender can easily verify wether the bounty needs to be funded (new bounty operation),
Expand Down Expand Up @@ -161,22 +159,22 @@ export class BountyBulletinBoardContract extends TokenContract {
}


@method fundBountyOriginal(
@method.returns(PublicKey) async fundBountyOriginal(
funderAddress: PublicKey,
bountyId: UInt64, // the size of the tree will be fixed, but a self-replication mechanism will be implemented
bbAmountIncrement: UInt64,
): PublicKey {
){

// 1. Derive the a PublicKey, which will represent the address of the account
// which contains the bounty $ZKL amount
const bountyPublicKey: PublicKey = this.deriveBountyAccountAddressMethod(funderAddress, bountyId);
const bountyPublicKey: PublicKey = await this.deriveBountyAccountAddressMethod(funderAddress, bountyId);

// NOTE: funding of new account may be required, if the "bountyPublicKey" is a new account.
// The sender can easily verify wether the bounty needs to be funded (new bounty operation),
// or whether it already exists
this.internal.send(
{
from: this.sender,
from: this.sender.getAndRequireSignature(),
to: bountyPublicKey,
amount: bbAmountIncrement,
}
Expand All @@ -191,13 +189,13 @@ export class BountyBulletinBoardContract extends TokenContract {
});
}

@method claimBounty(
@method async claimBounty(
funderAddress: PublicKey,
bountyId: UInt64,
) {
// 1. Derive the a PublicKey, which will represent the address of the account
// which contains the bounty $ZKL amount
const bountyPublicKey: PublicKey = this.deriveBountyAccountAddressMethod(funderAddress, bountyId);
const bountyPublicKey: PublicKey = await this.deriveBountyAccountAddressMethod(funderAddress, bountyId);

// // sender acccount update
let au = AccountUpdate.create(bountyPublicKey, this.deriveTokenId());
Expand All @@ -217,7 +215,7 @@ export class BountyBulletinBoardContract extends TokenContract {

let ac = this.internal.send({
from: bountyPublicKey,
to: this.sender,
to: this.sender.getAndRequireSignature(),
amount: balance,
});

Expand All @@ -227,11 +225,11 @@ export class BountyBulletinBoardContract extends TokenContract {
//this.burnToZKL(bountyPublicKey, this.sender);
}

@method approveUpdate(au: AccountUpdate) {
@method async approveUpdate(au: AccountUpdate) {
this.approve(au);
}

@method claimBountyWithApprove(
@method async claimBountyWithApprove(
claimAU: AccountUpdate,
bountyId: UInt64,
) {
Expand All @@ -240,17 +238,17 @@ export class BountyBulletinBoardContract extends TokenContract {

}

@method mintFromZKL(
@method async mintFromZKL(
zklAddress: PublicKey,
amount: UInt64,
) {
const zklContract: ZKLContract = new ZKLContract(zklAddress);
zklContract.sendFromTo(this.sender, this.address, amount);
zklContract.sendFromTo(this.sender.getAndRequireSignature(), this.address, amount);
// Mint custom token of the same amount
this.internal.mint({ address: this.sender, amount: amount });
this.internal.mint({ address: this.sender.getAndRequireSignature(), amount: amount });
}

@method burnToZKL(
@method async burnToZKL(
bountyAccountPublicKey: PublicKey,
receiverAccountAddress: PublicKey,
) {
Expand Down Expand Up @@ -280,7 +278,7 @@ export class BountyBulletinBoardContract extends TokenContract {
//zklContract.sendFromTo(this.address, receiverAccountAddress, bountyAmount);
}

deploy(args: DeployArgs) {
async deploy(args: DeployArgs) {
super.deploy(args);
// Temporarily all set to proof, will be refined later
this.account.permissions.set({
Expand All @@ -294,7 +292,7 @@ export class BountyBulletinBoardContract extends TokenContract {

}

@method init() {
@method async init() {
super.init();
this.account.tokenSymbol.set("BBB");
}
Expand Down
Loading

0 comments on commit be3f2ee

Please sign in to comment.