Skip to main content

Import from seed

A seed is a low-level representation of your wallet backup. Unlike mnemonic phrases (BIP39), seeds can be either a Uint8Array or a hex string. Importing from a seed is a straightforward way to restore or clone your existing wallet.

Keep your seed safe!

Your seed grants full access to all assets in your wallet. If someone obtains it, they can spend and manage your funds without restriction.

Import from a valid seed

The operation is synchronous. Below are examples using both a hex string and a Uint8Array:

import { importWallet } from 'chaingate'

// Using a hex string (128 hex chars = 64 bytes)
const wallet = importWallet({ seed: 'abcdef1234567890...' })

// Or using a Uint8Array
const seedBytes = new Uint8Array([0xab, 0xcd, 0xef, /* ... */])
const walletFromBytes = importWallet({ seed: seedBytes })

Using the wallet

Connect the wallet to a network to derive addresses and send transactions:

import { importWallet, ChainGate } from 'chaingate'

const wallet = importWallet({ seed: 'abcdef1234567890...' })
const cg = new ChainGate({ apiKey: 'your-api-key' })

const bitcoin = cg.connect(cg.networks.bitcoin, wallet)
console.log(await bitcoin.address())

Check seed validity

Before importing, validate the seed using isValidSeed():

import { isValidSeed, importWallet } from 'chaingate'

const seed = 'abcdef1234567890...'

if (!isValidSeed(seed)) {
throw new Error('Invalid seed')
}

const wallet = importWallet({ seed })
note

isValidSeed() returns true only if the seed is a properly formatted hex string (128 hex characters) or a valid Uint8Array.

Access the seed

If you need to retrieve the seed from an already initialized wallet:

const seedEntity = await wallet.getSeed()
console.log(seedEntity.hex) // hex string
caution

Never log your seed in a production environment!

Exporting a wallet

To back up or persist your wallet, you need to serialize it.

Learn how to serialize your wallet