Recovery phrases
Generate, validate and derive Amadeus accounts from BIP39 recovery phrases with @amadeus-protocol/sdk, including the EIP-2333 derivation change in 1.4.0 and how to import phrases that may open a legacy account.
An Amadeus recovery phrase is a standard BIP39 phrase from the English wordlist. Since SDK 1.4.0 the phrase is turned into a key with EIP-2333 BLS12-381 derivation along the path m/12381'/36969'/0'/0'/0' (12381 is the BLS purpose, 36969 the Amadeus coin type). The same phrase opens the same account in the SDK, the hub, Prime Hub and cli-wallet.
Wallets built on SDK 1.3.0 or earlier used a different derivation, so one phrase can open two accounts. This page covers both, and the helpers that pick the right one on import. If you are a wallet user rather than a developer, read Recovery phrase derivations instead.
Generate and derive an account
generateMnemonic() returns a new 12-word phrase (128 bits of entropy). mnemonicToSeedBase58() turns a phrase into a Base58 64-byte seed, the same private-key format generateKeypair() produces, so it works anywhere the SDK takes a private key.
import {
TransactionBuilder,
derivePublicKeyFromSeedBase58,
generateMnemonic,
mnemonicToSeedBase58,
normalizeMnemonic,
validateMnemonic
} from '@amadeus-protocol/sdk'
const phrase = generateMnemonic() // 12 English BIP39 words
// What a user might paste: extra spaces, line breaks, capitals
const pasted = ` ${phrase.toUpperCase().split(' ').join('\n')} `
console.log(validateMnemonic(pasted)) // true
console.log(normalizeMnemonic(pasted) === phrase) // true
const seed = mnemonicToSeedBase58(phrase) // Base58 64-byte seed (EIP-2333 account)
const address = derivePublicKeyFromSeedBase58(seed)
const builder = new TransactionBuilder(seed)
console.log(address, builder instanceof TransactionBuilder)
validateMnemonic(phrase)checks the wordlist, word count and checksum. It returns a boolean and never throws.normalizeMnemonic(phrase)trims, lowercases and collapses whitespace. Store and compare phrases in this form. Since 1.6.0 the validate and derive functions apply it themselves.
Two derivations per phrase
| Derivation | How the seed is derived | Produced by |
|---|---|---|
eip2333 (standard) | EIP-2333 BLS12-381 along m/12381'/36969'/0'/0'/0'. The 32-byte secret key is stored little-endian in the low 32 bytes of the 64-byte seed | mnemonicToSeedBase58 in SDK 1.4.0 and later, the hub, Prime Hub, cli-wallet |
legacy | The raw 64-byte BIP39 seed, used directly as the Amadeus seed | mnemonicToSeedBase58 in SDK 1.3.0 and earlier, and wallets built on it |
mnemonicToSeedBase58 always returns the eip2333 seed. The HdDerivation type is 'eip2333' | 'legacy'.
Migrating from SDK 1.3.0 or earlier
mnemonicToSeedBase58 kept its name and signature in 1.4.0 but returns a different seed, and so a different address, for the same phrase. What to do depends on what your app stores:
- You store the seed (for example in an encrypted vault): nothing changes. A stored seed signs as the same account on any SDK version.
- You re-derive from the phrase on every start or on import: after upgrading, phrases created with 1.3.0 or earlier open a new, usually empty, address. Derive both candidates and open the one the user owns, with
resolveMnemonicImportwhen you do not know the address, orresolveCandidateForAddresswhen you do. - You create new wallets: keep using
mnemonicToSeedBase58. New phrases use the standard derivation.
There is no exported function that returns only the legacy seed. Use mnemonicToCandidateKeypairs and take the legacy entry.
Derive both candidates
mnemonicToCandidateKeypairs returns two MnemonicCandidate objects ({ derivation, seed, publicKey }), the standard one first.
import { mnemonicToCandidateKeypairs, validateMnemonic } from '@amadeus-protocol/sdk'
export function listCandidates(phrase: string): void {
// Candidates skip the checksum check: validate first
if (!validateMnemonic(phrase)) throw new Error('Invalid recovery phrase')
const [standard, legacy] = mnemonicToCandidateKeypairs(phrase)
console.log(standard.derivation, standard.publicKey) // 'eip2333' <address>
console.log(legacy.derivation, legacy.publicKey) // 'legacy' <address>
}
It does not check the BIP39 checksum or wordlist, so a phrase with a typo still derives two valid-looking but wrong accounts. It only throws when the word count is not 12, 15, 18, 21 or 24. Always call validateMnemonic first.
Match a known address
When you know which address the phrase should open, for example one the user copied from their wallet, resolveCandidateForAddress returns the matching candidate. It validates the phrase first and throws if it fails the wordlist or checksum check. It trims the address and compares it exactly (Base58 is case-sensitive). It returns null when neither derivation opens the address.
import { TransactionBuilder, resolveCandidateForAddress } from '@amadeus-protocol/sdk'
export function builderForAddress(phrase: string, walletAddress: string): TransactionBuilder {
// Throws if the phrase fails the BIP39 wordlist or checksum check
const candidate = resolveCandidateForAddress(phrase, walletAddress)
if (!candidate) throw new Error('This phrase does not open that address')
console.log(candidate.derivation) // 'eip2333' or 'legacy'
return new TransactionBuilder(candidate.seed)
}
Import a phrase into a wallet
When the user only types a phrase, resolveMnemonicImport derives both candidates, asks a probe whether each address has history, and opens one only when the answer is clear. addressHasActivity is the probe to use. resolveMnemonicImport does not validate the phrase, so validate first.
import { addressHasActivity, resolveMnemonicImport, validateMnemonic } from '@amadeus-protocol/sdk'
import type { CandidateWithActivity } from '@amadeus-protocol/sdk'
const apiBaseUrl = 'https://mainnet-rpc.ama.one/api'
export async function importPhrase(phrase: string): Promise<CandidateWithActivity | null> {
if (!validateMnemonic(phrase)) throw new Error('Invalid recovery phrase')
const result = await resolveMnemonicImport(phrase, (address) =>
addressHasActivity(apiBaseUrl, address)
)
for (const candidate of result.candidates) {
console.log(candidate.derivation, candidate.publicKey, candidate.activity)
}
// 'both-active' or 'check-failed': show result.candidates and let the user pick
if (result.needsChoice) return null
// 'single-active' or 'none-active': open it, and still show the other address
return result.chosen
}
The result is a ResolvedMnemonicImport: candidates (both, standard first, each with an activity), chosen, needsChoice and reason.
reason | When | chosen | needsChoice |
|---|---|---|---|
single-active | One candidate has history and the other was checked and is empty | That candidate | false |
none-active | Both were checked and are empty | The standard (eip2333) candidate | false |
both-active | Both have history | null | true |
check-failed | Any probe returned unknown or threw | null | true |
Show the user the other address too, so a wrong pick can be undone.
addressHasActivity(apiBaseUrl, address, timeoutMs?) returns 'active', 'inactive' or 'unknown' and never throws. It checks current balances (/wallet/balance_all, PRIME included) and transactions where the address is the receiver or the signer (/chain/tx_by_filter on arg0 and signer), so a funded-then-emptied account still counts as active. Any history gives 'active'. Otherwise a single failed request, error payload or timeout gives 'unknown', never 'inactive'. Each request times out after timeoutMs, by default ACTIVITY_PROBE_TIMEOUT_MS (8,000 ms).
Pass the API base URL with its path prefix, the same value you give new AmadeusSDK({ baseUrl }). For testnet use https://testnet-rpc.ama.one/api:
import { addressHasActivity } from '@amadeus-protocol/sdk'
const activity = await addressHasActivity(
'https://testnet-rpc.ama.one/api',
'5kizwGYhCDy8Dev7Qw31aVutXwk4sZCK18hVwWqUdLx8X6oG338XpoSR7X9c1fMg5o'
)
console.log(activity) // 'active'
Label a stored account
For accounts saved with both a seed and a phrase but no record of the derivation, derivationForSeed(phrase, seed) returns 'eip2333', 'legacy' or null. It compares the seed exactly and never throws: a malformed phrase returns null.
import { derivationForSeed } from '@amadeus-protocol/sdk'
interface StoredAccount {
seed: string
mnemonic?: string
}
export function derivationLabel(account: StoredAccount): string {
if (!account.mnemonic) return 'Imported key'
const derivation = derivationForSeed(account.mnemonic, account.seed)
if (derivation === 'legacy') return 'Legacy derivation: other wallets open a different address'
if (derivation === 'eip2333') return 'Standard derivation'
return 'Unknown derivation'
}
Import a raw BLS secret key
Some tools export the 32-byte BLS secret key itself (big-endian) instead of a phrase or a 64-byte seed, for example an EIP-2333 key exported from the hub. secretKeyToSeedBase58 wraps it into a 64-byte seed that signs as that key, so it fits the same storage as any other seed. It throws unless the key is exactly 32 bytes.
import { derivePublicKeyFromSeedBase58, fromBase58, secretKeyToSeedBase58 } from '@amadeus-protocol/sdk'
// exportedKey: Base58 32-byte big-endian BLS secret key
export function importRawKey(exportedKey: string): { seed: string; address: string } {
const seed = secretKeyToSeedBase58(fromBase58(exportedKey)) // throws unless exactly 32 bytes
return { seed, address: derivePublicKeyFromSeedBase58(seed) }
}
Tell a phrase from a key
detectInputType(input) returns 'mnemonic' when the trimmed input is 12 or 24 words made only of letters, and 'seed' for anything else. It is a format check only: confirm a phrase with validateMnemonic and a key with validateSeed.
import { detectInputType, generateKeypair, generateMnemonic } from '@amadeus-protocol/sdk'
console.log(detectInputType(generateMnemonic())) // 'mnemonic'
console.log(detectInputType(generateKeypair().privateKey)) // 'seed'
Store a phrase in an encrypted vault
encodeVaultSecret(seed, mnemonic?) returns a JSON string with the seed and, if given, the phrase. Encrypt it with encryptWithPassword. decodeVaultSecret reads it back as { seed, mnemonic? } and also accepts an older vault that holds only a bare Base58 seed.
import {
decodeVaultSecret,
decryptWithPassword,
encodeVaultSecret,
encryptWithPassword,
generateMnemonic,
mnemonicToSeedBase58
} from '@amadeus-protocol/sdk'
const phrase = generateMnemonic()
const seed = mnemonicToSeedBase58(phrase)
const vaultData = encodeVaultSecret(seed, phrase) // '{"seed":"...","mnemonic":"..."}'
const encrypted = await encryptWithPassword(vaultData, 'user-password')
const decrypted = await decryptWithPassword(encrypted, 'user-password')
const secret = decodeVaultSecret(decrypted) // also reads old vaults that hold a bare Base58 seed
console.log(secret.seed === seed, secret.mnemonic === phrase) // true true
Store the seed, not only the phrase. A stored seed keeps opening the same account whatever the derivation.
Reference
| Function | Signature | Since |
|---|---|---|
generateMnemonic | () => string (12 words) | |
validateMnemonic | (mnemonic: string) => boolean | |
normalizeMnemonic | (mnemonic: string) => string | 1.6.0 |
mnemonicToSeedBase58 | (mnemonic: string) => string. Standard derivation since 1.4.0, legacy before | |
mnemonicToCandidateKeypairs | (mnemonic: string) => MnemonicCandidate[] (standard first). Skips the checksum and wordlist check; throws only on a wrong word count | 1.5.0 |
resolveCandidateForAddress | (mnemonic: string, address: string) => MnemonicCandidate | null. Throws on an invalid phrase | 1.6.0 |
derivationForSeed | (mnemonic: string, seed: string) => HdDerivation | null. Never throws | 1.6.0 |
resolveMnemonicImport | (mnemonic: string, probeActivity: (address: string) => Promise<AddressActivity>) => Promise<ResolvedMnemonicImport>. Does not validate | 1.6.0 |
addressHasActivity | (apiBaseUrl: string, address: string, timeoutMs?: number) => Promise<AddressActivity>. Never throws | 1.6.0 |
secretKeyToSeedBase58 | (secretKey: Uint8Array) => string. Throws unless 32 bytes | 1.5.0 |
detectInputType | (input: string) => 'mnemonic' | 'seed' | |
encodeVaultSecret | (seed: string, mnemonic?: string) => string | |
decodeVaultSecret | (decrypted: string) => VaultSecretData ({ seed, mnemonic? }) |
Types: HdDerivation, MnemonicCandidate, AddressActivity ('active' | 'inactive' | 'unknown'), CandidateWithActivity, ImportResolutionReason, ResolvedMnemonicImport, VaultSecretData.
Next steps
- Utilities: keys, encoding, validation and password encryption
- Networks and testnet: use a derived account on testnet
- Transaction building: sign with the seed you derived