Title: Get private_key.pem from WIF format Post by: barno on March 06, 2020, 04:09:55 PM 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` I can Get Private key WIF Code: $ bitcoin-cli dumpprivkey $ADDR_DEST_1 Now I can do base58 decode Code: $ printf cP73T5gHo6Wnyco11T2fsxocZqsQE6hwPG9UG1vcupMZh4iLyH19 | base58 -d | xxd -p -c 76 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 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 :( Title: Re: Get private_key.pem from WIF format Post by: BitMaxz on March 06, 2020, 11:52:39 PM 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. Title: Re: Get private_key.pem from WIF format Post by: dex1 on March 07, 2020, 12:06:50 PM To feed data to openssl it needs to be formatted rightly.
Following your example: Code: PRE_STRING=302e0201010420 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 https://stackoverflow.com/questions/48101258/how-to-convert-an-ecdsa-key-to-pem-format/49213805#49213805 (https://stackoverflow.com/questions/48101258/how-to-convert-an-ecdsa-key-to-pem-format/49213805#49213805) Title: Re: Get private_key.pem from WIF format Post by: dex1 on March 07, 2020, 12:30:51 PM Actually as OP asked for a pem format (following from example above):
Code: $ openssl ec -inform d < btc_priv.key Title: Re: Get private_key.pem from WIF format Post by: barno on March 08, 2020, 11:32:58 AM Amazing dex1, it works!
Can you explain Code: PRE_STRING=302e0201010420 Title: Re: Get private_key.pem from WIF format Post by: BrewMaster on March 09, 2020, 04:53:24 AM Amazing dex1, it works! Can you explain Code: PRE_STRING=302e0201010420 it is all the "stuff" you want to give OpenSSL in one place encoded using ASN.1 DER encoding. Code: 302e02010104202d7238106df87d7b70f9d7cc4ff833ff9fad86aa5e50f8cf42689c238d70f723a00706052b8104000a Code: 30 : sequence tag |