@@ -8,24 +8,24 @@ import { AppSelectors } from '@app/app.state';
88import { AppActions } from '@app/app.actions' ;
99import { Observable , timer } from 'rxjs' ;
1010import { 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 ( ) ) ) ;
0 commit comments