Skip to content

Commit e23b345

Browse files
committed
feat(connect): add mina_sendTransaction to the test zkapp
1 parent 758fc6f commit e23b345

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
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: 66 additions & 0 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

@@ -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>

bun.lockb

688 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)