Skip to content

Commit 2529ad0

Browse files
committed
Adding o1js dependency
1 parent 9c5a6fb commit 2529ad0

File tree

3 files changed

+108
-88
lines changed

3 files changed

+108
-88
lines changed

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"karma-coverage": "~2.2.0",
6363
"karma-jasmine": "~5.1.0",
6464
"karma-jasmine-html-reporter": "~2.1.0",
65+
"terser-webpack-plugin": "^5.3.10",
6566
"typescript": "~5.1.3",
6667
"webpack": "^5.88.2",
6768
"webpack-bundle-analyzer": "^4.9.0"

frontend/src/app/app.component.ts

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ import { AppSelectors } from '@app/app.state';
88
import { AppActions } from '@app/app.actions';
99
import { Observable, timer } from 'rxjs';
1010
import { CONFIG } from '@shared/constants/config';
11-
// import { AccountUpdate, declareMethods, Field, method, Mina, PrivateKey, SmartContract, State, state } from 'o1js';
12-
13-
// export class Square extends SmartContract {
14-
// @state(Field) num = State<Field>();
15-
//
16-
// override init() {
17-
// super.init();
18-
// this.num.set(Field(3));
19-
// }
20-
//
21-
// // @method
22-
// async update(square: Field) {
23-
// const currentState = this.num.get();
24-
// this.num.requireEquals(currentState);
25-
// square.assertEquals(currentState.mul(currentState));
26-
// this.num.set(square);
27-
// }
28-
// }
11+
import { AccountUpdate, declareMethods, Field, method, Mina, PrivateKey, SmartContract, State, state } from 'o1js';
12+
13+
export class Square extends SmartContract {
14+
@state(Field) num = State<Field>();
15+
16+
override init() {
17+
super.init();
18+
this.num.set(Field(3));
19+
}
20+
21+
// @method
22+
async update(square: Field) {
23+
const currentState = this.num.get();
24+
this.num.requireEquals(currentState);
25+
square.assertEquals(currentState.mul(currentState));
26+
this.num.set(square);
27+
}
28+
}
2929

3030
@Component({
3131
selector: 'app-root',
@@ -54,79 +54,78 @@ export class AppComponent extends ManualDetection implements OnInit {
5454
this.scheduleNodeUpdates();
5555
}
5656
this.listenToWindowResizing();
57-
// console.log('Start');
58-
// this.startZK();
59-
// console.log('Finish!');
57+
console.log('Start');
58+
this.startZK();
59+
console.log('Finish!');
6060
}
6161

62+
async startZK() {
63+
//@ts-ignore
64+
declareMethods(Square, { update: [Field] });
65+
66+
const useProof = false;
67+
68+
const Local = await Mina.LocalBlockchain({ proofsEnabled: useProof });
69+
Mina.setActiveInstance(Local);
70+
71+
const deployerAccount = Local.testAccounts[0];
72+
const deployerKey = deployerAccount.key;
73+
const senderAccount = Local.testAccounts[1];
74+
const senderKey = senderAccount.key;
75+
// ----------------------------------------------------
76+
77+
// Create a public/private key pair. The public key is your address and where you deploy the zkApp to
78+
const zkAppPrivateKey = PrivateKey.random();
79+
const zkAppAddress = zkAppPrivateKey.toPublicKey();
80+
81+
// create an instance of Square - and deploy it to zkAppAddress
82+
const zkAppInstance = new Square(zkAppAddress);
83+
const deployTxn = await Mina.transaction(deployerAccount, async () => {
84+
AccountUpdate.fundNewAccount(deployerAccount);
85+
await zkAppInstance.deploy();
86+
});
87+
await deployTxn.sign([deployerKey, zkAppPrivateKey]).send();
6288

63-
// async startZK() {
64-
// //@ts-ignore
65-
// declareMethods(Square, { update: [Field] });
66-
//
67-
// const useProof = false;
68-
//
69-
// const Local = await Mina.LocalBlockchain({ proofsEnabled: useProof });
70-
// Mina.setActiveInstance(Local);
71-
//
72-
// const deployerAccount = Local.testAccounts[0];
73-
// const deployerKey = deployerAccount.key;
74-
// const senderAccount = Local.testAccounts[1];
75-
// const senderKey = senderAccount.key;
76-
// // ----------------------------------------------------
77-
//
78-
// // Create a public/private key pair. The public key is your address and where you deploy the zkApp to
79-
// const zkAppPrivateKey = PrivateKey.random();
80-
// const zkAppAddress = zkAppPrivateKey.toPublicKey();
81-
//
82-
// // create an instance of Square - and deploy it to zkAppAddress
83-
// const zkAppInstance = new Square(zkAppAddress);
84-
// const deployTxn = await Mina.transaction(deployerAccount, async () => {
85-
// AccountUpdate.fundNewAccount(deployerAccount);
86-
// await zkAppInstance.deploy();
87-
// });
88-
// await deployTxn.sign([deployerKey, zkAppPrivateKey]).send();
89-
//
90-
// // get the initial state of Square after deployment
91-
// const num0 = zkAppInstance.num.get();
92-
// console.log('state after init:', num0.toString());
93-
//
94-
// // ----------------------------------------------------
95-
//
96-
// const txn1 = await Mina.transaction(senderAccount, async () => {
97-
// await zkAppInstance.update(Field(9));
98-
// });
99-
// await txn1.prove();
100-
// await txn1.sign([senderKey]).send();
101-
//
102-
// const num1 = zkAppInstance.num.get();
103-
// console.log('state after txn1:', num1.toString());
104-
//
105-
// // ----------------------------------------------------
106-
//
107-
// try {
108-
// const txn2 = await Mina.transaction(senderAccount, async () => {
109-
// await zkAppInstance.update(Field(75));
110-
// });
111-
// await txn2.prove();
112-
// await txn2.sign([senderKey]).send();
113-
// } catch (error: any) {
114-
// console.log(error.message);
115-
// }
116-
// const num2 = zkAppInstance.num.get();
117-
// console.log('state after txn2:', num2.toString());
118-
//
119-
// // ----------------------------------------------------
120-
//
121-
// const txn3 = await Mina.transaction(senderAccount, async () => {
122-
// await zkAppInstance.update(Field(81));
123-
// });
124-
// await txn3.prove();
125-
// await txn3.sign([senderKey]).send();
126-
//
127-
// const num3 = zkAppInstance.num.get();
128-
// console.log('state after txn3:', num3.toString());
129-
// }
89+
// get the initial state of Square after deployment
90+
const num0 = zkAppInstance.num.get();
91+
console.log('state after init:', num0.toString());
92+
93+
// ----------------------------------------------------
94+
95+
const txn1 = await Mina.transaction(senderAccount, async () => {
96+
await zkAppInstance.update(Field(9));
97+
});
98+
await txn1.prove();
99+
await txn1.sign([senderKey]).send();
100+
101+
const num1 = zkAppInstance.num.get();
102+
console.log('state after txn1:', num1.toString());
103+
104+
// ----------------------------------------------------
105+
106+
try {
107+
const txn2 = await Mina.transaction(senderAccount, async () => {
108+
await zkAppInstance.update(Field(75));
109+
});
110+
await txn2.prove();
111+
await txn2.sign([senderKey]).send();
112+
} catch (error: any) {
113+
console.log(error.message);
114+
}
115+
const num2 = zkAppInstance.num.get();
116+
console.log('state after txn2:', num2.toString());
117+
118+
// ----------------------------------------------------
119+
120+
const txn3 = await Mina.transaction(senderAccount, async () => {
121+
await zkAppInstance.update(Field(81));
122+
});
123+
await txn3.prove();
124+
await txn3.sign([senderKey]).send();
125+
126+
const num3 = zkAppInstance.num.get();
127+
console.log('state after txn3:', num3.toString());
128+
}
130129

131130
private scheduleNodeUpdates(): void {
132131
timer(1000, 5000).subscribe(() => this.store.dispatch(AppActions.getNodeDetails()));

frontend/webpack.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
const TerserPlugin = require("terser-webpack-plugin");
2+
console.log('Loading custom webpack!')
13
module.exports = {
24
experiments: {
35
topLevelAwait: true,
46
},
7+
optimization: {
8+
minimize: true,
9+
minimizer: [
10+
new TerserPlugin({
11+
terserOptions: {
12+
ecma: undefined,
13+
parse: {},
14+
compress: {
15+
keep_classnames: true,
16+
keep_fargs: true,
17+
keep_fnames: true,
18+
},
19+
keep_classnames: true,
20+
keep_fnames: true,
21+
},
22+
}),
23+
],
24+
}
525
}

0 commit comments

Comments
 (0)