Skip to content

Wallet Client

One API to rull them all.

If you missed a Wallet Client from Viem, you can use MinaJS's Wallet Client to interact with Mina wallets. It's a one API that wraps over various wallet types and connectivities.

Client properties

  • network: Network type. devnet or mainnet.
  • account: Account object compatible with toAccount format. string or Account object. If not provided, the client will use the last injected wallet.
  • providerSource: Provider source. klesia (JSON-RPC account), window.mina (last injected wallet), string (slug of specific browser wallet). Default is window.mina.

Initialize client

import { createWalletClient } from '@mina-js/connect'
 
const client = createWalletClient({ network: "devnet" });

Querying the data

From injected wallet

Query from the last injected wallet (using window.mina):

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const balance = await client.getBalance();

Query from a specific browser wallet (using slug of a specific injected wallet):

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({
  account,
  network: "devnet",
  providerSource: 'pallad'
});
 
const balance = await client.getBalance();

From Klesia JSON-RPC

Query data from our Klesia JSON-RPC:

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({
  account,
  network: "devnet",
  providerSource: 'klesia'
});
 
const balance = await client.getBalance();

Wallet queries

Get balance

Query current wallet's balance.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const balance = await client.getBalance();

Get accounts

Query wallet addresses of an injected wallet.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const balance = await client.getAccounts();

Get transaction count

Query the number of transactions of a wallet (used as nonce).

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const transactionCount = await client.getTransactionCount();

Get network ID

Query the network ID of the network.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect'
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const networkId = await client.getNetworkId();

Wallet commands

If you provide a local wallet, Wallet Client will sign with it, otherwise, it will use the injected wallet.

Sign a message

Sign a message with either the injected wallet or a local wallet.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect';
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ network: "devnet" });
 
const signature = await client.signMessage({ message: 'Hello World' })

Sign a transaction

Sign a transaction with either the injected wallet or a local wallet.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect';
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const signature = await client.signTransaction({
  transaction: {
  	nonce: "1",
  	from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
  	to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
  	amount: "3000000000",
  	fee: "100000000",
  }
})

Sign fields

Sign fields of a transaction with either the injected wallet or a local wallet.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect';
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const signature = await client.signFields({
  fields: ["1", "2", "3"]
})

Create a nullifier

Create a nullifier with either the injected wallet or a local wallet.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect';
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const nullifier = await client.createNullifier({
  message: ["1", "2", "3"]
});

Prepare a transaction request

There is a helper method to prepare a transaction request that lacks either nonce or fee. This will fetch the correct nonce for you and estimate a medium fee that ensures the transaction is included in the next block.

import { toAccount } from "@mina-js/accounts";
import { createWalletClient } from '@mina-js/connect';
 
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });
 
const transactionRequest = await client.prepareTransactionRequest({
  from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
  to: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
  amount: "3000000000",
})