Author

Topic: importprivkey Invalid private key encoding (Read 338 times)

legendary
Activity: 1624
Merit: 2481
April 13, 2018, 07:58:43 AM
#7
Quote from: bob123 link=topic=3299613.msg34595058#msg34595058 date=152360975
To convert your priv key [u
into[/u] the Wallet Import Format do the following:

Check again.  You've got that backwards.
~snip~

Thanks for pointing out. I have corrected my post.
legendary
Activity: 3472
Merit: 4801
Quote from: bob123 link=topic=3299613.msg34595058#msg34595058 date=152360975
To convert your priv key [u
into[/u] the Wallet Import Format do the following:

Check again.  You've got that backwards.

The OP does NOT need to convert into WIF.  He already has the private key in WIF.

The instructions you provided are for converting OUT OF WIF into a hex representation.  Whether the OP needs to convert to hex representation will depend on the software he is importing into.  Most likely he does NOT need to convert into hex representation, since most wallet software is designed to use Wallet Import Format.

Furthermore, you should never convert from WIF into hex representation unless you have first performed WIF checksum checking.
legendary
Activity: 1624
Merit: 2481
Possibly a "compressed" vs. "uncompressed" issue... The private key you have, starting with a "5", will generate the "uncompressed" public key/address. Is it possible that your library only works with "compressed" (ie. starting with an "L" or "K") keys? Huh


This should be the answer to OP's problem.
You can read about the importprivkey function here: https://bitcoin.org/en/developer-reference#importprivkey

Quote
The private key to import into the wallet encoded in base58check using wallet import format (WIF)


[EDITED]

To convert your priv key into the Wallet Import Format do the following:

  • Convert the priv key to a byte string using Base58Check encoding
  • Drop the last 4 checksum bytes from the byte string
  • Drop the first byte (it should be 0x80). If the private key corresponded to a compressed (started with K.. or [/tt]L..[/tt]) public key, also drop the last byte (it should be 0x01).



Edit: I have confused converting FROM and TO Wallet Import Format.
This is the correct way to convert into WIF (even though it is not necessary anymore):

  • Add a 0x80 byte in front of the private key
  • Also add a 0x01 byte at the end if the private key will correspond to a compressed public key
  • Calculate the sha256 of the sha256 of this extended key
  • Take the first 4 bytes (checksum) and add them to the end
  • Convert the result from a byte string into a base58 string using Base58Check encoding



(Still the same) Source: https://en.bitcoin.it/wiki/Wallet_import_format
HCP
legendary
Activity: 2086
Merit: 4361
Possibly a "compressed" vs. "uncompressed" issue... The private key you have, starting with a "5", will generate the "uncompressed" public key/address. Is it possible that your library only works with "compressed" (ie. starting with an "L" or "K") keys? Huh
newbie
Activity: 2
Merit: 1
thanks, thanks a lot reply

thanks for confirmation that my private key code generation Is correct.
let me check my code for import private key
member
Activity: 126
Merit: 50
Ask me for Pools, Nodes and Explorers.
Well i just tested the private key with electrum and it imports just fine there. It gives out the following public address: 14JykiGRtiicLsRvU5qGkasc54BEkLs6oV

The problem is with your program, as the private key is just fine. Is there some kind of checking for the validity of private key, which might have a bug of some kind?
I don't have much knowledge about programming, but the problem is indeed after generating the private key as the private key is completely valid.

If you are making a wallet, you should get someone who is professional on security of bitcoin wallets to review your code for any bugs. This will probably cost, but im sure that you want to ensure the safety of your users.
newbie
Activity: 2
Merit: 1
Hi
I m IOS developer and I m working on bitcoin wallet.
for Private key generate I am using core bitcoin lib, and I successfully generate that key
here is code to generate key

Code:
  NSUInteger length = 32;
    NSMutableData *secret = [NSMutableData dataWithLength:length];
    OSStatus sanityCheck = noErr;

    sanityCheck = SecRandomCopyBytes(kSecRandomDefault, length, secret.mutableBytes);
    if (sanityCheck != noErr) {
        NSLog(@"Issue generating a private key.");
    }
   
    NSAssert(secret.length == 32, @"secret must be 32 bytes long");
    BTCKey *key = [[BTCKey alloc] initWithPrivateKey:secret];
   
    NSLog(@"%@",key.WIF);


now I need to use that private key using JSONrpc command  importprivkey
here is code to import private key is
Code:
{
"jsonrpc":"2.0",
    "params":{
    "privkey":"5K1PvchJcWx1fay7PQ2DGGXoZLDfZw7htVdbFrEjjDVS8MCag8h",
    "rescan":true
    },
     "method":"importprivkey"
 }

but after run that command I got error of
Code:
{
    "result": null,
    "error": {
        "code": -5,
        "message": "Invalid private key encoding"
    },
    "id": null
}

Please help me, I read lots of code but didn't find solution.
thanks in advance
Jump to: