@@ -2,6 +2,38 @@ import { createStore } from "@mina-js/connect";
2
2
import { useLocalStorage , useObjectState } from "@uidotdev/usehooks" ;
3
3
import { clsx } from "clsx" ;
4
4
import { useState , useSyncExternalStore } from "react" ;
5
+ import bs58 from 'bs58' ;
6
+
7
+ function bytesToHex ( bytes : Uint8Array ) : string {
8
+ return Array . from ( bytes )
9
+ . map ( ( byte ) => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
10
+ . join ( '' ) ;
11
+ }
12
+
13
+ function convertSignature ( signature : string ) : { field : string ; scalar : string } {
14
+ // Decode the base58-encoded signature into bytes
15
+ const bytes = bs58 . decode ( signature ) ;
16
+
17
+ // Ensure the byte array can be split into two equal parts
18
+ if ( bytes . length % 2 !== 0 ) {
19
+ throw new Error ( 'Invalid signature length.' ) ;
20
+ }
21
+
22
+ const half = bytes . length / 2 ;
23
+ const fieldBytes = bytes . slice ( 0 , half ) ;
24
+ const scalarBytes = bytes . slice ( half ) ;
25
+
26
+ // Convert bytes to hexadecimal strings
27
+ const fieldHex = bytesToHex ( fieldBytes ) ;
28
+ const scalarHex = bytesToHex ( scalarBytes ) ;
29
+
30
+ // Convert hexadecimal strings to decimal strings
31
+ const field = BigInt ( '0x' + fieldHex ) . toString ( 10 ) ;
32
+ const scalar = BigInt ( '0x' + scalarHex ) . toString ( 10 ) ;
33
+
34
+ // Return the signature object
35
+ return { field, scalar } ;
36
+ }
5
37
6
38
const store = createStore ( ) ;
7
39
@@ -27,6 +59,7 @@ export const TestZkApp = () => {
27
59
mina_signFields : "" ,
28
60
mina_signTransaction : "" ,
29
61
mina_switchChain : "" ,
62
+ mina_sendTransaction : ""
30
63
} ) ;
31
64
const providers = useSyncExternalStore ( store . subscribe , store . getProviders ) ;
32
65
const provider = providers . find (
@@ -141,6 +174,22 @@ export const TestZkApp = () => {
141
174
mina_signTransaction : JSON . stringify ( result , undefined , "\t" ) ,
142
175
} ) ) ;
143
176
} ;
177
+ const sendTransaction = async ( ) => {
178
+ if ( ! provider ) return ;
179
+ if ( ! results . mina_signTransaction ) return ;
180
+ const signedTransaction = JSON . parse ( results . mina_signTransaction )
181
+ const { result } = await provider . request ( {
182
+ method : "mina_sendTransaction" ,
183
+ params : [ {
184
+ ...signedTransaction ,
185
+ signature : typeof signedTransaction . signature === "string" ?
186
+ convertSignature ( signedTransaction . signature ) : signedTransaction . signature
187
+ } ] ,
188
+ } ) ;
189
+ setResults ( ( ) => ( {
190
+ mina_sendTransaction : JSON . stringify ( result , undefined , "\t" ) ,
191
+ } ) ) ;
192
+ } ;
144
193
const switchChain = async ( networkId : string ) => {
145
194
if ( ! provider ) return ;
146
195
const { result } = await provider . request ( {
@@ -395,6 +444,23 @@ export const TestZkApp = () => {
395
444
className = "textarea textarea-bordered h-48 resize-none"
396
445
/>
397
446
</ div >
447
+ < div className = "flex gap-4" >
448
+ < button
449
+ type = "button"
450
+ className = "btn btn-primary flex-1"
451
+ disabled = { ! results . mina_signTransaction }
452
+ onClick = { sendTransaction }
453
+ >
454
+ Send Transaction
455
+ </ button >
456
+ </ div >
457
+ < div className = "flex flex-col gap-2" >
458
+ < label > Result</ label >
459
+ < textarea
460
+ value = { results . mina_sendTransaction }
461
+ className = "textarea textarea-bordered h-48 resize-none"
462
+ />
463
+ </ div >
398
464
</ div >
399
465
</ section >
400
466
</ main >
0 commit comments