11import { createStore } from "@mina-js/connect" ;
22import { useLocalStorage , useObjectState } from "@uidotdev/usehooks" ;
3+ import bs58 from "bs58" ;
34import { clsx } from "clsx" ;
45import { useState , useSyncExternalStore } from "react" ;
56import {
67 sampleCredentialRecursiveUpdated ,
78 samplePresentationRequestHttpsFromExampleUpdated ,
89} from "./sample-data" ;
9- import bs58 from 'bs58' ;
1010
1111enum TransactionType {
1212 PAYMENT = "payment" ,
1313 DELEGATION = "delegation" ,
14- ZKAPP = "zkapp"
14+ ZKAPP = "zkapp" ,
1515}
1616
1717function bytesToHex ( bytes : Uint8Array ) : string {
1818 return Array . from ( bytes )
19- . map ( ( byte ) => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
20- . join ( '' ) ;
19+ . map ( ( byte ) => byte . toString ( 16 ) . padStart ( 2 , "0" ) )
20+ . join ( "" ) ;
2121}
2222
23- function convertSignature ( signature : string ) : { field : string ; scalar : string } {
23+ function convertSignature ( signature : string ) : {
24+ field : string ;
25+ scalar : string ;
26+ } {
2427 // Decode the base58-encoded signature into bytes
2528 const bytes = bs58 . decode ( signature ) ;
2629
2730 // Ensure the byte array can be split into two equal parts
2831 if ( bytes . length % 2 !== 0 ) {
29- throw new Error ( ' Invalid signature length.' ) ;
32+ throw new Error ( " Invalid signature length." ) ;
3033 }
3134
3235 const half = bytes . length / 2 ;
@@ -38,8 +41,8 @@ function convertSignature(signature: string): { field: string; scalar: string }
3841 const scalarHex = bytesToHex ( scalarBytes ) ;
3942
4043 // Convert hexadecimal strings to decimal strings
41- const field = BigInt ( '0x' + fieldHex ) . toString ( 10 ) ;
42- const scalar = BigInt ( '0x' + scalarHex ) . toString ( 10 ) ;
44+ const field = BigInt ( `0x ${ fieldHex } ` ) . toString ( 10 ) ;
45+ const scalar = BigInt ( `0x ${ scalarHex } ` ) . toString ( 10 ) ;
4346
4447 // Return the signature object
4548 return { field, scalar } ;
@@ -67,7 +70,9 @@ export const TestZkApp = () => {
6770 const [ presentationRequest , setPresentationRequest ] = useState (
6871 JSON . stringify ( samplePresentationRequestHttpsFromExampleUpdated , null , 2 ) ,
6972 ) ;
70- const [ transactionType , setTransactionType ] = useState ( TransactionType . PAYMENT )
73+ const [ transactionType , setTransactionType ] = useState (
74+ TransactionType . PAYMENT ,
75+ ) ;
7176 const [ transactionBody , setTransactionBody ] = useObjectState ( {
7277 to : "B62qnVUL6A53E4ZaGd3qbTr6RCtEZYTu3kTijVrrquNpPo4d3MuJ3nb" ,
7378 amount : "3000000000" ,
@@ -86,7 +91,7 @@ export const TestZkApp = () => {
8691 mina_switchChain : "" ,
8792 mina_storePrivateCredential : "" ,
8893 mina_requestPresentation : "" ,
89- mina_sendTransaction : ""
94+ mina_sendTransaction : "" ,
9095 } ) ;
9196 const providers = useSyncExternalStore ( store . subscribe , store . getProviders ) ;
9297 const provider = providers . find (
@@ -234,9 +239,8 @@ export const TestZkApp = () => {
234239 feePayer : {
235240 body : {
236241 publicKey : accounts [ 0 ] ,
237- fee : "100000000" ,
238- validUntil : "100000" ,
239- nonce : "1" ,
242+ fee : transactionBody . fee ,
243+ nonce : transactionBody . nonce ,
240244 } ,
241245 authorization : "" ,
242246 } ,
@@ -259,19 +263,24 @@ export const TestZkApp = () => {
259263 const sendTransaction = async ( ) => {
260264 if ( ! provider ) return ;
261265 if ( ! results . mina_signTransaction ) return ;
262- const signedTransaction = JSON . parse ( results . mina_signTransaction )
263- if ( transactionType === TransactionType . PAYMENT ) {
264- const { result } = await provider . request ( {
265- method : "mina_sendTransaction" ,
266- params : [ {
267- input : signedTransaction . data ,
268- signature : signedTransaction . signature
269- } , "payment" ] ,
270- } ) ;
271- setResults ( ( ) => ( {
272- mina_sendTransaction : JSON . stringify ( result , undefined , "\t" ) ,
273- } ) ) ;
274- }
266+ const signedTransaction = JSON . parse ( results . mina_signTransaction ) ;
267+ const { result } = await provider . request ( {
268+ method : "mina_sendTransaction" ,
269+ params : [
270+ transactionType === TransactionType . ZKAPP
271+ ? {
272+ input : signedTransaction . data ,
273+ }
274+ : {
275+ input : signedTransaction . data ,
276+ signature : signedTransaction . signature ,
277+ } ,
278+ "zkapp" ,
279+ ] ,
280+ } ) ;
281+ setResults ( ( ) => ( {
282+ mina_sendTransaction : JSON . stringify ( result , undefined , "\t" ) ,
283+ } ) ) ;
275284 } ;
276285 const switchChain = async ( networkId : string ) => {
277286 if ( ! provider ) return ;
0 commit comments