Skip to content

Commit

Permalink
Merge pull request #643 from o1-labs/feature/update-api
Browse files Browse the repository at this point in the history
Set Update fields
  • Loading branch information
mitschabaude authored Dec 13, 2022
2 parents 355eca7 + f0ab0c9 commit edf229e
Show file tree
Hide file tree
Showing 40 changed files with 269 additions and 200 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `this.account.<field>.set()` as a unified API to update fields on the account https://github.com/o1-labs/snarkyjs/pull/643
- covers `permissions`, `verificationKey`, `zkappUri`, `tokenSymbol`, `delegate`, `votingFor`
- exists on `SmartContract.account` and `AccountUpdate.account`
- `Circuit.constraintSystemFromKeypair(keypair)` to inspect the circuit at a low level https://github.com/o1-labs/snarkyjs/pull/529
- Works with a `keypair` (prover + verifier key) generated with the `Circuit` API
- `tx.wait()` is now implemented. It waits for the transactions inclusion in a block https://github.com/o1-labs/snarkyjs/pull/645
- `wait()` also now takes an optional `options` parameter to specify the polling interval or maximum attempts. `wait(options?: { maxAttempts?: number; interval?: number }): Promise<void>;`

### Changed

- BREAKING CHANGE: Constraint changes in `sign()`, `requireSignature()` and `createSigned()` on `AccountUpdate` / `SmartContract`. _This means that smart contracts using these methods in their proofs won't be able to create valid proofs against old deployed verification keys._
- BREAKING CHANGE: Constraint changes in `sign()`, `requireSignature()` and `createSigned()` on `AccountUpdate` / `SmartContract`. _This means that smart contracts using these methods in their proofs won't be able to create valid proofs against old deployed verification keys._ https://github.com/o1-labs/snarkyjs/pull/637
- New option `enforceTransactionLimits` for `LocalBlockchain` (default value: `true`), to disable the enforcement of protocol transaction limits (maximum events, maximum sequence events and enforcing certain layout of `AccountUpdate`s depending on their authorization) https://github.com/o1-labs/snarkyjs/pull/620

### Deprecated

- `AccountUpdate.createSigned(privateKey: PrivateKey)` in favor of new signature `AccountUpdate.createSigned(publicKey: PublicKey)`
- `this.setPermissions()` in favor of `this.account.permissions.set()` https://github.com/o1-labs/snarkyjs/pull/643
- `this.tokenSymbol.set()` in favor of `this.account.tokenSymbol.set()`
- `this.setValue()` in favor of `this.account.<field>.set()`
- `AccountUpdate.createSigned(privateKey: PrivateKey)` in favor of new signature `AccountUpdate.createSigned(publicKey: PublicKey)` https://github.com/o1-labs/snarkyjs/pull/637

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion MINA_COMMIT
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
The mina commit used to generate the backends for node and chrome is
cb8fa95ef27ab92ddbecb0bd925f78d9fcce3d38
11eef6848acc011aaba2c8b1740e4b70f8d94a8a
4 changes: 2 additions & 2 deletions src/chrome_bindings/snarky_js_chrome.bc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/examples/simple_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SimpleZkapp extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
});
Expand Down
11 changes: 5 additions & 6 deletions src/examples/zkapps/dex/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ function createDex({
class TokenContract extends SmartContract {
deploy(args?: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
receive: Permissions.proof(),
Expand Down Expand Up @@ -437,14 +437,13 @@ class TokenContract extends SmartContract {
// => need callbacks for signatures
@method deployZkapp(address: PublicKey, verificationKey: VerificationKey) {
let tokenId = this.token.id;
let zkapp = AccountUpdate.defaultAccountUpdate(address, tokenId);
this.approve(zkapp);
AccountUpdate.setValue(zkapp.update.permissions, {
let zkapp = AccountUpdate.create(address, tokenId);
zkapp.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
});
AccountUpdate.setValue(zkapp.update.verificationKey, verificationKey);
zkapp.sign();
zkapp.account.verificationKey.set(verificationKey);
zkapp.requireSignature();
}

// let a zkapp send tokens to someone, provided the token supply stays constant
Expand Down
16 changes: 8 additions & 8 deletions src/examples/zkapps/dex/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Mina,
Int64,
PrivateKey,
VerificationKey,
} from 'snarkyjs';

/**
Expand Down Expand Up @@ -75,8 +76,8 @@ class TrivialCoin extends SmartContract implements Erc20 {

deploy(args: DeployArgs) {
super.deploy(args);
this.tokenSymbol.set('SOM');
this.setPermissions({
this.account.tokenSymbol.set('SOM');
this.account.permissions.set({
...Permissions.default(),
setPermissions: Permissions.proof(),
send: Permissions.proof(),
Expand Down Expand Up @@ -105,7 +106,7 @@ class TrivialCoin extends SmartContract implements Erc20 {
// that this function was run. Since it can be run only once, this implies it was run exactly once

// make account non-upgradable forever
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
setVerificationKey: Permissions.impossible(),
setPermissions: Permissions.impossible(),
Expand Down Expand Up @@ -145,7 +146,7 @@ class TrivialCoin extends SmartContract implements Erc20 {
// we don't have to check the balance of the sender -- this is done by the zkApp protocol
return Bool(true);
}
@method approve(spender: PublicKey, value: UInt64): Bool {
@method approveSpend(spender: PublicKey, value: UInt64): Bool {
// TODO: implement allowances
return Bool(false);
}
Expand Down Expand Up @@ -192,20 +193,19 @@ class TrivialCoin extends SmartContract implements Erc20 {
}

// this is a very standardized deploy method. instead, we could also take the account update from a callback
@method deployZkapp(zkappKey: PrivateKey) {
@method deployZkapp(zkappKey: PrivateKey, verificationKey: VerificationKey) {
let address = zkappKey.toPublicKey();
let tokenId = this.token.id;
let zkapp = Experimental.createChildAccountUpdate(
this.self,
address,
tokenId
);
AccountUpdate.setValue(zkapp.update.permissions, {
zkapp.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
});
// TODO pass in verification key
// AccountUpdate.setValue(zkapp.update.verificationKey, verificationKey);
zkapp.account.verificationKey.set(verificationKey);
zkapp.sign(zkappKey);
}

Expand Down
6 changes: 3 additions & 3 deletions src/examples/zkapps/dex/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ async function main({ withVesting }: { withVesting: boolean }) {
console.log('prepare test with forbidden send');
tx = await Mina.transaction(keys.tokenX, () => {
let tokenXtokenAccount = AccountUpdate.create(addresses.tokenX, tokenIds.X);
AccountUpdate.setValue(tokenXtokenAccount.update.permissions, {
tokenXtokenAccount.account.permissions.set({
...Permissions.initial(),
send: Permissions.impossible(),
});
tokenXtokenAccount.sign();
tokenXtokenAccount.requireSignature();
// token X owner approves w/ signature so we don't need another method for this test
let tokenX = AccountUpdate.create(addresses.tokenX);
tokenX.approve(tokenXtokenAccount);
tokenX.sign();
tokenX.requireSignature();
});
await tx.prove();
await tx.sign([keys.tokenX]).send();
Expand Down
41 changes: 17 additions & 24 deletions src/examples/zkapps/dex/upgradability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {
console.log('manipulating setDelegate field to impossible...');
// setting the setDelegate permission field to impossible
let dexAccount = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(dexAccount.update.permissions, {
dexAccount.account.permissions.set({
...Permissions.initial(),
setDelegate: Permissions.impossible(),
});
dexAccount.sign();
dexAccount.requireSignature();
});
await tx.prove();
tx.sign([keys.dex]);
Expand All @@ -123,11 +123,8 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {
tx = await Mina.transaction(feePayerKey, () => {
// setting the delegate field to something, although permissions forbid it
let dexAccount = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(
dexAccount.update.delegate,
PrivateKey.random().toPublicKey()
);
dexAccount.sign();
dexAccount.account.delegate.set(PrivateKey.random().toPublicKey());
dexAccount.requireSignature();
});
await tx.prove();

Expand All @@ -139,11 +136,11 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {

tx = await Mina.transaction(feePayerKey, () => {
let dexAccount = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(dexAccount.update.permissions, {
dexAccount.account.permissions.set({
...Permissions.initial(),
setDelegate: Permissions.proofOrSignature(),
});
dexAccount.sign();
dexAccount.requireSignature();
});
await tx.prove();
await tx.sign([keys.dex]).send();
Expand All @@ -153,8 +150,8 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {
let newDelegate = PrivateKey.random().toPublicKey();
tx = await Mina.transaction(feePayerKey, () => {
let dexAccount = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(dexAccount.update.delegate, newDelegate);
dexAccount.sign();
dexAccount.account.delegate.set(newDelegate);
dexAccount.requireSignature();
});
await tx.prove();
await tx.sign([keys.dex]).send();
Expand Down Expand Up @@ -182,19 +179,15 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {
// changing the permission to impossible and then trying to change the delegate field

let permissionUpdate = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(permissionUpdate.update.permissions, {
permissionUpdate.account.permissions.set({
...Permissions.initial(),
setDelegate: Permissions.impossible(),
});
permissionUpdate.sign();
permissionUpdate.requireSignature();

let fieldUpdate = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(
fieldUpdate.update.delegate,
PrivateKey.random().toPublicKey()
);

fieldUpdate.sign();
fieldUpdate.account.delegate.set(PrivateKey.random().toPublicKey());
fieldUpdate.requireSignature();
});
await tx.prove();
await expect(tx.sign([keys.dex]).send()).rejects.toThrow(
Expand All @@ -221,16 +214,16 @@ async function atomicActionsTest({ withVesting }: { withVesting: boolean }) {
tx = await Mina.transaction(feePayerKey, () => {
// changing field
let fieldUpdate = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(fieldUpdate.update.delegate, newDelegate);
fieldUpdate.sign();
fieldUpdate.account.delegate.set(newDelegate);
fieldUpdate.requireSignature();

// changing permissions back to impossible
let permissionUpdate2 = AccountUpdate.create(addresses.dex);
AccountUpdate.setValue(permissionUpdate2.update.permissions, {
permissionUpdate2.account.permissions.set({
...Permissions.initial(),
setDelegate: Permissions.impossible(),
});
permissionUpdate2.sign();
permissionUpdate2.requireSignature();
});
await tx.prove();
await tx.sign([keys.dex]).send();
Expand Down Expand Up @@ -431,7 +424,7 @@ async function upgradeabilityTests({ withVesting }: { withVesting: boolean }) {
tx = await Mina.transaction(feePayerKey, () => {
// pay fees for creating 3 dex accounts
let update = AccountUpdate.createSigned(keys.dex);
AccountUpdate.setValue(update.update.permissions, {
update.account.permissions.set({
...Permissions.initial(),
setVerificationKey: Permissions.impossible(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/escrow/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
export class Escrow extends SmartContract {
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proof(),
send: Permissions.proof(),
Expand Down
5 changes: 2 additions & 3 deletions src/examples/zkapps/hello_world/hello_world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
SmartContract,
state,
State,
AccountUpdate,
method,
DeployArgs,
PrivateKey,
Expand All @@ -21,13 +20,13 @@ export class HelloWorld extends SmartContract {

deploy(input: DeployArgs) {
super.deploy(input);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
});
this.x.set(Field(2));

AccountUpdate.setValue(this.self.update.delegate, adminPublicKey);
this.account.delegate.set(adminPublicKey);
}

@method update(squared: Field, admin: PrivateKey) {
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/local_events_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SimpleZkapp extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
send: Permissions.proofOrSignature(),
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/merkle_tree/merkle_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Leaderboard extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/set_local_preconditions_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ await isReady;
class SimpleZkapp extends SmartContract {
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
send: Permissions.proofOrSignature(),
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/simple_and_counter_zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CounterZkapp extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
editSequenceState: Permissions.proofOrSignature(),
Expand Down Expand Up @@ -97,7 +97,7 @@ class SimpleZkapp extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
send: Permissions.proofOrSignature(),
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/simple_zkapp_payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ await isReady;
class SendMINAExample extends SmartContract {
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
editSequenceState: Permissions.proofOrSignature(),
Expand Down
9 changes: 3 additions & 6 deletions src/examples/zkapps/token_with_proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ await isReady;
class TokenContract extends SmartContract {
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
});
Expand All @@ -36,14 +36,11 @@ class TokenContract extends SmartContract {
address,
tokenId
);
AccountUpdate.setValue(deployUpdate.update.permissions, {
deployUpdate.account.permissions.set({
...Permissions.default(),
send: Permissions.proof(),
});
AccountUpdate.setValue(
deployUpdate.update.verificationKey,
verificationKey
);
deployUpdate.account.verificationKey.set(verificationKey);
deployUpdate.sign(deployer);
}

Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/voting/deployContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Voting_ } from './voting.js';
class InvalidContract extends SmartContract {
deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.none(),
editSequenceState: Permissions.none(),
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/voting/dummyContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DummyContract extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
editSequenceState: Permissions.proofOrSignature(),
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/voting/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Membership_ extends SmartContract {

deploy(args: DeployArgs) {
super.deploy(args);
this.setPermissions({
this.account.permissions.set({
...Permissions.default(),
editState: Permissions.proofOrSignature(),
editSequenceState: Permissions.proofOrSignature(),
Expand Down
Loading

0 comments on commit edf229e

Please sign in to comment.