Skip to content

Commit

Permalink
feat(connect): add mina_sendTransaction to the test zkapp
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMonkeyCoder committed Nov 6, 2024
1 parent 758fc6f commit e23b345
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
66 changes: 66 additions & 0 deletions apps/docs/src/components/test-zkapp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -395,6 +444,23 @@ export const TestZkApp = () => {
className="textarea textarea-bordered h-48 resize-none"
/>
</div>
<div className="flex gap-4">
<button
type="button"
className="btn btn-primary flex-1"
disabled={!results.mina_signTransaction}
onClick={sendTransaction}
>
Send Transaction
</button>
</div>
<div className="flex flex-col gap-2">
<label>Result</label>
<textarea
value={results.mina_sendTransaction}
className="textarea textarea-bordered h-48 resize-none"
/>
</div>
</div>
</section>
</main>
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit e23b345

Please sign in to comment.