From e23b345561c2b7d7113846aa0977a352c3eb771a Mon Sep 17 00:00:00 2001 From: TheMonkeyCoder Date: Sat, 2 Nov 2024 00:34:30 +0300 Subject: [PATCH 1/2] feat(connect): add mina_sendTransaction to the test zkapp --- apps/docs/package.json | 1 + apps/docs/src/components/test-zkapp.tsx | 66 ++++++++++++++++++++++++ bun.lockb | Bin 338024 -> 338712 bytes 3 files changed, 67 insertions(+) diff --git a/apps/docs/package.json b/apps/docs/package.json index a7cb445..9f31803 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -12,6 +12,7 @@ "@theguild/remark-mermaid": "0.1.3", "@types/react": "latest", "@uidotdev/usehooks": "^2.4.1", + "bs58": "^6.0.0", "clsx": "^2.1.1", "lucide-react": "0.454.0", "react": "18.3.1", diff --git a/apps/docs/src/components/test-zkapp.tsx b/apps/docs/src/components/test-zkapp.tsx index fb24a28..b2e942d 100644 --- a/apps/docs/src/components/test-zkapp.tsx +++ b/apps/docs/src/components/test-zkapp.tsx @@ -2,6 +2,38 @@ import { createStore } from "@mina-js/connect"; import { useLocalStorage, useObjectState } from "@uidotdev/usehooks"; import { clsx } from "clsx"; import { useState, useSyncExternalStore } from "react"; +import bs58 from 'bs58'; + +function bytesToHex(bytes: Uint8Array): string { + return Array.from(bytes) + .map((byte) => byte.toString(16).padStart(2, '0')) + .join(''); +} + +function convertSignature(signature: string): { field: string; scalar: string } { + // Decode the base58-encoded signature into bytes + const bytes = bs58.decode(signature); + + // Ensure the byte array can be split into two equal parts + if (bytes.length % 2 !== 0) { + throw new Error('Invalid signature length.'); + } + + const half = bytes.length / 2; + const fieldBytes = bytes.slice(0, half); + const scalarBytes = bytes.slice(half); + + // Convert bytes to hexadecimal strings + const fieldHex = bytesToHex(fieldBytes); + const scalarHex = bytesToHex(scalarBytes); + + // Convert hexadecimal strings to decimal strings + const field = BigInt('0x' + fieldHex).toString(10); + const scalar = BigInt('0x' + scalarHex).toString(10); + + // Return the signature object + return { field, scalar }; +} const store = createStore(); @@ -27,6 +59,7 @@ export const TestZkApp = () => { mina_signFields: "", mina_signTransaction: "", mina_switchChain: "", + mina_sendTransaction: "" }); const providers = useSyncExternalStore(store.subscribe, store.getProviders); const provider = providers.find( @@ -141,6 +174,22 @@ export const TestZkApp = () => { mina_signTransaction: JSON.stringify(result, undefined, "\t"), })); }; + const sendTransaction = async () => { + if (!provider) return; + if (!results.mina_signTransaction) return; + const signedTransaction = JSON.parse(results.mina_signTransaction) + const { result } = await provider.request({ + method: "mina_sendTransaction", + params: [{ + ...signedTransaction, + signature: typeof signedTransaction.signature === "string" ? + convertSignature(signedTransaction.signature) : signedTransaction.signature + }], + }); + setResults(() => ({ + mina_sendTransaction: JSON.stringify(result, undefined, "\t"), + })); + }; const switchChain = async (networkId: string) => { if (!provider) return; const { result } = await provider.request({ @@ -395,6 +444,23 @@ export const TestZkApp = () => { className="textarea textarea-bordered h-48 resize-none" /> +
+ +
+
+ +