Skip to content

Commit 4c83cf0

Browse files
feat(connect): add mina_sendTransaction to the test zkapp (#2)
1 parent 7f3160c commit 4c83cf0

File tree

1 file changed

+95
-11
lines changed

1 file changed

+95
-11
lines changed

apps/docs/src/components/test-zkapp.tsx

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { createStore } from "@mina-js/connect";
2+
import type {
3+
Sendable,
4+
TransactionPayload,
5+
ZkAppCommandTransactionInput,
6+
} from "@mina-js/utils";
27
import { useLocalStorage, useObjectState } from "@uidotdev/usehooks";
38
import { clsx } from "clsx";
49
import { useState, useSyncExternalStore } from "react";
@@ -7,6 +12,12 @@ import {
712
samplePresentationRequestHttpsFromExampleUpdated,
813
} from "./sample-data";
914

15+
enum TransactionType {
16+
PAYMENT = "payment",
17+
DELEGATION = "delegation",
18+
ZKAPP = "zkapp",
19+
}
20+
1021
const store = createStore();
1122

1223
const sampleSignFieldsWithPassphrase = {
@@ -29,6 +40,9 @@ export const TestZkApp = () => {
2940
const [presentationRequest, setPresentationRequest] = useState(
3041
JSON.stringify(samplePresentationRequestHttpsFromExampleUpdated, null, 2),
3142
);
43+
const [transactionType, setTransactionType] = useState(
44+
TransactionType.PAYMENT,
45+
);
3246
const [transactionBody, setTransactionBody] = useObjectState({
3347
to: "B62qnVUL6A53E4ZaGd3qbTr6RCtEZYTu3kTijVrrquNpPo4d3MuJ3nb",
3448
amount: "3000000000",
@@ -47,6 +61,7 @@ export const TestZkApp = () => {
4761
mina_switchChain: "",
4862
mina_storePrivateCredential: "",
4963
mina_requestPresentation: "",
64+
mina_sendTransaction: "",
5065
});
5166
const providers = useSyncExternalStore(store.subscribe, store.getProviders);
5267
const provider = providers.find(
@@ -164,38 +179,61 @@ export const TestZkApp = () => {
164179
mina_signFields: JSON.stringify(result, undefined, "\t"),
165180
}));
166181
};
167-
const signTransaction = async () => {
182+
const signPayment = async () => {
183+
if (!provider) return;
184+
const { result: accounts } = await provider.request({
185+
method: "mina_accounts",
186+
});
187+
const transactionPayload = {
188+
transaction: {
189+
...transactionBody,
190+
from: accounts[0],
191+
},
192+
};
193+
const { result } = await provider.request({
194+
method: "mina_signTransaction",
195+
params: [transactionPayload],
196+
});
197+
setResults(() => ({
198+
mina_signTransaction: JSON.stringify(result, undefined, "\t"),
199+
}));
200+
setTransactionType(TransactionType.PAYMENT);
201+
};
202+
const signDelegation = async () => {
168203
if (!provider) return;
169204
const { result: accounts } = await provider.request({
170205
method: "mina_accounts",
171206
});
172-
const transaction = {
173-
...transactionBody,
174-
from: accounts[0],
207+
const { amount, ...rest } = transactionBody;
208+
const transactionPayload: TransactionPayload = {
209+
transaction: {
210+
...rest,
211+
from: accounts[0],
212+
},
175213
};
176214
const { result } = await provider.request({
177215
method: "mina_signTransaction",
178-
params: [{ transaction }],
216+
params: [transactionPayload],
179217
});
180218
setResults(() => ({
181219
mina_signTransaction: JSON.stringify(result, undefined, "\t"),
182220
}));
221+
setTransactionType(TransactionType.DELEGATION);
183222
};
184223
const signZkAppCommand = async () => {
185224
if (!provider) return;
186225
const { result: accounts } = await provider.request({
187226
method: "mina_accounts",
188227
});
189-
const command = {
228+
const command: ZkAppCommandTransactionInput = {
190229
zkappCommand: {
191230
accountUpdates: [],
192231
memo: "E4YM2vTHhWEg66xpj52JErHUBU4pZ1yageL4TVDDpTTSsv8mK6YaH",
193232
feePayer: {
194233
body: {
195234
publicKey: accounts[0],
196-
fee: "100000000",
197-
validUntil: "100000",
198-
nonce: "1",
235+
fee: transactionBody.fee,
236+
nonce: transactionBody.nonce,
199237
},
200238
authorization: "",
201239
},
@@ -213,6 +251,28 @@ export const TestZkApp = () => {
213251
setResults(() => ({
214252
mina_signTransaction: JSON.stringify(result, undefined, "\t"),
215253
}));
254+
setTransactionType(TransactionType.ZKAPP);
255+
};
256+
const sendTransaction = async () => {
257+
if (!provider) return;
258+
if (!results.mina_signTransaction) return;
259+
const signedTransaction = JSON.parse(results.mina_signTransaction);
260+
const transaction: Sendable =
261+
transactionType === TransactionType.ZKAPP
262+
? {
263+
input: signedTransaction.data,
264+
}
265+
: {
266+
input: signedTransaction.data,
267+
signature: signedTransaction.signature,
268+
};
269+
const { result } = await provider.request({
270+
method: "mina_sendTransaction",
271+
params: [transaction, transactionType],
272+
});
273+
setResults(() => ({
274+
mina_sendTransaction: JSON.stringify(result, undefined, "\t"),
275+
}));
216276
};
217277
const switchChain = async (networkId: string) => {
218278
if (!provider) return;
@@ -449,9 +509,16 @@ export const TestZkApp = () => {
449509
<button
450510
type="button"
451511
className="btn btn-primary flex-1"
452-
onClick={signTransaction}
512+
onClick={signPayment}
513+
>
514+
Sign Payment
515+
</button>
516+
<button
517+
type="button"
518+
className="btn btn-primary flex-1"
519+
onClick={signDelegation}
453520
>
454-
Sign Transaction
521+
Sign Delegation
455522
</button>
456523
<button
457524
type="button"
@@ -468,6 +535,23 @@ export const TestZkApp = () => {
468535
className="textarea textarea-bordered h-48 resize-none"
469536
/>
470537
</div>
538+
<div className="flex gap-4">
539+
<button
540+
type="button"
541+
className="btn btn-primary flex-1"
542+
disabled={!results.mina_signTransaction}
543+
onClick={sendTransaction}
544+
>
545+
Send Transaction
546+
</button>
547+
</div>
548+
<div className="flex flex-col gap-2">
549+
<label>Result</label>
550+
<textarea
551+
value={results.mina_sendTransaction}
552+
className="textarea textarea-bordered h-48 resize-none"
553+
/>
554+
</div>
471555
</div>
472556
</section>
473557
<section className="card bg-neutral">

0 commit comments

Comments
 (0)