Auto-detect import format
createWalletFromString() automatically detects the format of a string and creates the appropriate wallet type. This is useful when you have user input and don't know in advance whether it's a mnemonic phrase, private key, xpub, or other format.
Usage
import { createWalletFromString } from 'chaingate'
// Mnemonic phrase → PhraseWallet
const wallet1 = createWalletFromString('abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about')
// Hex private key (64 chars) → PrivateKeyWallet
const wallet2 = createWalletFromString('abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890')
// WIF private key → PrivateKeyWallet
const wallet3 = createWalletFromString('L4mEiExaMplEPriv4Tek3y...')
// Hex seed (128 chars) → SeedWallet
const wallet4 = createWalletFromString('abcdef1234567890...' /* 128 hex chars */)
// xpriv → XprivWallet
const wallet5 = createWalletFromString('xprv9s21ZrQH143K...')
// xpub → XpubWallet (view-only)
const wallet6 = createWalletFromString('xpub6CUGRUo...')
// Compressed public key (66 hex chars) → PublicKeyWallet (view-only)
const wallet7 = createWalletFromString('02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc')
Detection rules
| Input format | Detected as | Wallet type |
|---|---|---|
| BIP39 mnemonic (12-24 words) | phrase | PhraseWallet |
| 64 hex characters | privateKey | PrivateKeyWallet |
| 128 hex characters | seed | SeedWallet |
| 66 or 130 hex characters | publicKey | PublicKeyWallet |
| Base58 (51-52 chars) | wif | PrivateKeyWallet |
Starts with xprv | xpriv | XprivWallet |
Starts with xpub | xpub | XpubWallet |
Detecting format without creating
Use detectWalletImportType() to identify the format without creating a wallet:
import { detectWalletImportType } from 'chaingate'
const type = detectWalletImportType('abandon abandon abandon ...')
console.log(type) // 'phrase'
const type2 = detectWalletImportType('xpub6CUGRUo...')
console.log(type2) // 'xpub'
Returns one of: 'phrase', 'xpriv', 'xpub', 'wif', 'seed', 'privateKey', 'publicKey'.
Throws UnrecognizedFormatError if the format cannot be identified.
Key points
- Synchronous: No API key or async call needed.
- All formats: Supports phrases, seeds, hex keys, WIF, xpriv, xpub, and public keys.
- Error on unknown: Throws
UnrecognizedFormatErrorif the input doesn't match any recognized format. - Useful for UIs: Let users paste any wallet credential and auto-detect what it is.