Import from private key
A private key is the fundamental credential for your wallet. It can be provided as either a Uint8Array or a string (in hex or WIF format). Like seeds or mnemonic phrases, your private key must be kept secure to protect your assets.
Anyone with access to your private key can fully control the wallet and all associated funds.
Unlike wallets initialized from seeds or mnemonic phrases, private key wallets do not support derivation paths. Each private key directly corresponds to a single address, which means you cannot derive additional accounts or addresses from it.
Import from a valid private key
The operation is synchronous. Below are examples using hex, WIF, and a Uint8Array:
import { importWallet } from 'chaingate'
// Using a hex string
const wallet = importWallet({ privateKey: 'abcdef1234567890...' })
// Using a WIF (Wallet Import Format) string
const wifWallet = importWallet({ privateKey: 'L4mEiExaMplEPriv4Tek3y...' })
// Using a Uint8Array
const keyBytes = new Uint8Array([0xab, 0xcd, 0xef, /* ... */])
const walletFromBytes = importWallet({ privateKey: keyBytes })
Using the wallet
Connect the wallet to a network to derive addresses and send transactions:
import { importWallet, ChainGate } from 'chaingate'
const wallet = importWallet({ privateKey: 'abcdef1234567890...' })
const cg = new ChainGate()
const bitcoin = cg.connect(cg.networks.bitcoin, wallet)
console.log(await bitcoin.address())
Check private key validity
Before importing, validate the private key using isValidPrivateKey():
import { isValidPrivateKey, importWallet } from 'chaingate'
const privateKey = 'abcdef1234567890...'
if (!isValidPrivateKey(privateKey)) {
throw new Error('Invalid private key')
}
const wallet = importWallet({ privateKey })
isValidPrivateKey() returns true only if the private key is a valid Uint8Array, hex string, or WIF format.
Access the private key
To retrieve the private key from an already initialized wallet:
const pk = await wallet.getPrivateKey()
console.log(pk.hex) // hex string
Do not store or log this private key unless you fully understand the security implications. If someone gains access to it, they control all associated funds.
Exporting a wallet
To back up or persist your wallet, you need to serialize it.