Bitcoin Forum
November 02, 2024, 02:04:38 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Get adress from WIF private Key  (Read 133 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
November 28, 2021, 08:32:45 PM
Merited by ABCbits (1)
 #1

Java. bitcoinj

i have private key

String str="5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf";

i try get compress and decompress. 2 adress. in this format :
1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm  1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH

i do

Quote
NetworkParameters params = MainNetParams.get();
      ECKey key;

      if (str.length() == 51 || str.length() == 52) {
          DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, str);
          key = dumpedPrivateKey.getKey();            //ERROR
      } else {
          BigInteger privKey = Base58.decodeToBigInteger(str);
          key = ECKey.fromPrivate(privKey);
      }
.
.
.


and
Quote
Exception in thread "main" java.lang.IllegalArgumentException
   at com.google.common.base.Preconditions.checkArgument(Preconditions.java:128)
   at org.bitcoinj.core.ECKey.<init>(ECKey.java:195)
   at org.bitcoinj.core.ECKey.fromPrivate(ECKey.java:243)
   at org.bitcoinj.core.ECKey.fromPrivate(ECKey.java:259)
   at org.bitcoinj.core.DumpedPrivateKey.getKey(DumpedPrivateKey.java:101)
   at com.example.demo2.controllers.AdressFromKey.main(AdressFromKey.java:35)


what am i doing wrong?
larry_vw_1955
Sr. Member
****
Offline Offline

Activity: 1190
Merit: 469


View Profile
November 29, 2021, 02:03:19 AM
 #2

Java. bitcoinj


what am i doing wrong?

no idea about that but that addresses corresponds to hex private key of 0x1. cool. don't use that address because people knows about it alot. Grin
n0nce
Hero Member
*****
Offline Offline

Activity: 882
Merit: 5918


not your keys, not your coins!


View Profile WWW
November 29, 2021, 03:17:46 AM
Merited by ABCbits (3), NeuroticFish (2), pooya87 (1), nc50lc (1)
 #3

According to the error message, I looked at the code and found this:

Code: (https://github.com/bitcoinj/bitcoinj/blob/31c7e5fbceb9884cb02d2dabc755009caa2d613e/core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java#L100)
public ECKey getKey() {
    return ECKey.fromPrivate(Arrays.copyOf(bytes, 32), isPubKeyCompressed());
}

So, you're calling getKey() just fine, but something breaks when this method calls ECKey.fromPrivate(...).
It seems to me that there's something wrong with bytes, so probably your initialization of dumpedPrivateKey is wrong. Might want to look into that a bit more.

Thanks to @larry_vw_1955, I think I got it.
that addresses corresponds to hex private key of 0x1.

We can see in the Java code that it doesn't permit private keys zero or one exactly because they're not allowed to be used:

Code: (https://github.com/bitcoinj/bitcoinj/blob/31c7e5fbceb9884cb02d2dabc755009caa2d613e/core/src/main/java/org/bitcoinj/core/ECKey.java#L193)
checkArgument(!priv.equals(BigInteger.ZERO));
checkArgument(!priv.equals(BigInteger.ONE));

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
pooya87
Legendary
*
Offline Offline

Activity: 3626
Merit: 10994


Crypto Swap Exchange


View Profile
November 29, 2021, 04:22:44 AM
Merited by ABCbits (1)
 #4

We can see in the Java code that it doesn't permit private keys zero or one exactly because they're not allowed to be used:
It is worth mentioning that zero is an invalid bitcoin private key because it is outside the allowed private key range ([0,N)). But one is a perfectly valid private key and it is a bug in the library to reject it as invalid with an exception.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
November 29, 2021, 06:47:07 AM
Last edit: November 29, 2021, 07:06:14 AM by Sanka555
 #5

Thanks a lot.  My task turned out to be very successful Undecided

I am trying to generate these keys
https://keys.lol/bitcoin/1

and get the corresponding addresses. They all knock out a bug.

Maybe there is some way to hardcode and get these addresses without libraries?
n0nce
Hero Member
*****
Offline Offline

Activity: 882
Merit: 5918


not your keys, not your coins!


View Profile WWW
November 29, 2021, 11:35:05 AM
 #6

Thanks a lot.  My task turned out to be very successful Undecided

I am trying to generate these keys
https://keys.lol/bitcoin/1

and get the corresponding addresses. They all knock out a bug.

Maybe there is some way to hardcode and get these addresses without libraries?
Either remove that assertion which doesn't allow private key 1, which as @pooya87 said, is a bug anyway, or skip private key 1 and start with key 2 simply.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!