3

I tested a code example for generating a mnemonic phrase and restoring a wallet using a mnemonic phrase from the official Solan documentation and a question arose.

const mnemonic =
    "pill tomorrow foster begin walnut borrow virtual kick shift mutual shoe scatter";
  const seed = bip39.mnemonicToSeedSync(mnemonic, ""); // (mnemonic, password)
  const keypair = Keypair.fromSeed(seed.slice(0, 32));
  console.log(`${keypair.publicKey.toBase58()}`); 
// 5ZWj7a1f8tWkjBESHKgrLmXshuXxqeY9SYcfbshpAqPG

but if i import this secret phrase in phantom app, the wallet address will be different (AWjbG5SH5VEay5ksZbGHHgJhYRhM1rsN5Z538cfFvs4a). How can I make sure that when importing into phantom the address is the same as when creating the wallet?

1 Answer 1

3

In case someone encounters the same problem, I will leave an answer to my question. The problem is that phantom find first key derivation along the path m/44'/501'/0'/0'. To get a wallet with the same public key as in phantom you need to use the following code from official documentation:

import { Keypair } from "@solana/web3.js";
import { HDKey } from "micro-ed25519-hdkey";
import * as bip39 from "bip39";

(async () => {
  const mnemonic =
    "neither lonely flavor argue grass remind eye tag avocado spot unusual intact";
  const seed = bip39.mnemonicToSeedSync(mnemonic, ""); // (mnemonic, password)
  const hd = HDKey.fromMasterSeed(seed.toString("hex"));
  for (let i = 0; i < 10; i++) {
    const path = `m/44'/501'/${i}'/0'`;
    const keypair = Keypair.fromSeed(hd.derive(path).privateKey);
    console.log(`${path} => ${keypair.publicKey.toBase58()}`);
  }
})();

P.S.: someone wrote that if you top up your account, it will be higher in the list of keys along the derivation paths and the phantom will find it. But I haven't checked it.

UPD:

From the official phantom app docs:

How does Phantom import wallet addresses?

When importing addresses from an existing seed phrase, Phantom will scan for 20 addresses in each of our three supported derivation paths (bip44change, bip44, and a deprecated path), for a total of 60 addresses. For the convenience of the user, Phantom will filter this list of addresses down to wallets that have ever had signatures (i.e. have ever been used). Phantom will then sort this filtered list based on how many signatures each wallet has had plus the amount of lamports it currently owns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.