Wallet Client
Import
import { http, createWalletClient } 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 { createWalletClient, custom } from 'cive'
import { mainnet } from 'cive/chains'
import { http } from "cive";
const client = createWalletClient({
chain: mainnet,
transport: http(),
});
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 { createWalletClient, custom } from 'cive'
import { mainnet } from 'cive/chains'
import { http } from "cive";
import { privateKeyToAccount } from "cive/accounts";
const client = createWalletClient({
chain: mainnet,
transport: http(),
});
const account = privateKeyToAccount("0x...", {
networkId: mainnet.id,
});
3. Consume Wallet Actions
New you can use the Account whtin Wallet Actions.
import { http, createWalletClient, parseCFX } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
const client = createWalletClient({
chain: mainnet,
transport: http(),
});
const account = privateKeyToAccount("0x...", {
networkId: mainnet.id,
});
const hash = await client.sendTransaction({
account,
to: "cfxtest:...",
value: parseCFX("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 { http, createWalletClient, parseCFX } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
const account = privateKeyToAccount("0x...", {
networkId: mainnet.id,
});
const client = createWalletClient({
account,
chain: mainnet,
transport: http(),
});
const hash = await client.sendTransaction({
account,
to: "cfxtest:....",
value: parseCFX("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 { http, createWalletClient, parseCFX, publicActions } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
const account = privateKeyToAccount("0x...", {
networkId: mainnet.id,
});
const client = createWalletClient({
account,
chain: mainnet,
transport: http(),
}).extend(publicActions);
const block = await client.getBlock();
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 { http, createWalletClient, parseCFX } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
const account = privateKeyToAccount("0x...", {
networkId: mainnet.id,
});
const client = createWalletClient({
account,
chain: mainnet,
transport: http(),
});
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(),
});