Skip to content

Commit

Permalink
Merge pull request #1567 from harrysolovay/prove-in-chain
Browse files Browse the repository at this point in the history
represent whether signed and/or proven within `Transaction` type + rework `prove` method
  • Loading branch information
MartinMinkov authored Apr 17, 2024
2 parents f74abb3 + 8e76633 commit 2f83cbb
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 358 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `Mina.LocalBlockchain` no longer supports the network kind configuration https://github.com/o1-labs/o1js/pull/1581
- `Poseidon.hashToGroup()` now returns a `Group` directly, and constrains it to be deterministic https://github.com/o1-labs/o1js/pull/1546
- Added `Poseidon.Unsafe.hashToGroup()` as a more efficient, non-deterministic version for advanced use cases
- A `Transaction`'s `prove` method no longer returns the proofs promise directly, but rather returns a `Transaction` promise, the resolved value of which contains a `proofs` prop. https://github.com/o1-labs/o1js/pull/1567
- The `Transaction` type now has two type params `Proven extends boolean` and `Signed extends boolean`, which are used to conditionally show/hide relevant state. https://github.com/o1-labs/o1js/pull/1567

### Added

- Export `Events` under `AccountUpdate.Events`. https://github.com/o1-labs/o1js/pull/1563
- `Mina.transaction` has been reworked such that one can call methods directly on the returned promise (now a `TransactionPromise`). This enables a fluent / method-chaining API. https://github.com/o1-labs/o1js/pull/1567
- `TransactionPendingPromise` enables calling `wait` directly on the promise returned by calling `send` on a `Transaction`. https://github.com/o1-labs/o1js/pull/1567
- `initializeBindings()` to explicitly trigger setup work that is needed when running provable code https://github.com/o1-labs/o1js/pull/1583
- calling this function is optional

Expand Down
2 changes: 1 addition & 1 deletion src/bindings
Submodule bindings updated 0 files
20 changes: 16 additions & 4 deletions src/examples/chaining-tx-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SmartContract,
Mina,
AccountUpdate,
TransactionPromise,
} from 'o1js';

class SimpleZkapp extends SmartContract {
Expand Down Expand Up @@ -52,10 +53,21 @@ await Mina.transaction(sender, async () => {
console.log('initial state: ' + zkapp.x.get());

console.log('increment');
const incrementTx = Mina.transaction(sender, async () => {

await Mina.transaction(sender, async () => {
await zkapp.increment();
}).sign([sender.key]);
await incrementTx.then((v) => v.prove());
await incrementTx.send().wait();
})
.prove()
.sign([sender.key])
.send()
.wait();

console.log('final state: ' + zkapp.x.get());

const a = Mina.transaction(sender, async () => {
await zkapp.increment();
});
a satisfies TransactionPromise<false, false>;
const b = a.prove() satisfies TransactionPromise<true, false>;
const c = b.sign([sender.key]) satisfies TransactionPromise<true, true>;
await c.send().wait();
1 change: 0 additions & 1 deletion src/examples/simple-zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ tx = await Mina.transaction(sender, async () => {
});
await tx.prove();
await tx.sign([sender.key]).send();
sender;

console.log('final state: ' + zkapp.x.get());
console.log(`final balance: ${zkapp.account.balance.get().div(1e9)} MINA`);
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/hello-world/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ let txn, txn2, txn3, txn4;
let Local = await Mina.LocalBlockchain({ proofsEnabled: false });
Mina.setActiveInstance(Local);

// test accounts that pays all the fees, and puts additional funds into the zkapp
// test accounts that pays all the fees, and puts additional funds into the contract
const [feePayer1, feePayer2, feePayer3, feePayer4] = Local.testAccounts;

// zkapp account
// contract account
const contractAccount = Mina.TestPublicKey.random();
const contract = new HelloWorld(contractAccount);

Expand Down
45 changes: 26 additions & 19 deletions src/examples/zkapps/simple-zkapp-with-proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ let { verificationKey: trivialVerificationKey } = await TrivialZkapp.compile();
// submitting transactions? or is this an irrelevant use case?
// would also improve the return type -- `Proof` instead of `(Proof | undefined)[]`
console.log('prove (trivial zkapp)');
let [trivialProof] = await (
await Mina.transaction(feePayer, async () => {
await new TrivialZkapp(notSoSimpleContractAccount).proveSomething(Field(1));
})
).prove();
let [trivialProof] = await Mina.transaction(feePayer, async () => {
await new TrivialZkapp(notSoSimpleContractAccount).proveSomething(Field(1));
})
.prove()
.proofs();

trivialProof = await testJsonRoundtripAndVerify(
TrivialProof,
Expand All @@ -73,19 +73,22 @@ let { verificationKey } = await NotSoSimpleZkapp.compile();
let zkapp = new NotSoSimpleZkapp(trivialContractAccount);

console.log('deploy');
let tx = await Mina.transaction(feePayer, async () => {
await Mina.transaction(feePayer, async () => {
AccountUpdate.fundNewAccount(feePayer);
await zkapp.deploy();
});
await tx.prove();
await tx.sign([feePayer.key, trivialContractAccount.key]).send();
})
.prove()
.sign([feePayer.key, trivialContractAccount.key])
.send();

console.log('initialize');
tx = await Mina.transaction(feePayer, async () => {
let tx = await Mina.transaction(feePayer, async () => {
await zkapp.initialize(trivialProof!);
});
let [proof] = await tx.prove();
await tx.sign([feePayer.key]).send();
})
.prove()
.sign([feePayer.key]);
let [proof] = tx.proofs;
await tx.send();

proof = await testJsonRoundtripAndVerify(
NotSoSimpleZkapp.Proof(),
Expand All @@ -98,9 +101,11 @@ console.log('initial state: ' + zkapp.x.get());
console.log('update');
tx = await Mina.transaction(feePayer, async () => {
await zkapp.update(Field(3), proof!, trivialProof!);
});
[proof] = await tx.prove();
await tx.sign([feePayer.key]).send();
})
.prove()
.sign([feePayer.key]);
[proof] = tx.proofs;
await tx.send();

proof = await testJsonRoundtripAndVerify(
NotSoSimpleZkapp.Proof(),
Expand All @@ -113,9 +118,11 @@ console.log('state 2: ' + zkapp.x.get());
console.log('update');
tx = await Mina.transaction(feePayer, async () => {
await zkapp.update(Field(3), proof!, trivialProof!);
});
[proof] = await tx.prove();
await tx.sign([feePayer.key]).send();
})
.prove()
.sign([feePayer.key]);
[proof] = tx.proofs;
await tx.send();

proof = await testJsonRoundtripAndVerify(
NotSoSimpleZkapp.Proof(),
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export { MerkleList, MerkleListIterator } from './lib/provable/merkle-list.js';

export * as Mina from './lib/mina/mina.js';
export {
type Transaction,
Transaction,
type TransactionPromise,
type PendingTransaction,
type IncludedTransaction,
Expand Down
Loading

0 comments on commit 2f83cbb

Please sign in to comment.