0

I am having trouble creating SegWit address from mnemonic. I can't find any useful code to do that. I tried using BitcoinLib but I don't know how to disable saving wallets to disk.

EDIT: I found solution that I was looking for. It now shows correct wallet on TrustWallet and on my PC. I used cryptotools libary. Here is the code:

from cryptotools.BTC import Xprv
m = Xprv.from_mnemonic('... mnemonic ...')
print((m/84./0./0./0/0).address('P2WPKH'))
4
  • check out github.com/mcdallas/cryptotools
    – Mike D
    Commented Mar 5, 2023 at 13:01
  • thanks, will check it out and let you know if it worked.
    – cloudzik
    Commented Mar 5, 2023 at 14:58
  • Fixed, thank you so much
    – cloudzik
    Commented Mar 5, 2023 at 15:57
  • @cloudzik: If you answered your own question can you post an answer rather than answering it in the question? Thanks Commented Mar 14, 2023 at 13:06

1 Answer 1

-1
import cryptotools

# Set up the mnemonic and derivation path
mnemonic = "your mnemonic goes here"
path = "m/0'"

# Derive the private key and public key from the mnemonic and path
private_key = cryptotools.bip32.master_key(mnemonic.encode())
derived_private_key = private_key / path
public_key = derived_private_key.public_key()

# Generate the SegWit address from the public key
segwit_address = cryptotools.bech32.encode("bc", 0, cryptotools.hash160(public_key.to_bytes()))

print(segwit_address)

Note that this code assumes that you have already installed the cryptotools library using pip. Also, make sure to replace "your mnemonic goes here" with your actual mnemonic phrase, and "m/0'" with the actual derivation path you want to use.

As for disabling wallet saving in BitcoinLib, you can set the BitcoinSecret.Saving property to false to prevent the wallet from being saved to disk. Here is an example code snippet:

BitcoinSecret secret = new BitcoinSecret("your private key goes here");
secret.Saving = false;

Again, replace "your private key goes here" with your actual private key. This code assumes that you have already installed and imported the NBitcoin library, which BitcoinLib is based on.

1
  • 2
    Stop using ChatGPT, ill report this.
    – cloudzik
    Commented Mar 5, 2023 at 20:39

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.