There is something I didn't understand regarding different Address formats. In short: I create a Master key, import it via xprv-Key into Electrum, generate P2SH and P2WSH-Adresses, send Coins to it but they don't appear.
I have created an extended key on the testnet with Bitwasp:
tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654
.I have imported this Key into Electrum running also on testnet.
- Clicking on "Receive" tells me the following address:
n28hWyXh8VPBCV6seKty19dW5ZNbqapB58
I've sent some testnet coins to this address, which became visible in Electrum very fast.
Using the BitWasp-Library I wrote the following Code (inspired by there own example-code:
$network = Bitcoin::setNetwork(NetworkFactory::bitcoinTestnet());
$random = new Random();
$hdFactory = new HierarchicalKeyFactory();
$master = $hdFactory->fromExtended("tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK654");
$addrReader = new AddressCreator();
$publicKey = $master->getPublicKey();
$helper = new P2pkhScriptDataFactory();
$scriptData = $helper->convertKey($publicKey);
$script = $scriptData->getScriptPubKey();
echo "key hash types\n";
$p2pkh = $scriptData->getAddress($addrReader);
echo " * p2pkh address: {$p2pkh->getAddress()}\n";
echo "\nscript hash types:\n";
$redeemScript = new P2shScript($p2pkh->getScriptPubKey());
$p2shAddr = $redeemScript->getAddress();
echo " * p2sh: {$p2shAddr->getAddress()}\n";
$p2wshScript = new WitnessScript($p2pkh->getScriptPubKey());
$p2wshAddr = $p2wshScript->getAddress();
echo " * p2wsh: {$p2wshAddr->getAddress()}\n";
$p2shP2wshScript = new P2shScript(new WitnessScript($p2pkh->getScriptPubKey()));
$p2shP2wshAddr = $p2shP2wshScript->getAddress();
echo " * p2sh|p2wsh: {$p2shP2wshAddr->getAddress()}\n";
It gives me the following output:
WIF: cUhtBXtsVhSikXiD46B5dKsCTjKqZGTWEADxSMNknP1V6VZfm1zR
key hash types
* p2pkh address: mjCptm7yxr3c7DpA6i17ude5ZkiHQMz7Rr
script hash types:
* p2sh: 2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G
* p2wsh: tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2
* p2sh|p2wsh: 2NFS1fXMuAJuJWaGAwMi7puE86n5XnbLEPo
Questions:
- Why is the Receive-Address in Electrum so different from those that I've got generated with the Bitwasp-Library. None of the generated addresses is similar to the address out of Electrum
- Sending Coins to the Address from Electrum will show up the received coins
- Sending Coins to P2shScript-Address for instance (
2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G
) will never appear in the Electrum-Wallet, even they are based out of the same private key I've tried also sending coins to the p2wsh-Address (tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2
), which also never gets any coins that was sent to.
Where is my mistake?
Update:
after the answer from @Abdussamad I tried the following: derivePath and send coins to that address then. I also imported the xprv of the derived Key into Electrum and send out some satoshis to its address. Nothing came in.
Additional code:
$purposePriv = $master->derivePath("{$purpose}'/0'/0'/0/0");
echo "m/{$purpose}'/0'/0/0/0': ".$purposePriv->toExtendedPrivateKey().PHP_EOL;
echo "M/{$purpose}'/0'/0/0/0': ".$purposePriv->toExtendedPublicKey().PHP_EOL;
$purposePriv = $master->derivePath("{$purpose}'/0'/0'");
$masterAddr = new PayToPubKeyHashAddress($purposePriv->getPublicKey()->getPubKeyHash());
echo " Address: " . $masterAddr->getAddress() . "\n\n";
Sending coins to the address will send them to elsewhere, not to the specific wallet.
Update 2:
I tried the following code, suggested here:
$mnemonic = "blahblahblah";
$seed = (new Bip39SeedGenerator())->getSeed($mnemonic);
$purpose = 44;
$factory = new HierarchicalKeyFactory();
$root = $factory->fromEntropy($seed);
$accountPriv = $root->derivePath("{$purpose}'/0'/0'"); // m/44'/0'/0' (change and regular addresses come from here)
echo "master public key: ".$accountPriv->toExtendedPublicKey().PHP_EOL;
$addressPriv = $accountPriv->derivePath("0/0"); // // m/44'/0'/0'/0/0 <- 0'th non-change address
$masterAddr = new PayToPubKeyHashAddress($accountPriv->getPublicKey()->getPubKeyHash());
echo " Address: " . $masterAddr->getAddress() . "\n\n";
I've got:
master public key: xpub6CXUv9gQCfQNNksArMAnKyu14vji7cK3V5wG7gYSoVntmtXTwzmhz4tFPzAiwagcdTYXXXjKCuFAQcUXM67k7cZL2sPbBEuZjp3GrwURdCo
Address: 15e5vNCSE9iBUbRW6kdHx84XFEcQgY4zfC
But after importing the xpub-key into Electrum, it shows the receive-address: 144aHwG4JKtpCjpJLKcB7aZq8nEidrBaKN