Skip to content

Commit

Permalink
feat(connect): add chain related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Nov 3, 2024
1 parent 363f34c commit c07429c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 204 deletions.
106 changes: 106 additions & 0 deletions apps/docs/src/pages/connect/wallet-interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ To make your zkApp compatible with Mina wallets, we've created a strongly typed

### mina_accounts

Get accounts. If not authorized, the response is an empty array.

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

Expand All @@ -14,8 +16,22 @@ const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_accounts'>({ method: 'mina_accounts' })
```

### mina_requestAccounts

Acts like `mina_accounts` but with a prompt to authorize in case user didn't authorize yet.

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

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

### mina_chainId

Get the chain ID. It's a string that represents the current network. Values are `mina:mainnet` or `mina:testnet`.

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

Expand All @@ -26,6 +42,8 @@ const { result } = await provider.request<'mina_chainId'>({ method: 'mina_chainI

### mina_getBalance

Get the balance of the current account. The value is already parsed to Mina units.

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

Expand All @@ -36,6 +54,8 @@ const { result } = await provider.request<'mina_getBalance'>({ method: 'mina_get

### mina_chainInformation

Get chain information. Similar to `mina_chainId`, but more detailed. It returns current network's RPC url, name, and slug (chain ID).

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

Expand All @@ -44,6 +64,21 @@ const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_chainInformation'>({ method: 'mina_chainInformation' })
```

### mina_getState

Returns filtered Public Credentials.

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_getState'>({
method: 'mina_getState',
params: [{ issuer: "University of Example" }, []],
})
```

## Commands

### mina_sign
Expand Down Expand Up @@ -128,3 +163,74 @@ const { result } = await provider.request<'mina_createNullifier'>({
params: [['1', '2', '3']]
})
```

### mina_sendTransaction

Send a signed transaction to the network.

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_sendTransaction'>({
method: 'mina_sendTransaction',
params: [{ input: {}, signature: { field: 'xyz', scalar: 'xyz' } }, 'payment']
})
```

### mina_setState

Saves a new Public Credential.

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

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_setState'>({
method: "mina_setState",
params: [
{
objectName: "Pallad Mock Credential",
object: {}, // DID Credential with a Kimchi proof
},
],
})
```

### mina_switchChain

Prompts user to switch to another network. It's useful for dApps that support multiple networks.

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

const store = createStore()
const { provider } = store.getProviders()[0]
await provider.request<'mina_switchChain'>({
method: 'mina_switchChain',
params: ['mina:mainnet']
})
```

### mina_addChain

Prompts user to add a new chain.

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

const store = createStore()
const { provider } = store.getProviders()[0]
await provider.request<'mina_addChain'>({
method: 'mina_addChain',
params: [
{
name: 'Mina Fakenet',
slug: 'mina:fakenet',
url: 'https://fakenet.example.com',
}
]
})
```
9 changes: 6 additions & 3 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {
SendTransactionBodySchema,
SendZkAppBodySchema,
type Sendable,
} from "@mina-js/utils";
import { gql } from "@urql/core";
import { match } from "ts-pattern";
import { SendTransactionBodySchema, SendZkAppBodySchema } from "../schema";
import { getNodeClient } from "../utils/node";

export const PRIORITY = {
Expand Down Expand Up @@ -83,8 +87,7 @@ const sendTransaction = async ({
signedTransaction,
type,
}: {
// biome-ignore lint/suspicious/noExplicitAny: TODO
signedTransaction: any;
signedTransaction: Sendable;
type: "payment" | "delegation" | "zkapp";
}) => {
const client = getNodeClient();
Expand Down
14 changes: 1 addition & 13 deletions apps/klesia/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PublicKeySchema, TransactionBodySchema } from "@mina-js/utils";
import { PublicKeySchema } from "@mina-js/utils";
import { z } from "zod";
import { SendZkappInput } from "./zkapp";

export const KlesiaNetwork = z.enum(["devnet", "mainnet", "zeko_devnet"]);
export const PublicKeyParamsSchema = z.array(PublicKeySchema).length(1);
Expand All @@ -11,17 +10,6 @@ export const SignatureSchema = z.union([
}),
z.object({ field: z.string(), scalar: z.string() }),
]);
export const SendTransactionBodySchema = z.object({
input: TransactionBodySchema,
signature: SignatureSchema,
});
export const SendZkAppBodySchema = z.object({
input: SendZkappInput,
});
export const SendableSchema = z.union([
SendTransactionBodySchema,
SendZkAppBodySchema,
]);
export const SendTransactionSchema = z.tuple([
z.any(),
z.enum(["payment", "delegation", "zkapp"]),
Expand Down
184 changes: 0 additions & 184 deletions apps/klesia/src/zkapp.ts

This file was deleted.

Loading

0 comments on commit c07429c

Please sign in to comment.