Skip to main content

Deserialize

Deserialization allows you to restore a previously saved wallet from a single serialized string.
Every wallet type—seed-based, private key–based, or phrase-based—can be serialized and later reconstructed via initializeWallet.deserialize().

info

Serialization is the recommended way to back up wallets.
Check the Serialization guide to learn how to generate a serialized string.

Initialize from a valid serialization​

import { initializeWallet } from 'chaingate'

async function askForPasswordFn(attempts: number, reject: () => void) {
// Display a secure password prompt (like a login popup)
const userInput = prompt(`Type in the password (Attempt ${attempts}/3):`)
return userInput
}

const wallet = await initializeWallet.deserialize({
serialized: 'your-serialized-wallet-string',
askForPassword: askForPasswordFn
})

Once deserialized, the wallet is fully operational. For example:

const bitcoin = wallet.currency('bitcoin')
console.log(await bitcoin.getAddress())
Encrypted wallet

For security reasons, serialized wallets are always encrypted, so you must provide an askForPassword function. For more information, refer to the Wallet in-memory encryption guide.

Casting the wallet​

Depending on how the wallet was originally created, specific functions or getters (e.g., getPhrase(), getSeed(), or getPrivateKey()) require casting the wallet to the correct type.

Checking the wallet type​

import { castWallet } from 'chaingate'

if (castWallet.isPhraseWallet(wallet)) {
// The wallet is phrase-based
const phrase = await wallet.getPhrase()
console.log(await phrase.getPhrase())
} else if (castWallet.isSeedWallet(wallet)) {
// The wallet is seed-based
const seed = await wallet.getSeed()
console.log(seed.hex)
} else if (castWallet.isPrivateKeyWallet(wallet)) {
// The wallet is private key–based
const privateKey = await wallet.getPrivateKey()
console.log(privateKey.hex)
}

Forcing a cast​

Alternatively, if you know the wallet type, you can forcibly cast it:

import { castWallet } from 'chaingate'

// If the deserialized wallet is phrase-based
castWallet.requirePhraseWallet()

// Then you can safely retrieve the phrase
const phrase = await wallet.getPhrase()
console.log(await phrase.getPhrase())

// If the deserialized wallet is seed-based
castWallet.requireSeedWallet()
const seed = await wallet.getSeed()
console.log(seed.hex)

// If the deserialized wallet is private key–based
castWallet.requirePrivateKeyWallet()
const privateKey = await wallet.getPrivateKey()
console.log(privateKey.hex)
Secure your secrets

Remember that phrases, seeds, and private keys are extremely sensitive.
If anyone gains access to them, they have full control over your wallet.

Key points​

  • Universal format: All wallet types can be serialized into a single string for easy storage or transfer.
  • Deserialization: Restores the wallet exactly as it was before serialization, including all accounts/addresses if applicable.
  • Casting required: To access specialized functions, you must cast (or check) the wallet to its underlying type.
  • Security: Handle the serialized data with care. Although encrypted, if compromised, it could enable an attacker to reconstruct your wallet.