Skip to content

Commit b586ecd

Browse files
committed
feat(wallets): rewrite mina_signTransaction
1 parent 00e2b7a commit b586ecd

File tree

9 files changed

+83
-25
lines changed

9 files changed

+83
-25
lines changed

apps/docs/src/pages/connect/wallet-interface.mdx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const { result } = await provider.request<'mina_sign'>({ method: 'mina_sign', pa
5858

5959
### mina_signTransaction
6060

61+
Sign payment or delegation:
62+
6163
```ts twoslash
6264
import { createStore } from '@mina-js/connect'
6365

@@ -67,11 +69,32 @@ const { result } = await provider.request<'mina_signTransaction'>({
6769
method: 'mina_signTransaction',
6870
params: [{
6971
// You should probably get the right nonce from Klesia for that.
70-
nonce: 1n,
72+
nonce: '1',
7173
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
7274
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
73-
amount: 3000000000n,
74-
fee: 100000000n,
75+
amount: '3000000000',
76+
fee: '100000000',
77+
}]
78+
})
79+
```
80+
81+
Sign zkApp command:
82+
83+
```ts twoslash
84+
import { createStore } from '@mina-js/connect'
85+
86+
const store = createStore()
87+
const { provider } = store.getProviders()[0]
88+
const { result } = await provider.request<'mina_signTransaction'>({
89+
method: 'mina_signTransaction',
90+
params: [{
91+
zkappCommand: {},
92+
feePayer: {
93+
feePayer: 'B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5',
94+
fee: '100000000',
95+
nonce: '1',
96+
memo: 'Hello, Mina!',
97+
}
7598
}]
7699
})
77100
```

bun.lockb

24 Bytes
Binary file not shown.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ it("signs a transaction", async () => {
2323
privateKey: Test.accounts[0].privateKey,
2424
});
2525
const transaction = {
26-
nonce: 1n,
26+
nonce: "1",
2727
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
2828
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
29-
amount: 3000000000n,
30-
fee: 100000000n,
29+
amount: "3000000000",
30+
fee: "100000000",
3131
};
3232
const signedTransaction = await account.signTransaction({ transaction });
3333
expect(signedTransaction).toMatchSnapshot();
@@ -38,7 +38,9 @@ it("creates a nullifier", async () => {
3838
privateKey: Test.accounts[0].privateKey,
3939
});
4040
const message = [1n, 2n, 3n];
41-
const nullifier = await account.createNullifier({ message });
41+
const nullifier = await account.createNullifier({
42+
message: message.map((el) => el.toString()),
43+
});
4244
expect(typeof nullifier.private.c).toBe("bigint");
4345
});
4446

@@ -47,6 +49,8 @@ it("signs fields", async () => {
4749
privateKey: Test.accounts[0].privateKey,
4850
});
4951
const fields = [1n, 2n, 3n];
50-
const signedFields = await account.signFields({ fields });
52+
const signedFields = await account.signFields({
53+
fields: fields.map((el) => el.toString()),
54+
});
5155
expect(signedFields).toMatchSnapshot();
5256
});

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ export function privateKeyToAccount({
3535
);
3636
},
3737
async createNullifier({ message }) {
38-
return NullifierSchema.parse(client.createNullifier(message, privateKey));
38+
return NullifierSchema.parse(
39+
client.createNullifier(
40+
message.map((el) => BigInt(el)),
41+
privateKey,
42+
),
43+
);
3944
},
4045
async signFields({ fields }) {
41-
return SignedFieldsSchema.parse(client.signFields(fields, privateKey));
46+
return SignedFieldsSchema.parse(
47+
client.signFields(
48+
fields.map((el) => BigInt(el)),
49+
privateKey,
50+
),
51+
);
4252
},
4353
});
4454

packages/connect/src/client.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const createWalletClient = ({
6363
method: "mina_getBalance",
6464
params: [account.publicKey],
6565
});
66-
return BigInt(result);
66+
return result;
6767
};
6868
return match(providerSource)
6969
.with("klesia", async () => {
@@ -79,7 +79,7 @@ export const createWalletClient = ({
7979
method: "mina_getTransactionCount",
8080
params: [account.publicKey],
8181
});
82-
return BigInt(result);
82+
return result;
8383
};
8484
const getChainId = async () => {
8585
return match(providerSource)
@@ -122,9 +122,9 @@ export const createWalletClient = ({
122122
method: "mina_estimateFees",
123123
});
124124
return {
125-
low: BigInt(result.low),
126-
medium: BigInt(result.medium),
127-
high: BigInt(result.high),
125+
low: result.low,
126+
medium: result.medium,
127+
high: result.high,
128128
};
129129
};
130130
const prepareTransactionRequest = async (
@@ -136,7 +136,7 @@ export const createWalletClient = ({
136136
nonce = await getTransactionCount();
137137
}
138138
if (!fee) {
139-
fee = BigInt((await estimateFees()).medium);
139+
fee = (await estimateFees()).medium;
140140
}
141141
return {
142142
...transaction,

packages/providers/src/validation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ZkAppCommandPayload } from "@mina-js/utils";
12
import {
23
FieldSchema,
34
NullifierSchema,
@@ -47,13 +48,13 @@ export const SignFieldsRequestParamsSchema = z
4748
export const SignTransactionRequestParamsSchema = z
4849
.object({
4950
method: z.literal("mina_signTransaction"),
50-
params: z.array(TransactionPayload),
51+
params: z.array(z.union([TransactionPayload, ZkAppCommandPayload])),
5152
})
5253
.strict();
5354
export const SendTransactionRequestParamsSchema = z
5455
.object({
5556
method: z.literal("mina_sendTransaction"),
56-
params: z.array(TransactionPayload),
57+
params: z.array(SignedTransactionSchema),
5758
})
5859
.strict();
5960
export const CreateNullifierRequestParamsSchema = z

packages/utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"cleanup": "rimraf dist .turbo"
1919
},
2020
"dependencies": {
21+
"mina-signer": "3.0.7",
2122
"zod": "3.23.8"
2223
},
2324
"peerDependencies": {

packages/utils/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
SignedTransactionSchema,
1111
TransactionPayload,
1212
TransactionReceiptSchema,
13+
ZkAppCommandPayload,
1314
} from "./validation";
1415

1516
/**
@@ -23,6 +24,7 @@ export type TransactionProperties = z.infer<typeof TransactionPayload>;
2324
export type PartiallyFormedTransactionProperties = z.infer<
2425
typeof PartiallyFormedTransactionPayload
2526
>;
27+
export type ZkAppCommandProperties = z.infer<typeof ZkAppCommandPayload>;
2628

2729
/**
2830
* Return types

packages/utils/src/validation.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const JsonSchema: z.ZodType<Json> = z.lazy(() =>
1414
z.union([LiteralSchema, z.array(JsonSchema), z.record(JsonSchema)]),
1515
);
1616

17-
export const FieldSchema = z.coerce.bigint();
17+
export const FieldSchema = z.coerce.string();
1818

1919
export const GroupSchema = z
2020
.object({
@@ -27,14 +27,24 @@ export const PublicKeySchema = z.string().length(55).startsWith("B62");
2727

2828
export const PrivateKeySchema = z.string().length(52);
2929

30+
export const FeePayerSchema = z
31+
.object({
32+
feePayer: PublicKeySchema,
33+
fee: z.coerce.string(),
34+
nonce: z.coerce.string(),
35+
memo: z.string().optional(),
36+
validUntil: z.coerce.string().optional(),
37+
})
38+
.strict();
39+
3040
export const DelegationPayload = z
3141
.object({
3242
from: PublicKeySchema,
3343
to: PublicKeySchema,
3444
memo: z.string().optional(),
35-
fee: z.coerce.bigint(),
36-
nonce: z.coerce.bigint(),
37-
validUntil: z.coerce.bigint().optional(),
45+
fee: z.coerce.string(),
46+
nonce: z.coerce.string(),
47+
validUntil: z.coerce.string().optional(),
3848
})
3949
.strict();
4050

@@ -50,7 +60,7 @@ export const TransportableDelegationPayload = z
5060
.strict();
5161

5262
export const TransactionPayload = DelegationPayload.extend({
53-
amount: z.coerce.bigint(),
63+
amount: z.coerce.string(),
5464
}).strict();
5565

5666
export const TransportableTransactionPayload =
@@ -59,10 +69,17 @@ export const TransportableTransactionPayload =
5969
}).strict();
6070

6171
export const PartiallyFormedTransactionPayload = TransactionPayload.extend({
62-
fee: z.coerce.bigint().optional(),
63-
nonce: z.coerce.bigint().optional(),
72+
fee: z.coerce.string().optional(),
73+
nonce: z.coerce.string().optional(),
6474
});
6575

76+
export const ZkAppCommandPayload = z
77+
.object({
78+
zkappCommand: JsonSchema,
79+
feePayer: FeePayerSchema,
80+
})
81+
.strict();
82+
6683
/**
6784
* Return type schemas
6885
*/

0 commit comments

Comments
 (0)