@@ -2,6 +2,38 @@ import { createStore } from "@mina-js/connect";
22import { useLocalStorage , useObjectState } from "@uidotdev/usehooks" ;
33import { clsx } from "clsx" ;
44import { 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+ }
537
638const store = createStore ( ) ;
739
@@ -27,6 +59,7 @@ export const TestZkApp = () => {
2759 mina_signFields : "" ,
2860 mina_signTransaction : "" ,
2961 mina_switchChain : "" ,
62+ mina_sendTransaction : ""
3063 } ) ;
3164 const providers = useSyncExternalStore ( store . subscribe , store . getProviders ) ;
3265 const provider = providers . find (
@@ -141,6 +174,22 @@ export const TestZkApp = () => {
141174 mina_signTransaction : JSON . stringify ( result , undefined , "\t" ) ,
142175 } ) ) ;
143176 } ;
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+ } ;
144193 const switchChain = async ( networkId : string ) => {
145194 if ( ! provider ) return ;
146195 const { result } = await provider . request ( {
@@ -395,6 +444,23 @@ export const TestZkApp = () => {
395444 className = "textarea textarea-bordered h-48 resize-none"
396445 />
397446 </ 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 >
398464 </ div >
399465 </ section >
400466 </ main >
0 commit comments