Skip to content
Cive

Wallet Client

Import

import { ,  } from "cive";

Local Accounts (Private Key, Mnemonic, etc)

A local account signs transactions and messages with a private key before executing a method via JSON-RPC.

There are three types of Local Accounts in cive:

The following steps outline how to integrate a Private Key Account, and the same steps can be applied to Mnemonic and HD Accounts as well.

1. Initialize a Wallet Client

First we create the Wallet Client with the http Transport.

import { ,  } from 'cive'
import {  } from 'cive/chains'
import {  } from "cive";
const  = ({
  : ,
  : (),
});

2. Create a Private Key Account

Next, we create a Private Key Account using privateKeyToAccount. To create an account, you need to provide the private key and the Network ID (1029 for mainnet, 1 for testnet).

import { ,  } from 'cive'
import {  } from 'cive/chains'
import {  } from "cive";
import {  } from "cive/accounts";
const  = ({
  : ,
  : (),
});
const  = ("0x...", {
  : ., 
}); 

3. Consume Wallet Actions

New you can use the Account whtin Wallet Actions.

import { , ,  } from "cive";
import {  } from "cive/accounts";
import {  } from "cive/chains";
const  = ({
  : ,
  : (),
});
const  = ("0x...", {
  : .,
});
const  = await .({
  ,
  : "cfxtest:...",
  : ("0.001"),
});

Optional: Hoist the Account

If you do not wish to pass the account for every action, you can also elevate the account to the wallet client.

import { , ,  } from "cive";
import {  } from "cive/accounts";
import {  } from "cive/chains";
const  = ("0x...", {
  : .,
});
const  = ({
  , 
  : ,
  : (),
});
 
const  = await .({
  , 
  : "cfxtest:....",
  : ("0.001"),
});

Optional: Extend with Public Actions

When you using a local account, you may find yourself using a publick client instantiated with the same parameters as you wallet client.

In this case ,you can extend your wallet client with Public Actions to avoid having to handle multiple clients.

import { , , ,  } from "cive";
import {  } from "cive/accounts";
import {  } from "cive/chains";
const  = ("0x...", {
  : .,
});
const  = ({
  ,
  : ,
  : (),
}).(); // [!code ++]
 
const  = await .();

Parameters

account (optional)

  • Type: Account

The Account to use for the Wallet Client. This will be used for Actions that require an account as an argument.

Accepts a Local Account (Private Key, etc).

import { , ,  } from "cive";
import {  } from "cive/accounts";
import {  } from "cive/chains";
const  = ("0x...", {
  : ., 
}); 
const  = ({
  , // [!code ++]
  : ,
  : (),
});

chain (optional)

  • Type: Chain

The Chain of the Wallet Client.

const client = createWalletClient({
  chain: mainnet, 
  transport: http(),
});

cacheTime (optional)

  • Type: number
  • Default: 4000
const client = createWalletClient({
  cacheTime: 4000, 
  chain: mainnet,
  transport: http(),
});

key(optional)

  • Type string
  • Default public

The key of the Public Client.

const publicClient = createWalletClient({
  key: "wallet", 
  chain: mainnet,
  transport: http(),
});

name(optional)

  • Type string
  • Default Public Client

The name of the Public Client.

const publicClient = createWalletClient({
  name: "wallet Client", 
  chain: mainnet,
  transport: http(),
});

pollingInterval(optional)

  • Type number
  • Default 4000
const publicClient = createWalletClient({
  pollingInterval: 4000, 
  chain: mainnet,
  transport: http(),
});