Post
Topic
Board Bitcoin Technical Support
Topic OP
Get private_key.pem from WIF format
by
barno
on 06/03/2020, 16:09:55 UTC
I want to retrieve my private_key.pem format in order to sign my transaction

For example, I create P2SH address
Code:
$ ADDR_DEST_1=`bitcoin-cli getnewaddress`            
$ echo $ADDR_DEST_1
2MzdMAQKoPr2x7Bzm6tpGpDPnvWmsL9AjQA

I can Get Private key WIF
Code:
$ bitcoin-cli dumpprivkey $ADDR_DEST_1
cP73T5gHo6Wnyco11T2fsxocZqsQE6hwPG9UG1vcupMZh4iLyH19

Now I can do base58 decode
Code:
$ printf cP73T5gHo6Wnyco11T2fsxocZqsQE6hwPG9UG1vcupMZh4iLyH19 | base58 -d | xxd -p -c 76    
ef2d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f72301c597c498

ef  is version prefix for testnet/regtest
01 compression flag
c597c498 should be the checksum.

Then My key is: 2d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723

Now I need to get private key pem.
Then I tried with

Code:
echo 2d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723 > btc_priv.key

$ openssl ec -noout -text -inform DER -in foo_priv.key
read EC key
unable to load Key

$ openssl x509 -in btc_priv.key -inform DER -outform PEM
unable to load certificate
4486393452:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1220:
4486393452:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:386:Type=X509

$ cat btc_priv.key | xxd -r -p  > test.bin  
$ openssl ec -in test.bin -inform DER -pubin -text -noout                
read EC key
unable to load Key
4456304236:error:0D06B08E:asn1 encoding routines:ASN1_D2I_READ_BIO:not enough data:a_d2i_fp.c:247:

I searched in https://github.com/bitcoin/bitcoin/blob/452bb90c718da18a79bfad50ff9b7d1c8f1b4aa3/src/secp256k1/contrib/lax_der_privatekey_parsing.c and https://github.com/bitcoin/bitcoin/blob/99813a9745fe10a58bedd7a4cb721faf14f907a4/src/rest.cpp But I don't understand Sad