Skip to content

Commit

Permalink
feat(connect): add wallet interface docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 16, 2024
1 parent 4eeda33 commit ce28b1d
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 112 deletions.
3 changes: 2 additions & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"dev": "bunx vocs dev",
"build": "bunx vocs build",
"preview": "bunx vocs preview"
"preview": "bunx vocs preview",
"cleanup": "rimraf dist .turbo node_modules"
},
"dependencies": {
"@theguild/remark-mermaid": "^0.1.2",
Expand Down
103 changes: 103 additions & 0 deletions apps/docs/src/pages/connect/wallet-interface.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Wallet Interface [Make your zkApp compatible with Mina wallets.]

To make your zkApp compatible with Mina wallets, we've created a strongly typed interface for `window.mina` and injected providers found by [Provider Discovery](/connect/provider-discovery).

## Queries

### mina_accounts

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_accounts'>({ method: 'mina_accounts' })
```

### mina_chainId

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_chainId'>({ method: 'mina_chainId' })
```

### mina_getBalance

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_getBalance'>({ method: 'mina_getBalance' })
```

### mina_chainInformation

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_chainInformation'>({ method: 'mina_chainInformation' })
```

## Commands

### mina_sign

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_sign'>({ method: 'mina_sign', params: ['My message'] })
```

### mina_signTransaction

```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: [{
// You should probably get the right nonce from Klesia for that.
nonce: 1n,
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
amount: 3000000000n,
fee: 100000000n,
}]
})
```

### mina_signFields

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_signFields'>({
method: 'mina_signFields',
params: [[1n, 2n, 3n]]
})
```

### mina_createNullifier

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_createNullifier'>({
method: 'mina_createNullifier',
params: [[1n, 2n, 3n]]
})
```
1 change: 1 addition & 0 deletions apps/docs/vocs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default defineConfig({
{ text: "Introduction", link: "/connect" },
{ text: "Getting Started", link: "/connect/getting-started" },
{ text: "Provider Discovery", link: "/connect/provider-discovery" },
{ text: "Wallet Interface", link: "/connect/wallet-interface" },
],
},
{
Expand Down
3 changes: 2 additions & 1 deletion apps/klesia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"build": "tsup-node",
"dev": "tsup-node --watch --onSuccess \"node dist/server.js\"",
"start": "node dist/server.js",
"test": "bun test"
"test": "bun test",
"cleanup": "rimraf dist .turbo node_modules"
},
"dependencies": {
"@hono/node-server": "^1.12.2",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"build": "turbo build",
"test": "turbo test",
"cleanup": "bun run --filter '*' cleanup",
"cleanup": "bun run --filter '*' cleanup && rimraf node_modules .turbo",
"lint": "bunx biome check .",
"format": "bunx biome check . --write --unsafe"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"@scure/base": "^1.1.8",
"@scure/bip32": "1.4.0",
"@scure/bip39": "1.3.0",
"mina-signer": "3.0.7"
"mina-signer": "3.0.7",
"zod": "^3.23.8"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
35 changes: 31 additions & 4 deletions packages/accounts/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import type {
CreateNullifier,
Nullifier,
PublicKey,
SignFields,
SignMessage,
SignTransaction,
SignedFields,
SignedMessage,
SignedTransaction,
} from "@mina-js/shared";
import type { HDKey } from "@scure/bip32";
import type { Simplify } from "type-fest";
import type { z } from "zod";
import type {
CreateNullifierParamsSchema,
SignFieldsParamsSchema,
SignMessageParamsSchema,
SignTransactionParamsSchema,
} from "./validation";

export enum MinaKeyConst {
PURPOSE = 44,
Expand Down Expand Up @@ -63,3 +70,23 @@ export type HDAccount = Simplify<
export type PrivateKeyAccount = Simplify<LocalAccount<"privateKey">>;

export type { HDKey };

/**
* Parameter types
*/
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
*/
export type SignFields = (params: SignFieldsParams) => Promise<SignedFields>;
export type SignMessage = (params: SignMessageParams) => Promise<SignedMessage>;
export type CreateNullifier = (
params: CreateNullifierParams,
) => Promise<Nullifier>;
export type SignTransaction = (
params: SignTransactionParams,
) => Promise<SignedTransaction>;
26 changes: 26 additions & 0 deletions packages/accounts/src/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FieldSchema, TransactionPayload } from "@mina-js/shared";
import { z } from "zod";

export const SignFieldsParamsSchema = z
.object({
fields: z.array(FieldSchema),
})
.strict();

export const SignMessageParamsSchema = z
.object({
message: z.string(),
})
.strict();

export const CreateNullifierParamsSchema = z
.object({
message: z.array(FieldSchema),
})
.strict();

export const SignTransactionParamsSchema = z
.object({
transaction: TransactionPayload,
})
.strict();
13 changes: 8 additions & 5 deletions packages/providers/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { z } from "zod";
import type {
MinaProviderDetailSchema,
MinaProviderInfoSchema,
ProviderListenerSchema,
ProviderRequestParamsSchema,
ProviderRequestParamsUnion,
ProviderRpcErrorSchema,
ResultType,
} from "./validation";

export type MinaProviderDetail = z.infer<typeof MinaProviderDetailSchema>;
export type MinaProviderDetail = {
info: MinaProviderInfo;
provider: MinaProviderClient;
};

export type MinaProviderInfo = z.infer<typeof MinaProviderInfoSchema>;

Expand All @@ -32,11 +34,12 @@ export type ProviderRpcEvent =

export type ProviderListener = z.infer<typeof ProviderListenerSchema>;

export type ProviderRequestParams = z.infer<typeof ProviderRequestParamsUnion>;

export type MinaProviderRequest = <M extends string>(
args: Extract<M, z.infer<typeof ProviderRequestParamsSchema>>,
args: Extract<ProviderRequestParams, { method: M }>,
) => Promise<ResultType<M>>;

// export type MinaProviderClient = z.infer<typeof MinaProviderClientSchema>;
export type MinaProviderClient = {
request: MinaProviderRequest;
on: ProviderListener;
Expand Down
49 changes: 13 additions & 36 deletions packages/providers/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {
CreateNullifierParamsSchema,
FieldSchema,
NullifierSchema,
PublicKeySchema,
SendTransactionParamsSchema,
SignFieldsParamsSchema,
SignMessageParamsSchema,
SignTransactionParamsSchema,
SignedFieldsSchema,
SignedMessageSchema,
SignedTransactionSchema,
TransactionPayload,
TransactionReceiptSchema,
} from "@mina-js/shared";
import { z } from "zod";
Expand Down Expand Up @@ -38,41 +35,41 @@ export const GetBalanceRequestParamsSchema = z
export const SignRequestParamsSchema = z
.object({
method: z.literal("mina_sign"),
params: SignMessageParamsSchema,
params: z.array(z.string()),
})
.strict();
export const SignFieldsRequestParamsSchema = z
.object({
method: z.literal("mina_signFields"),
params: SignFieldsParamsSchema,
params: z.array(z.array(FieldSchema)),
})
.strict();
export const SignTransactionRequestParamsSchema = z
.object({
method: z.literal("mina_signTransaction"),
params: SignTransactionParamsSchema,
params: z.array(TransactionPayload),
})
.strict();
export const SendTransactionRequestParamsSchema = z
.object({
method: z.literal("mina_sendTransaction"),
params: SendTransactionParamsSchema,
params: z.array(TransactionPayload),
})
.strict();
export const CreateNullifierRequestParamsSchema = z
.object({
method: z.literal("mina_createNullifier"),
params: CreateNullifierParamsSchema,
params: z.array(z.array(FieldSchema)),
})
.strict();
export const SwitchChainRequestParamsSchema = z.object({
method: z.literal("mina_switchChain"),
params: SwitchChainRequestParams,
params: z.array(z.string()),
});
export const AddChainRequestParamsSchema = z
.object({
method: z.literal("mina_addChain"),
params: AddChainRequestParams,
params: z.array(AddChainRequestParams),
})
.strict();

Expand Down Expand Up @@ -144,7 +141,7 @@ export const AddChainRequestReturnSchema = z
})
.strict();

export const RpcReturnTypes = z.discriminatedUnion("method", [
export const RpcReturnTypesUnion = z.discriminatedUnion("method", [
AccountsRequestReturnSchema,
ChainIdRequestReturnSchema,
ChainInformationRequestReturnSchema,
Expand All @@ -158,7 +155,7 @@ export const RpcReturnTypes = z.discriminatedUnion("method", [
AddChainRequestReturnSchema,
]);

export const ProviderRequestParamsSchema = z.discriminatedUnion("method", [
export const ProviderRequestParamsUnion = z.discriminatedUnion("method", [
AccountsRequestParamsSchema,
ChainIdRequestParamsSchema,
ChainInformationRequestParamsSchema,
Expand All @@ -171,18 +168,11 @@ export const ProviderRequestParamsSchema = z.discriminatedUnion("method", [
SwitchChainRequestParamsSchema,
AddChainRequestParamsSchema,
]);
export const ResultSchema = z.object({
jsonrpc: z.literal("2.0"),
result: z.promise(RpcReturnTypes),
});
export type RpcReturnTypesUnionType = z.infer<typeof RpcReturnTypesUnion>;
export type ResultType<M extends string> = {
jsonrpc: "2.0";
result: Extract<M, z.infer<typeof RpcReturnTypes>>;
result: Extract<RpcReturnTypesUnionType, { method: M }>["result"];
};
export const ProviderRequestSchema = z
.function()
.args(ProviderRequestParamsSchema)
.returns(z.promise(ResultSchema));

export const ChainIdCallbackSchema = z
.function()
Expand Down Expand Up @@ -258,16 +248,3 @@ export const MinaProviderInfoSchema = z.object({
rdns: z.string(),
slug: z.string(),
});

export const MinaProviderClientSchema = z.object({
request: ProviderRequestSchema,
on: ProviderListenerSchema,
removeListener: ProviderListenerSchema,
});

export const MinaProviderDetailSchema = z
.object({
info: MinaProviderInfoSchema,
provider: MinaProviderClientSchema,
})
.strict();
1 change: 1 addition & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"import": "./dist/index.js"
}
},
"files": ["dist"],
"scripts": {
"build": "tsup",
"test": "bun test",
Expand Down
Loading

0 comments on commit ce28b1d

Please sign in to comment.