Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accounts): add zkapp command signing #7

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ exports[`signs fields 1`] = `
"signature": "7mXCUvhLhFvG9ptrdfNceCrpThkCUyg1ct2z8uwY7eQbKz7UNmhv33TbuDjTznaypJtXRiMJyQWDnf27TH1FSXG7uJHTKAd9",
}
`;

exports[`signs a zkapp command 1`] = `
{
"data": {
"feePayer": {
"fee": "100000000",
"feePayer": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
"memo": "Test",
"nonce": "0",
"validUntil": "null",
},
"zkappCommand": {
"accountUpdates": [],
"feePayer": {
"authorization": "7mXWqNfmqMTM5uSCS2xLfsRBLTjGZKTtpEakdsrdQz1EUgYXogSvKxxtfGbBkqQ2mZRMA3uPAM8riaCF56pkqpZBLr2kNBLa",
"body": {
"fee": "100000000",
"nonce": "0",
"publicKey": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
"validUntil": null,
},
},
"memo": "E4YVT4x3A9rUhmjkjGn8ZYBLZn7zK4cfvnMtBYZFdWkg37n2s3nrP",
},
},
"publicKey": "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
"signature": "7mXWqNfmqMTM5uSCS2xLfsRBLTjGZKTtpEakdsrdQz1EUgYXogSvKxxtfGbBkqQ2mZRMA3uPAM8riaCF56pkqpZBLr2kNBLa",
}
`;
29 changes: 29 additions & 0 deletions packages/accounts/src/accounts/private-key-to-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ it("signs a transaction", async () => {
expect(signedTransaction).toMatchSnapshot();
});

it("signs a zkapp command", async () => {
const account = privateKeyToAccount({
privateKey: Test.accounts[0].privateKey,
});
const command = {
zkappCommand: {
accountUpdates: [],
memo: "E4YM2vTHhWEg66xpj52JErHUBU4pZ1yageL4TVDDpTTSsv8mK6YaH",
feePayer: {
body: {
publicKey: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
fee: "100000000",
validUntil: "100000",
nonce: "1",
},
authorization: "",
},
},
feePayer: {
feePayer: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
fee: "100000000",
nonce: "0",
memo: "Test",
},
};
const signedTransaction = await account.signTransaction({ command });
expect(signedTransaction).toMatchSnapshot();
});

it("creates a nullifier", async () => {
const account = privateKeyToAccount({
privateKey: Test.accounts[0].privateKey,
Expand Down
9 changes: 7 additions & 2 deletions packages/accounts/src/accounts/private-key-to-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export function privateKeyToAccount({
async signMessage({ message }) {
return SignedMessageSchema.parse(client.signMessage(message, privateKey));
},
async signTransaction({ transaction }) {
async signTransaction(signable) {
if ("transaction" in signable) {
return SignedTransactionSchema.parse(
client.signTransaction(signable.transaction, privateKey),
);
}
return SignedTransactionSchema.parse(
client.signTransaction(transaction, privateKey),
client.signTransaction(signable.command as never, privateKey),
);
},
async createNullifier({ message }) {
Expand Down
5 changes: 2 additions & 3 deletions packages/accounts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
SignedFields,
SignedMessage,
SignedTransaction,
TransactionOrZkAppCommandProperties,
} from "@mina-js/utils";
import type { HDKey } from "@scure/bip32";
import type { Simplify } from "type-fest";
Expand All @@ -12,7 +13,6 @@ import type {
CreateNullifierParamsSchema,
SignFieldsParamsSchema,
SignMessageParamsSchema,
SignTransactionParamsSchema,
} from "./validation";

export enum MinaKeyConst {
Expand Down Expand Up @@ -81,7 +81,6 @@ export type { HDKey };
export type SignFieldsParams = z.infer<typeof SignFieldsParamsSchema>;
export type SignMessageParams = z.infer<typeof SignMessageParamsSchema>;
export type CreateNullifierParams = z.infer<typeof CreateNullifierParamsSchema>;
export type SignTransactionParams = z.infer<typeof SignTransactionParamsSchema>;

/**
* Signer methods
Expand All @@ -92,5 +91,5 @@ export type CreateNullifier = (
params: CreateNullifierParams,
) => Promise<Nullifier>;
export type SignTransaction = (
params: SignTransactionParams,
params: TransactionOrZkAppCommandProperties,
) => Promise<SignedTransaction>;
4 changes: 4 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
SignedTransactionSchema,
StoredCredentialSchema,
TransactionBodySchema,
TransactionOrZkAppCommandSchema,
TransactionPayloadSchema,
TransactionReceiptSchema,
ZkAppCommandBodySchema,
Expand All @@ -32,6 +33,9 @@ export type TransactionPayload = z.infer<typeof TransactionPayloadSchema>;
export type PartialTransaction = z.infer<typeof PartialTransactionSchema>;
export type ZkAppCommandBody = z.infer<typeof ZkAppCommandBodySchema>;
export type ZkAppCommandProperties = z.infer<typeof ZkAppCommandPayload>;
export type TransactionOrZkAppCommandProperties = z.infer<
typeof TransactionOrZkAppCommandSchema
>;
export type Sendable = z.infer<typeof SendableSchema>;

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export const ZkAppCommandPayload = z
})
.strict();

export const TransactionOrZkAppCommandSchema = z.union([
TransactionPayloadSchema,
ZkAppCommandPayload,
]);

/**
* Return type schemas
*/
Expand Down Expand Up @@ -122,7 +127,7 @@ export const NullifierSchema = z

export const SignedTransactionSchema = z
.object({
signature: SignatureSchema,
signature: z.union([SignatureSchema, z.string()]),
publicKey: PublicKeySchema,
data: z.union([TransactionBodySchema, ZkAppCommandBodySchema]),
})
Expand Down