spesmilo / electrum

Electrum Bitcoin Wallet
https://electrum.org
MIT License
7.46k stars 3.1k forks source link

How can I achieve spending on the addresses generated from the master public key of an electrum wallet? #7103

Closed Hammad-Mubeen closed 3 years ago

Hammad-Mubeen commented 3 years ago

My Problem:

I have code for generating addresses from the master public key of an electrum wallet, it generates addresses but not their private keys, I need to check faucet spending on those addresses for that I need the addresses private key?...because when I import a wallet by these addresses electrum says watch-only wallet, (i need to import wallet from private keys which I don't have ) if I am wrong how can I achieve spending on the addresses generated from the master public key of an electrum wallet.

My code:

var express = require("express");
var router = express.Router();
const bjs = require("bitcoinjs-lib");
const network = bjs.networks.testnet;
const bip32 = require("bip32");

//master public key of electrum for mainnet
const xpub =
  "xpub661MyMwAqRbcGwzeg1yfxcrFFqE8HdZEv5hA2kLodenvqTfg2UgPE8Y6UADxgWr5qEgGd6SSQ8GXwzqnEKSC1yfra8x2KDZon6DitiouFdf";

//master public key of electrum for testnet
const tpub =
  "tpubD6NzVbkrYhZ4XErHL7zyG4hGKtQY9UvdNzkRPFSvL7Wtrx97yeMwcs4uNVZa1yEha54ZfN8Q6AxWyEpsQaEshi8ukgfCR8LYzX7daxW2cyU";

router.post("/generateaddressestestnet", async function (req, res, next) {
  try {
    if (!req.body.index) {
      return res.status(404).json({
        success: false,
        message: "Index not found for generating unique address.",
      });
    }

    const p2wpkh = bjs.payments.p2wpkh({
      pubkey: bjs.bip32
        .fromBase58(tpub, network)
        .derive(0)
        .derive(req.body.index).publicKey,
      network,
    });
    const payment = bjs.payments.p2sh({ redeem: p2wpkh, network });

    const addressString = payment.address;
    console.log(`the addressString is ${addressString}`);
    return res.status(200).json({
      success: true,
      message: "Address generated Successfully! ",
      address: addressString,
    });
  } catch (error) {
    console.log("error (try-catch) : " + error);
    return res.status(500).json({ success: false, err: error });
  }
});

router.post("/generateaddressesmainnet", async function (req, res, next) {
  try {
    if (!req.body.index) {
      return res.status(404).json({
        success: false,
        message: "Index not found for generating unique address.",
      });
    }
    const { address } = bjs.payments.p2pkh({
      pubkey: bip32.fromBase58(xpub).derive(0).derive(req.body.index).publicKey,
    });

    console.log(`The addressString is ${address}`);
    return res.status(200).json({
      success: true,
      message: "Address generated Successfully! ",
      address: address,
    });
  } catch (error) {
    console.log("error (try-catch) : " + error);
    return res.status(500).json({ success: false, err: error });
  }
});

module.exports = router;
SomberNight commented 3 years ago

I have code for generating addresses from the master public key of an electrum wallet, it generates addresses but not their private keys electrum says watch-only wallet

A master public key is called as such because it can be publically shared without loss of security. It only allows watching addresses, not spending from them.

how can I achieve spending on the addresses generated from the master public key

You need the corresponding private keys.

I need to check faucet spending on those addresses for that I need the addresses private key?

By "check spending" do you mean watching the addresses? In that case a watch-only wallet should suffice.

Hammad-Mubeen commented 3 years ago

A master public key is called as such because it can be publically shared without loss of security. It only allows watching addresses, not spending from them.

So, I cannot spend them

You need the corresponding private keys.

Can I somehow have those private keys for the addresses generated from master public key? or it can not be achieved as you mentioned above.

By "check spending" do you mean watching the addresses? In that case a watch-only wallet should suffice.

No, by faucet spending i mean, I need to receive faucets on those addresses (which I am successful to do so) in a watch only wallet but can not spend them because it a watch only wallet. For spending i need private keys of those addresses, can I somehow have the private keys of the addresses generated from the master public key? or it can not be achieved as you mentioned above.

Hammad-Mubeen commented 3 years ago

"I have received funds on an address generated by xpub key generated using electrum wallet" how do i spend those funds"

SomberNight commented 3 years ago

How did you create the master public key (xpub)? If you generated it, presumably at one point you did have the corresponding private keys.

Hammad-Mubeen commented 3 years ago

no electrum generated it by itself, i have just retrieved it from the wallet to use it.

SomberNight commented 3 years ago

How did you create the wallet?

Hammad-Mubeen commented 3 years ago

i have created a standard electrum wallet, it gives me the master public key ....using that i am generating addresses from node backend then sends faucat to that address ...have import address into a wallet (watch only wallet) to see faucets have come or not(they are coming)...now i want to spend those faucats (want to transfer it to someone). I know i need private key to spend but how i can have the private key?

SomberNight commented 3 years ago

i have created a standard electrum wallet, it gives me the master public key

No it does not. It first gives you seed words. You are supposed to write those down on paper. Only after that can you obtain the master public key.

You should restore from seed words.