Skip to content

Commit 9a9de63

Browse files
committed
Merge remote-tracking branch 'upstream/main' into feature/presentation
2 parents de77460 + 2be76ea commit 9a9de63

24 files changed

+311
-16
lines changed

apps/docs/src/pages/utils.mdx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Utils [Don't waste time on implementing these yourself.]
2+
3+
We've implemented some utilities that you might find useful. You can find them in the `@mina-js/utils` package.
4+
5+
## Units
6+
7+
Utils library provides functions for unit conversion.
8+
9+
### formatMina
10+
11+
Formats micro-Mina to human-readable Mina value.
12+
13+
```typescript twoslash
14+
import { formatMina } from '@mina-js/utils'
15+
16+
const mina = formatMina(5_000_000_000n)
17+
// -> "5"
18+
```
19+
20+
### parseMina
21+
22+
Parses human-readable Mina value to micro-Mina.
23+
24+
```typescript twoslash
25+
import { parseMina } from '@mina-js/utils'
26+
27+
const mina = parseMina('5')
28+
// -> 5_000_000_000n
29+
```
30+
31+
### formatUnit
32+
33+
```typescript twoslash
34+
import { formatUnits } from '@mina-js/utils'
35+
36+
const formatted = formatUnits(4200000000000n, 10)
37+
// -> "420"
38+
```
39+
40+
### parseUnits
41+
42+
```typescript twoslash
43+
import { parseUnits } from '@mina-js/utils'
44+
45+
const parsed = parseUnits("420", 10)
46+
// -> 4200000000000n
47+
```
48+
49+
## Web Workers
50+
51+
Proof related computations can be heavy and can block the main thread. To avoid this, you can use Web Workers to run these computations in a separate thread. We've prepared a JSON-RPC protocol to easily connect the dots.
52+
53+
```typescript twoslash
54+
// @filename: worker.ts
55+
import { createRpcHandler } from "@mina-js/utils";
56+
57+
const { messageHandler } = createRpcHandler({
58+
methods: {
59+
ping: async () => 'pong',
60+
}
61+
})
62+
63+
self.onmessage = messageHandler
64+
65+
// @filename: index.ts
66+
import { createRpc } from "@mina-js/utils";
67+
68+
const worker = new Worker(new URL('./worker.ts', import.meta.url))
69+
const rpc = createRpc({ worker })
70+
const response = await rpc.request({
71+
method: 'ping',
72+
params: [],
73+
})
74+
```

apps/docs/vocs.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export default defineConfig({
8181
text: "Roadmap",
8282
link: "/roadmap",
8383
},
84+
{
85+
text: "Utils",
86+
link: "/utils",
87+
},
8488
{
8589
text: "MinaJS Connect",
8690
link: "/connect",

apps/klesia/src/methods/mina.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const networkId = async () => {
7878
`,
7979
{},
8080
);
81-
return data.networkID;
81+
return data.networkID === "mina:testnet" ? "mina:devnet" : data.networkID;
8282
};
8383

8484
const sendTransaction = async ({

bun.lockb

1.75 KB
Binary file not shown.

packages/accounts/src/accounts/__snapshots__/private-key-to-account.spec.ts.snap

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ exports[`signs fields 1`] = `
5353
"signature": "7mXCUvhLhFvG9ptrdfNceCrpThkCUyg1ct2z8uwY7eQbKz7UNmhv33TbuDjTznaypJtXRiMJyQWDnf27TH1FSXG7uJHTKAd9",
5454
}
5555
`;
56+
57+
exports[`signs a zkapp command 1`] = `
58+
{
59+
"data": {
60+
"feePayer": {
61+
"fee": "100000000",
62+
"feePayer": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
63+
"memo": "Test",
64+
"nonce": "0",
65+
"validUntil": "null",
66+
},
67+
"zkappCommand": {
68+
"accountUpdates": [],
69+
"feePayer": {
70+
"authorization": "7mXWqNfmqMTM5uSCS2xLfsRBLTjGZKTtpEakdsrdQz1EUgYXogSvKxxtfGbBkqQ2mZRMA3uPAM8riaCF56pkqpZBLr2kNBLa",
71+
"body": {
72+
"fee": "100000000",
73+
"nonce": "0",
74+
"publicKey": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
75+
"validUntil": null,
76+
},
77+
},
78+
"memo": "E4YVT4x3A9rUhmjkjGn8ZYBLZn7zK4cfvnMtBYZFdWkg37n2s3nrP",
79+
},
80+
},
81+
"publicKey": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
82+
"signature": "7mXWqNfmqMTM5uSCS2xLfsRBLTjGZKTtpEakdsrdQz1EUgYXogSvKxxtfGbBkqQ2mZRMA3uPAM8riaCF56pkqpZBLr2kNBLa",
83+
}
84+
`;

packages/accounts/src/accounts/private-key-to-account.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ it("signs a transaction", async () => {
3333
expect(signedTransaction).toMatchSnapshot();
3434
});
3535

36+
it("signs a zkapp command", async () => {
37+
const account = privateKeyToAccount({
38+
privateKey: Test.accounts[0].privateKey,
39+
});
40+
const command = {
41+
zkappCommand: {
42+
accountUpdates: [],
43+
memo: "E4YM2vTHhWEg66xpj52JErHUBU4pZ1yageL4TVDDpTTSsv8mK6YaH",
44+
feePayer: {
45+
body: {
46+
publicKey: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
47+
fee: "100000000",
48+
validUntil: "100000",
49+
nonce: "1",
50+
},
51+
authorization: "",
52+
},
53+
},
54+
feePayer: {
55+
feePayer: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
56+
fee: "100000000",
57+
nonce: "0",
58+
memo: "Test",
59+
},
60+
};
61+
const signedTransaction = await account.signTransaction({ command });
62+
expect(signedTransaction).toMatchSnapshot();
63+
});
64+
3665
it("creates a nullifier", async () => {
3766
const account = privateKeyToAccount({
3867
privateKey: Test.accounts[0].privateKey,

packages/accounts/src/accounts/private-key-to-account.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ export function privateKeyToAccount({
2929
async signMessage({ message }) {
3030
return SignedMessageSchema.parse(client.signMessage(message, privateKey));
3131
},
32-
async signTransaction({ transaction }) {
32+
async signTransaction(signable) {
33+
if ("transaction" in signable) {
34+
return SignedTransactionSchema.parse(
35+
client.signTransaction(signable.transaction, privateKey),
36+
);
37+
}
3338
return SignedTransactionSchema.parse(
34-
client.signTransaction(transaction, privateKey),
39+
client.signTransaction(signable.command as never, privateKey),
3540
);
3641
},
3742
async createNullifier({ message }) {

packages/accounts/src/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
SignedFields,
55
SignedMessage,
66
SignedTransaction,
7+
TransactionOrZkAppCommandProperties,
78
} from "@mina-js/utils";
89
import type { HDKey } from "@scure/bip32";
910
import type { Simplify } from "type-fest";
@@ -12,7 +13,6 @@ import type {
1213
CreateNullifierParamsSchema,
1314
SignFieldsParamsSchema,
1415
SignMessageParamsSchema,
15-
SignTransactionParamsSchema,
1616
} from "./validation";
1717

1818
export enum MinaKeyConst {
@@ -81,7 +81,6 @@ export type { HDKey };
8181
export type SignFieldsParams = z.infer<typeof SignFieldsParamsSchema>;
8282
export type SignMessageParams = z.infer<typeof SignMessageParamsSchema>;
8383
export type CreateNullifierParams = z.infer<typeof CreateNullifierParamsSchema>;
84-
export type SignTransactionParams = z.infer<typeof SignTransactionParamsSchema>;
8584

8685
/**
8786
* Signer methods
@@ -92,5 +91,5 @@ export type CreateNullifier = (
9291
params: CreateNullifierParams,
9392
) => Promise<Nullifier>;
9493
export type SignTransaction = (
95-
params: SignTransactionParams,
94+
params: TransactionOrZkAppCommandProperties,
9695
) => Promise<SignedTransaction>;

packages/providers/src/validation.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ export const StorePrivateCredentialRequestParamsSchema =
102102
export const PresentationRequestParamsSchema = RequestWithContext.extend({
103103
method: z.literal("mina_requestPresentation"),
104104
params: z.array(
105-
z.object({
106-
presentationRequest: PresentationRequestSchema,
107-
zkAppAccount: zkAppAccountSchema.optional(),
108-
}).strict(),
105+
z
106+
.object({
107+
presentationRequest: PresentationRequestSchema,
108+
zkAppAccount: zkAppAccountSchema.optional(),
109+
})
110+
.strict(),
109111
),
110112
}).strict();
111113

packages/utils/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"cleanup": "rimraf dist .turbo"
1818
},
1919
"dependencies": {
20-
"mina-signer": "3.0.7"
20+
"mina-signer": "3.0.7",
21+
"serialize-error": "^11.0.3",
22+
"superjson": "2.2.1"
2123
},
2224
"devDependencies": {
2325
"zod": "3.23.8"

0 commit comments

Comments
 (0)