Skip to content

Commit

Permalink
feat(wallets): rewrite mina_signTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Oct 24, 2024
1 parent 00e2b7a commit b586ecd
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 25 deletions.
29 changes: 26 additions & 3 deletions apps/docs/src/pages/connect/wallet-interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const { result } = await provider.request<'mina_sign'>({ method: 'mina_sign', pa

### mina_signTransaction

Sign payment or delegation:

```ts twoslash
import { createStore } from '@mina-js/connect'

Expand All @@ -67,11 +69,32 @@ const { result } = await provider.request<'mina_signTransaction'>({
method: 'mina_signTransaction',
params: [{
// You should probably get the right nonce from Klesia for that.
nonce: 1n,
nonce: '1',
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
amount: 3000000000n,
fee: 100000000n,
amount: '3000000000',
fee: '100000000',
}]
})
```

Sign zkApp command:

```ts twoslash
import { createStore } from '@mina-js/connect'

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_signTransaction'>({
method: 'mina_signTransaction',
params: [{
zkappCommand: {},
feePayer: {
feePayer: 'B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5',
fee: '100000000',
nonce: '1',
memo: 'Hello, Mina!',
}
}]
})
```
Expand Down
Binary file modified bun.lockb
Binary file not shown.
14 changes: 9 additions & 5 deletions packages/accounts/src/accounts/private-key-to-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ it("signs a transaction", async () => {
privateKey: Test.accounts[0].privateKey,
});
const transaction = {
nonce: 1n,
nonce: "1",
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
amount: 3000000000n,
fee: 100000000n,
amount: "3000000000",
fee: "100000000",
};
const signedTransaction = await account.signTransaction({ transaction });
expect(signedTransaction).toMatchSnapshot();
Expand All @@ -38,7 +38,9 @@ it("creates a nullifier", async () => {
privateKey: Test.accounts[0].privateKey,
});
const message = [1n, 2n, 3n];
const nullifier = await account.createNullifier({ message });
const nullifier = await account.createNullifier({
message: message.map((el) => el.toString()),
});
expect(typeof nullifier.private.c).toBe("bigint");
});

Expand All @@ -47,6 +49,8 @@ it("signs fields", async () => {
privateKey: Test.accounts[0].privateKey,
});
const fields = [1n, 2n, 3n];
const signedFields = await account.signFields({ fields });
const signedFields = await account.signFields({
fields: fields.map((el) => el.toString()),
});
expect(signedFields).toMatchSnapshot();
});
14 changes: 12 additions & 2 deletions packages/accounts/src/accounts/private-key-to-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ export function privateKeyToAccount({
);
},
async createNullifier({ message }) {
return NullifierSchema.parse(client.createNullifier(message, privateKey));
return NullifierSchema.parse(
client.createNullifier(
message.map((el) => BigInt(el)),
privateKey,
),
);
},
async signFields({ fields }) {
return SignedFieldsSchema.parse(client.signFields(fields, privateKey));
return SignedFieldsSchema.parse(
client.signFields(
fields.map((el) => BigInt(el)),
privateKey,
),
);
},
});

Expand Down
12 changes: 6 additions & 6 deletions packages/connect/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const createWalletClient = ({
method: "mina_getBalance",
params: [account.publicKey],
});
return BigInt(result);
return result;
};
return match(providerSource)
.with("klesia", async () => {
Expand All @@ -79,7 +79,7 @@ export const createWalletClient = ({
method: "mina_getTransactionCount",
params: [account.publicKey],
});
return BigInt(result);
return result;
};
const getChainId = async () => {
return match(providerSource)
Expand Down Expand Up @@ -122,9 +122,9 @@ export const createWalletClient = ({
method: "mina_estimateFees",
});
return {
low: BigInt(result.low),
medium: BigInt(result.medium),
high: BigInt(result.high),
low: result.low,
medium: result.medium,
high: result.high,
};
};
const prepareTransactionRequest = async (
Expand All @@ -136,7 +136,7 @@ export const createWalletClient = ({
nonce = await getTransactionCount();
}
if (!fee) {
fee = BigInt((await estimateFees()).medium);
fee = (await estimateFees()).medium;
}
return {
...transaction,
Expand Down
5 changes: 3 additions & 2 deletions packages/providers/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ZkAppCommandPayload } from "@mina-js/utils";
import {
FieldSchema,
NullifierSchema,
Expand Down Expand Up @@ -47,13 +48,13 @@ export const SignFieldsRequestParamsSchema = z
export const SignTransactionRequestParamsSchema = z
.object({
method: z.literal("mina_signTransaction"),
params: z.array(TransactionPayload),
params: z.array(z.union([TransactionPayload, ZkAppCommandPayload])),
})
.strict();
export const SendTransactionRequestParamsSchema = z
.object({
method: z.literal("mina_sendTransaction"),
params: z.array(TransactionPayload),
params: z.array(SignedTransactionSchema),
})
.strict();
export const CreateNullifierRequestParamsSchema = z
Expand Down
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cleanup": "rimraf dist .turbo"
},
"dependencies": {
"mina-signer": "3.0.7",
"zod": "3.23.8"
},
"peerDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
SignedTransactionSchema,
TransactionPayload,
TransactionReceiptSchema,
ZkAppCommandPayload,
} from "./validation";

/**
Expand All @@ -23,6 +24,7 @@ export type TransactionProperties = z.infer<typeof TransactionPayload>;
export type PartiallyFormedTransactionProperties = z.infer<
typeof PartiallyFormedTransactionPayload
>;
export type ZkAppCommandProperties = z.infer<typeof ZkAppCommandPayload>;

/**
* Return types
Expand Down
31 changes: 24 additions & 7 deletions packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const JsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([LiteralSchema, z.array(JsonSchema), z.record(JsonSchema)]),
);

export const FieldSchema = z.coerce.bigint();
export const FieldSchema = z.coerce.string();

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

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

export const FeePayerSchema = z
.object({
feePayer: PublicKeySchema,
fee: z.coerce.string(),
nonce: z.coerce.string(),
memo: z.string().optional(),
validUntil: z.coerce.string().optional(),
})
.strict();

export const DelegationPayload = z
.object({
from: PublicKeySchema,
to: PublicKeySchema,
memo: z.string().optional(),
fee: z.coerce.bigint(),
nonce: z.coerce.bigint(),
validUntil: z.coerce.bigint().optional(),
fee: z.coerce.string(),
nonce: z.coerce.string(),
validUntil: z.coerce.string().optional(),
})
.strict();

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

export const TransactionPayload = DelegationPayload.extend({
amount: z.coerce.bigint(),
amount: z.coerce.string(),
}).strict();

export const TransportableTransactionPayload =
Expand All @@ -59,10 +69,17 @@ export const TransportableTransactionPayload =
}).strict();

export const PartiallyFormedTransactionPayload = TransactionPayload.extend({
fee: z.coerce.bigint().optional(),
nonce: z.coerce.bigint().optional(),
fee: z.coerce.string().optional(),
nonce: z.coerce.string().optional(),
});

export const ZkAppCommandPayload = z
.object({
zkappCommand: JsonSchema,
feePayer: FeePayerSchema,
})
.strict();

/**
* Return type schemas
*/
Expand Down

0 comments on commit b586ecd

Please sign in to comment.