From 8e8cf88c08c097fc7d1d62fd0cad910cdffc3b0b Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 18 Oct 2024 19:09:09 +0200 Subject: [PATCH] fix getUnconstrained --- src/lib/mina/zkapp.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/mina/zkapp.ts b/src/lib/mina/zkapp.ts index d73520e5a..8720f3355 100644 --- a/src/lib/mina/zkapp.ts +++ b/src/lib/mina/zkapp.ts @@ -867,6 +867,7 @@ super.init(); sender = { self: this as SmartContract, + /** * The public key of the current transaction's sender account. * @@ -878,7 +879,22 @@ super.init(); * Consider using `this.sender.getAndRequireSignature()` if you need to prove that the sender controls this account. */ getUnconstrained(): PublicKey { - let sender = this.getUnconstrained(); + // TODO this logic now has some overlap with this.self, we should combine them somehow + // (but with care since the logic in this.self is a bit more complicated) + if (!Mina.currentTransaction.has()) { + throw Error( + `this.sender is not available outside a transaction. Make sure you only use it within \`Mina.transaction\` blocks or smart contract methods.` + ); + } + let transactionId = Mina.currentTransaction.id(); + let sender; + if (this.self.#_senderState?.transactionId === transactionId) { + sender = this.self.#_senderState.sender; + } else { + sender = Provable.witness(PublicKey, () => Mina.sender()); + this.self.#_senderState = { transactionId, sender }; + } + // we prove that the returned public key is not the empty key, in which case // `createSigned()` would skip adding the account update, and nothing is proved sender.x.assertNotEquals(0);