Bitcoin Forum
November 12, 2024, 04:29:12 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: importprivkey Invalid private key encoding  (Read 338 times)
deepakbhati99 (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 1


View Profile
April 11, 2018, 10:26:51 AM
Merited by LoyceV (1)
 #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
Sellingaccs
Member
**
Offline Offline

Activity: 126
Merit: 50

Ask me for Pools, Nodes and Explorers.


View Profile
April 11, 2018, 04:59:54 PM
Last edit: April 11, 2018, 05:40:35 PM by Sellingaccs
 #2

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.

deepakbhati99 (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 1


View Profile
April 12, 2018, 06:26:01 AM
 #3

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
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4361

<insert witty quote here>


View Profile
April 13, 2018, 03:03:32 AM
 #4

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

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
April 13, 2018, 08:55:57 AM
Last edit: April 13, 2018, 12:57:41 PM by bob123
 #5

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

DannyHamilton
Legendary
*
Offline Offline

Activity: 3486
Merit: 4832



View Profile
April 13, 2018, 12:19:51 PM
Merited by bob123 (2)
 #6

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.
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
April 13, 2018, 12:58:43 PM
 #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.

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!