1
1
import { createStore } from "@mina-js/connect" ;
2
+ import type {
3
+ Sendable ,
4
+ TransactionPayload ,
5
+ ZkAppCommandTransactionInput ,
6
+ } from "@mina-js/utils" ;
2
7
import { useLocalStorage , useObjectState } from "@uidotdev/usehooks" ;
3
8
import { clsx } from "clsx" ;
4
9
import { useState , useSyncExternalStore } from "react" ;
@@ -7,6 +12,12 @@ import {
7
12
samplePresentationRequestHttpsFromExampleUpdated ,
8
13
} from "./sample-data" ;
9
14
15
+ enum TransactionType {
16
+ PAYMENT = "payment" ,
17
+ DELEGATION = "delegation" ,
18
+ ZKAPP = "zkapp" ,
19
+ }
20
+
10
21
const store = createStore ( ) ;
11
22
12
23
const sampleSignFieldsWithPassphrase = {
@@ -29,6 +40,9 @@ export const TestZkApp = () => {
29
40
const [ presentationRequest , setPresentationRequest ] = useState (
30
41
JSON . stringify ( samplePresentationRequestHttpsFromExampleUpdated , null , 2 ) ,
31
42
) ;
43
+ const [ transactionType , setTransactionType ] = useState (
44
+ TransactionType . PAYMENT ,
45
+ ) ;
32
46
const [ transactionBody , setTransactionBody ] = useObjectState ( {
33
47
to : "B62qnVUL6A53E4ZaGd3qbTr6RCtEZYTu3kTijVrrquNpPo4d3MuJ3nb" ,
34
48
amount : "3000000000" ,
@@ -47,6 +61,7 @@ export const TestZkApp = () => {
47
61
mina_switchChain : "" ,
48
62
mina_storePrivateCredential : "" ,
49
63
mina_requestPresentation : "" ,
64
+ mina_sendTransaction : "" ,
50
65
} ) ;
51
66
const providers = useSyncExternalStore ( store . subscribe , store . getProviders ) ;
52
67
const provider = providers . find (
@@ -164,38 +179,61 @@ export const TestZkApp = () => {
164
179
mina_signFields : JSON . stringify ( result , undefined , "\t" ) ,
165
180
} ) ) ;
166
181
} ;
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 ( ) => {
168
203
if ( ! provider ) return ;
169
204
const { result : accounts } = await provider . request ( {
170
205
method : "mina_accounts" ,
171
206
} ) ;
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
+ } ,
175
213
} ;
176
214
const { result } = await provider . request ( {
177
215
method : "mina_signTransaction" ,
178
- params : [ { transaction } ] ,
216
+ params : [ transactionPayload ] ,
179
217
} ) ;
180
218
setResults ( ( ) => ( {
181
219
mina_signTransaction : JSON . stringify ( result , undefined , "\t" ) ,
182
220
} ) ) ;
221
+ setTransactionType ( TransactionType . DELEGATION ) ;
183
222
} ;
184
223
const signZkAppCommand = async ( ) => {
185
224
if ( ! provider ) return ;
186
225
const { result : accounts } = await provider . request ( {
187
226
method : "mina_accounts" ,
188
227
} ) ;
189
- const command = {
228
+ const command : ZkAppCommandTransactionInput = {
190
229
zkappCommand : {
191
230
accountUpdates : [ ] ,
192
231
memo : "E4YM2vTHhWEg66xpj52JErHUBU4pZ1yageL4TVDDpTTSsv8mK6YaH" ,
193
232
feePayer : {
194
233
body : {
195
234
publicKey : accounts [ 0 ] ,
196
- fee : "100000000" ,
197
- validUntil : "100000" ,
198
- nonce : "1" ,
235
+ fee : transactionBody . fee ,
236
+ nonce : transactionBody . nonce ,
199
237
} ,
200
238
authorization : "" ,
201
239
} ,
@@ -213,6 +251,28 @@ export const TestZkApp = () => {
213
251
setResults ( ( ) => ( {
214
252
mina_signTransaction : JSON . stringify ( result , undefined , "\t" ) ,
215
253
} ) ) ;
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
+ } ) ) ;
216
276
} ;
217
277
const switchChain = async ( networkId : string ) => {
218
278
if ( ! provider ) return ;
@@ -449,9 +509,16 @@ export const TestZkApp = () => {
449
509
< button
450
510
type = "button"
451
511
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 }
453
520
>
454
- Sign Transaction
521
+ Sign Delegation
455
522
</ button >
456
523
< button
457
524
type = "button"
@@ -468,6 +535,23 @@ export const TestZkApp = () => {
468
535
className = "textarea textarea-bordered h-48 resize-none"
469
536
/>
470
537
</ 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 >
471
555
</ div >
472
556
</ section >
473
557
< section className = "card bg-neutral" >
0 commit comments