Skip to content

Commit 08bf704

Browse files
committed
feat(connect): add mina_sendTransaction to the test zkapp
1 parent a51d3b4 commit 08bf704

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

apps/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@theguild/remark-mermaid": "0.1.3",
1313
"@types/react": "latest",
1414
"@uidotdev/usehooks": "^2.4.1",
15+
"bs58": "^6.0.0",
1516
"clsx": "^2.1.1",
1617
"lucide-react": "0.454.0",
1718
"react": "18.3.1",

apps/docs/src/components/test-zkapp.tsx

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@ import { createStore } from "@mina-js/connect";
22
import { useLocalStorage, useObjectState } from "@uidotdev/usehooks";
33
import { clsx } from "clsx";
44
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+
}
537

638
const store = createStore();
739

@@ -26,6 +58,7 @@ export const TestZkApp = () => {
2658
mina_sign: "",
2759
mina_signFields: "",
2860
mina_signTransaction: "",
61+
mina_sendTransaction: ""
2962
});
3063
const providers = useSyncExternalStore(store.subscribe, store.getProviders);
3164
const provider = providers.find(
@@ -140,6 +173,22 @@ export const TestZkApp = () => {
140173
mina_signTransaction: JSON.stringify(result, undefined, "\t"),
141174
}));
142175
};
176+
const sendTransaction = async () => {
177+
if (!provider) return;
178+
if (!results.mina_signTransaction) return;
179+
const signedTransaction = JSON.parse(results.mina_signTransaction)
180+
const { result } = await provider.request({
181+
method: "mina_sendTransaction",
182+
params: [{
183+
...signedTransaction,
184+
signature: typeof signedTransaction.signature === "string" ?
185+
convertSignature(signedTransaction.signature) : signedTransaction.signature
186+
}],
187+
});
188+
setResults(() => ({
189+
mina_sendTransaction: JSON.stringify(result, undefined, "\t"),
190+
}));
191+
};
143192
return (
144193
<main className="flex flex-col gap-8">
145194
<h1 className="text-3xl font-bold">Test zkApp</h1>
@@ -302,39 +351,39 @@ export const TestZkApp = () => {
302351
<input
303352
value={transactionBody.to}
304353
onChange={(event) =>
305-
setTransactionBody(() => ({ to: event.target.value }))
354+
setTransactionBody(() => ({to: event.target.value}))
306355
}
307356
className="input input-bordered"
308357
/>
309358
<label>Amount</label>
310359
<input
311360
value={transactionBody.amount}
312361
onChange={(event) =>
313-
setTransactionBody(() => ({ amount: event.target.value }))
362+
setTransactionBody(() => ({amount: event.target.value}))
314363
}
315364
className="input input-bordered"
316365
/>
317366
<label>Fee</label>
318367
<input
319368
value={transactionBody.fee}
320369
onChange={(event) =>
321-
setTransactionBody(() => ({ fee: event.target.value }))
370+
setTransactionBody(() => ({fee: event.target.value}))
322371
}
323372
className="input input-bordered"
324373
/>
325374
<label>Memo</label>
326375
<input
327376
value={transactionBody.memo}
328377
onChange={(event) =>
329-
setTransactionBody(() => ({ memo: event.target.value }))
378+
setTransactionBody(() => ({memo: event.target.value}))
330379
}
331380
className="input input-bordered"
332381
/>
333382
<label>Nonce</label>
334383
<input
335384
value={transactionBody.nonce}
336385
onChange={(event) =>
337-
setTransactionBody(() => ({ nonce: event.target.value }))
386+
setTransactionBody(() => ({nonce: event.target.value}))
338387
}
339388
className="input input-bordered"
340389
/>
@@ -362,6 +411,23 @@ export const TestZkApp = () => {
362411
className="textarea textarea-bordered h-48 resize-none"
363412
/>
364413
</div>
414+
<div className="flex gap-4">
415+
<button
416+
type="button"
417+
className="btn btn-primary flex-1"
418+
disabled={!results.mina_signTransaction}
419+
onClick={sendTransaction}
420+
>
421+
Send Transaction
422+
</button>
423+
</div>
424+
<div className="flex flex-col gap-2">
425+
<label>Result</label>
426+
<textarea
427+
value={results.mina_sendTransaction}
428+
className="textarea textarea-bordered h-48 resize-none"
429+
/>
430+
</div>
365431
</div>
366432
</section>
367433
</main>

bun.lockb

688 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)