Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Sanka555 on April 19, 2021, 05:17:12 AM



Title: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 05:17:12 AM
I am programming in Java (bitcoij library)

i have original line String="someline";

after transformations and encryptions i get correct

19SP2KEonvrYQAgk1yZ8ir9dDLq3SwXRP3 +  key1
1APDCaHFRpRi8tzHgKnzfaW3rqsxJiqDjY     + key2

its ok.

and i do
String segvit = SegwitAddress.fromKey(MainNetParams.get(), key).toBech32();
i get correct

bc1qvmk80pd2uet0zdgf4sqjlumup7f68225esv2v2    

how do i get key3?


Title: Re: how to get the missing key?
Post by: NotATether on April 19, 2021, 06:54:18 AM
I'm assuming you want to get the bech32 address from the public key hash?

Then you just import the org.bitcoinj.core.SegwitAddress (https://bitcoinj.org/javadoc/0.15/org/bitcoinj/core/SegwitAddress.html) class and call its toBech32() method like this:

Code:
SegwitAddress segwitAddress = SegwitAddress.fromHash(MainNetParams.get(), hash160); 
segwitAddress.toBech32();


Title: Re: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 07:17:32 AM
Yes. that's what I do.
Quote
key = ECKey.fromPrivate(bigInteger, true);
String segvit = SegwitAddress.fromKey(MainNetParams.get(), key).toBech32();

And now I need a key to this address.

The key, that I can enter into bitcoin core, and Core will give to me bc1qvmk80pd2uet0zdgf4sqjlumup7f68225esv2v2

if i use "key" from my code i get only
19SP2KEonvrYQAgk1yZ8ir9dDLq3SwXRP3   
or
1APDCaHFRpRi8tzHgKnzfaW3rqsxJiqDjY     


Title: Re: how to get the missing key?
Post by: NotATether on April 19, 2021, 08:01:13 AM
Yes. that's what I do.
Quote
key = ECKey.fromPrivate(bigInteger, true);
String segvit = SegwitAddress.fromKey(MainNetParams.get(), key).toBech32();

And now I need a key to this address.

The key, that I can enter into bitcoin core, and Core will give to me bc1qvmk80pd2uet0zdgf4sqjlumup7f68225esv2v2

I'm having trouble following, do you want to:

- Get the private key to this bech32 address, or
- From the Bitcoin Core password, use it to find and decrypt this address in the wallet file?


Title: Re: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 08:06:06 AM
 Get the private key to this bech32 address


Title: Re: how to get the missing key?
Post by: NotATether on April 19, 2021, 08:33:59 AM
Get the private key to this bech32 address

You can't get the private key using only an address. However, if you also have the wallet.dat file, you can use a Berkeley DB library such as Oracle's (https://www.oracle.com/database/berkeley-db/java-edition.html) (free and OSS) to read all of the ckey (https://github.com/bitcoin/bitcoin/blob/83c715415a8886e3e46ef375e042f13d8165dabd/src/wallet/walletdb.cpp#L35) (encrypted private key) or key (https://github.com/bitcoin/bitcoin/blob/83c715415a8886e3e46ef375e042f13d8165dabd/src/wallet/walletdb.cpp#L42) (unencrypted) fields inside the database.

Since the "ckey" fields are encrypted and need a complex process to decrypt them, let's assume you are reading an unencrypted wallet.dat, so "key" fields only.

I'm guessing based on this post (https://bitcointalk.org/index.php?topic=5257844.msg54694553#msg54694553) and https://github.com/bitcoin/bitcoin/blob/master/src/wallet/walletdb.cpp#L102-L115 that the "key" field's key and record is

Code:
<length of id string > | key | <length> | <public key>

and

Code:
<length of private key> | <private key> | <sha256 of private key>

So you'd read each pair of these, and then run each public key through your BitcoinJ code above to get Bech32 addresses, and see which one matches "bc1qvmk80pd2uet0zdgf4sqjlumup7f68225esv2v2", and take its associated private key.


Title: Re: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 08:46:14 AM
sorry, probably my fault
English..  :-\ i am not a native speaker)

i have string "blabla..."
I use Java, myself, make a pair of address + key from a string.
i make 2 pair. "compress" and "decompress"
and i want make third pair - toBech32

but I can only get the address. I can not get the key to this address


the key from which I am making the address (compressed)
doesn't give me Bech32  if i enter this key into Core . He gives me "compressed" adress


I do not need to extract and decrypt anything from wallet.dat
I am trying to create an address myself


Title: Re: how to get the missing key?
Post by: NotATether on April 19, 2021, 09:04:08 AM
i have string "blabla..."
I use Java, myself, make a pair of address + key from a string.

Ok, so you're using the string as random bytes to create the private key (basically a form of brainwallet).

So in this case you should make the key first, using this string. And then the address. I have written about this in another post:

You start with getting the private key. The double SHA256 hash of the password will give you that, and you can derive the public key from it as normal...

In Java it translates to this:

Code:
import java.security.MessageDigest;

/* ... */

String text = "blabla..."; // this is your input

// First round of SHA256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(text.getBytes(StandardCharsets.UTF_8));
// Second round of SHA256
byte[] privatekey = digest.digest(hash);

// Get the public key HASH160 from the private key
ECKey key = ECKey.fromPrivate(privatekey);
byte[] publickeyhash = key.getPubKeyHash();

// Encode HASH160 as Bech32 address
SegwitAddress segwitAddress = SegwitAddress.fromHash(MainNetParams.get(), publickeyhash);
segwitAddress.toBech32();


Title: Re: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 10:25:37 AM
i do it for  "soslsoeoeo"

and i get
bc1q7gmyvmrg2fwvyap46kztuqmg0ukvyanxcqctxl    and key = L4iHGYsgeMW5HyGXhpuzvFjESsrSAaoReb6QqreZkfVuZjSUJ4AJ

but if i give this key  Electrum or https://www.blockchain.com/  wallet   i dont get  bc1q7gmyvmrg2fwvyap46kztuqmg0ukvyanxcqctxl

i get 1KqcH1oRstqekPXbwhtikewr9XRXD4tXMs

how can i get  bc1q7gmyvmrg2fwvyap46kztuqmg0ukvyanxcqctxl in wallet??



what am I doing wrong? Thanks in advance for your patience :)

Quote
MessageDigest digest;
         try {
            digest = MessageDigest.getInstance("SHA-256");
      
         byte[] hash = digest.digest(str.getBytes(StandardCharsets.UTF_8));
         byte[] privatekey = digest.digest(hash);
            key = ECKey.fromPrivate(privatekey);
         byte[] publickeyhash = key.getPubKeyHash();
         SegwitAddress segwitAddress = SegwitAddress.fromHash(MainNetParams.get(), publickeyhash);
         segwitAddress.toBech32();
         System.out.println(segwitAddress.toBech32() +" "+ key.getPrivateKeyAsWiF(MainNetParams.get()));



Title: Re: how to get the missing key?
Post by: Sanka555 on April 19, 2021, 11:07:39 AM
This is my small mistake.

Thank you very much everyone. Through joint efforts, this issue was successfully resolved.
Thank you so much!