Bitcoin Forum
May 02, 2024, 09:20:30 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Get private_key.pem from WIF format  (Read 196 times)
barno (OP)
Newbie
*
Offline Offline

Activity: 23
Merit: 54


View Profile
March 06, 2020, 04:09:55 PM
Last edit: March 08, 2020, 11:30:07 AM by barno
 #1

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
1714684830
Hero Member
*
Offline Offline

Posts: 1714684830

View Profile Personal Message (Offline)

Ignore
1714684830
Reply with quote  #2

1714684830
Report to moderator
The forum was founded in 2009 by Satoshi and Sirius. It replaced a SourceForge forum.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714684830
Hero Member
*
Offline Offline

Posts: 1714684830

View Profile Personal Message (Offline)

Ignore
1714684830
Reply with quote  #2

1714684830
Report to moderator
1714684830
Hero Member
*
Offline Offline

Posts: 1714684830

View Profile Personal Message (Offline)

Ignore
1714684830
Reply with quote  #2

1714684830
Report to moderator
1714684830
Hero Member
*
Offline Offline

Posts: 1714684830

View Profile Personal Message (Offline)

Ignore
1714684830
Reply with quote  #2

1714684830
Report to moderator
BitMaxz
Legendary
*
Offline Offline

Activity: 3248
Merit: 2955


Block halving is coming.


View Profile WWW
March 06, 2020, 11:52:39 PM
 #2

I don't have much knowledge about development but I found some useful posts from other websites related to this.

Check this https://bitcoin.stackexchange.com/questions/5706/how-can-i-test-and-verify-a-vanity-key-before-i-send-coins-to-it

In the last post he is talking about converting the private key to .PEM format and I think you will need to make the private key from Wif format to hex format and convert it to pem format.

█▀▀▀











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











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
dex1
Full Member
***
Offline Offline

Activity: 141
Merit: 115



View Profile
March 07, 2020, 12:06:50 PM
Merited by ABCbits (1), Heisenberg_Hunter (1)
 #3

To feed data to openssl it needs to be formatted rightly.
Following your example:

Code:
PRE_STRING=302e0201010420
YOUR_PRIVKEY=2d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723
secp256k1_ID=a00706052b8104000a

Concatenate and convert to a binary:

Code:
echo $PRE_STRING $YOUR_PRIVKEY $secp256k1_ID | xxd -r -p > btc_priv.key

And there you go:

Code:
openssl ec -noout -text -inform DER -in btc_priv.key
read EC key
Private-Key: (256 bit)
priv:
    2d:72:38:10:6d:f8:7d:7b:70:f9:d7:cc:4f:f8:33:
    ff:9f:ad:86:aa:5e:50:f8:cf:42:68:9c:23:8d:70:
    f7:23
pub:
    04:e6:2b:19:a8:86:b1:5f:b5:cb:ad:69:f7:7f:2c:
    1d:81:6c:da:32:d9:fb:85:0b:97:e8:10:6a:38:70:
    0c:4c:93:79:65:64:ff:5a:d4:96:3e:d4:1c:a8:3f:
    71:a7:08:72:b9:79:5b:09:dd:6c:b2:28:64:e3:58:
    c4:85:73:d9:a7
ASN1 OID: secp256k1

https://stackoverflow.com/questions/48101258/how-to-convert-an-ecdsa-key-to-pem-format/49213805#49213805






dex1
Full Member
***
Offline Offline

Activity: 141
Merit: 115



View Profile
March 07, 2020, 12:30:51 PM
Merited by barno (5), ABCbits (2), Heisenberg_Hunter (1)
 #4

Actually as OP asked for a pem format (following from example above):

Code:
$ openssl ec -inform d < btc_priv.key
read EC key
writing EC key
-----BEGIN EC PRIVATE KEY-----
MC4CAQEEIC1yOBBt+H17cPnXzE/4M/+frYaqXlD4z0JonCONcPcjoAcGBSuBBAAK
-----END EC PRIVATE KEY-----

barno (OP)
Newbie
*
Offline Offline

Activity: 23
Merit: 54


View Profile
March 08, 2020, 11:32:58 AM
 #5

Amazing dex1, it works!
Can you explain
Code:
PRE_STRING=302e0201010420
secp256k1_ID=a00706052b8104000a
I searched more details but I don't find good resources
BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
March 09, 2020, 04:53:24 AM
Merited by ABCbits (1), Heisenberg_Hunter (1), barno (1)
 #6

Amazing dex1, it works!
Can you explain
Code:
PRE_STRING=302e0201010420
secp256k1_ID=a00706052b8104000a
I searched more details but I don't find good resources

it is all the "stuff" you want to give OpenSSL in one place encoded using ASN.1 DER encoding.
Code:
302e02010104202d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723a00706052b8104000a
which is:
Code:
30 : sequence tag
2e : length = 46
02 : int tag
01 : length = 1
01 : int value = 1
04 : octet string tag
20 : length = 32
2d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723 : the string (key hex)
a0 : context specific tag
07 : length = 7
06 : object identifier tag
05 : length = 5
2b8104000a : sekp256k1 identifier

There is a FOMO brewing...
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!