1
1
import { createStore } from "@mina-js/connect" ;
2
2
import { useLocalStorage , useObjectState } from "@uidotdev/usehooks" ;
3
+ import bs58 from "bs58" ;
3
4
import { clsx } from "clsx" ;
4
5
import { useState , useSyncExternalStore } from "react" ;
5
6
import {
6
7
sampleCredentialRecursiveUpdated ,
7
8
samplePresentationRequestHttpsFromExampleUpdated ,
8
9
} from "./sample-data" ;
9
- import bs58 from 'bs58' ;
10
10
11
11
enum TransactionType {
12
12
PAYMENT = "payment" ,
13
13
DELEGATION = "delegation" ,
14
- ZKAPP = "zkapp"
14
+ ZKAPP = "zkapp" ,
15
15
}
16
16
17
17
function bytesToHex ( bytes : Uint8Array ) : string {
18
18
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 ( "" ) ;
21
21
}
22
22
23
- function convertSignature ( signature : string ) : { field : string ; scalar : string } {
23
+ function convertSignature ( signature : string ) : {
24
+ field : string ;
25
+ scalar : string ;
26
+ } {
24
27
// Decode the base58-encoded signature into bytes
25
28
const bytes = bs58 . decode ( signature ) ;
26
29
27
30
// Ensure the byte array can be split into two equal parts
28
31
if ( bytes . length % 2 !== 0 ) {
29
- throw new Error ( ' Invalid signature length.' ) ;
32
+ throw new Error ( " Invalid signature length." ) ;
30
33
}
31
34
32
35
const half = bytes . length / 2 ;
@@ -38,8 +41,8 @@ function convertSignature(signature: string): { field: string; scalar: string }
38
41
const scalarHex = bytesToHex ( scalarBytes ) ;
39
42
40
43
// 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 ) ;
43
46
44
47
// Return the signature object
45
48
return { field, scalar } ;
@@ -67,7 +70,9 @@ export const TestZkApp = () => {
67
70
const [ presentationRequest , setPresentationRequest ] = useState (
68
71
JSON . stringify ( samplePresentationRequestHttpsFromExampleUpdated , null , 2 ) ,
69
72
) ;
70
- const [ transactionType , setTransactionType ] = useState ( TransactionType . PAYMENT )
73
+ const [ transactionType , setTransactionType ] = useState (
74
+ TransactionType . PAYMENT ,
75
+ ) ;
71
76
const [ transactionBody , setTransactionBody ] = useObjectState ( {
72
77
to : "B62qnVUL6A53E4ZaGd3qbTr6RCtEZYTu3kTijVrrquNpPo4d3MuJ3nb" ,
73
78
amount : "3000000000" ,
@@ -86,7 +91,7 @@ export const TestZkApp = () => {
86
91
mina_switchChain : "" ,
87
92
mina_storePrivateCredential : "" ,
88
93
mina_requestPresentation : "" ,
89
- mina_sendTransaction : ""
94
+ mina_sendTransaction : "" ,
90
95
} ) ;
91
96
const providers = useSyncExternalStore ( store . subscribe , store . getProviders ) ;
92
97
const provider = providers . find (
@@ -234,9 +239,8 @@ export const TestZkApp = () => {
234
239
feePayer : {
235
240
body : {
236
241
publicKey : accounts [ 0 ] ,
237
- fee : "100000000" ,
238
- validUntil : "100000" ,
239
- nonce : "1" ,
242
+ fee : transactionBody . fee ,
243
+ nonce : transactionBody . nonce ,
240
244
} ,
241
245
authorization : "" ,
242
246
} ,
@@ -259,19 +263,24 @@ export const TestZkApp = () => {
259
263
const sendTransaction = async ( ) => {
260
264
if ( ! provider ) return ;
261
265
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
+ } ) ) ;
275
284
} ;
276
285
const switchChain = async ( networkId : string ) => {
277
286
if ( ! provider ) return ;
0 commit comments