Bitcoin Forum
January 11, 2026, 10:21:44 PM *
News: Latest Bitcoin Core release: 30.2 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Importing a 256-bit random number as the private key?  (Read 76 times)
PorkyP (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 4


View Profile
October 28, 2025, 02:54:48 AM
Merited by pooya87 (2), vapourminer (1), ertil (1)
 #1

I would prefer to generate my own random numbers (von Neumann coin flip "algorithm").  Because I have never yet managed to figure out how to convert a 32-byte hexadecimal number to WIF, I'd prefer to just enter them in hex number format.

Is there any way to do this?  I've tried digging through the source code to use the internal functions to generate the WIF, but haven't found the way to do it correctly.  Anyone know which function calls to use with a plain 256-bit random number to get the WIF?

Exporting a wallet in plaintext likewise doesn't really work because the keys are 33 hex bytes long -- there appears to be either a checksum or something else appended or added to them.  When I overwrite one of these hex numbers with my own hex private key, leaving either the front byte in place, and then reload the wallet, I get an error, which is why I think there must be a checksum.  The front byte is always 02 or 03, and I've tried both, and still get errors either way.

I've tried asking this on the Reddit forum and whoever moderates it just deletes my posts, so I figured I'd try here.
ertil
Member
**
Offline Offline

Activity: 118
Merit: 211


View Profile
October 28, 2025, 04:33:29 AM
 #2

Quote
The front byte is always 02 or 03, and I've tried both, and still get errors either way.
If you don't know the difference between the private, and the public key, then you probably shouldn't process real coins with your tools. Try test networks like regtest first.

Quote
how to convert a 32-byte hexadecimal number to WIF
https://en.bitcoin.it/wiki/Wallet_import_format

Quote
I'd prefer to just enter them in hex number format.
Using WIF is optional. If you don't like it, then you don't have to use it. WIF is just some hex chars, after base58 encoding, nothing more. If you prefer base16 over base58, then you can work with base16 WIFs, and get pretty much the same results.

For example:
Code:
base16: ef00000000000000000000000000000000000000000000000000000000000000010184e38d1f
base58: cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA
If you don't want to use base58, then just stick with base16, it is just one encoding away from what is used by many other tools.

Some base58 tools: https://learnmeabitcoin.com/technical/keys/base58/
nc50lc
Legendary
*
Offline Offline

Activity: 3024
Merit: 8229


Self-proclaimed Genius


View Profile
October 28, 2025, 07:40:20 AM
Merited by vapourminer (4), pooya87 (4), Abdussamad (2), hugeblack (2)
 #3

Is there any way to do this?  I've tried digging through the source code to use the internal functions to generate the WIF, but haven't found the way to do it correctly.
Anyone know which function calls to use with a plain 256-bit random number to get the WIF?
This "serialize_privkey" internal function should work: github.com/spesmilo/electrum/blob/master/electrum/bitcoin.py#L620

For example, replace "0000000000000000000000000000000000000000000000000000000000000002" with your HEX private key:
Code:
bitcoin.serialize_privkey(bytes.fromhex("0000000000000000000000000000000000000000000000000000000000000002"),"true","banana")
You can replace "true" compressed bool with a blank "" if you want to derive a WIF without the compressed flag byte.
And the last positional arg is just the prefix shown when exporting a private key so it will be displayed in front of the encoded WIF like in the example below,
Use the standard prefixes (e.g.: p2pkh) if you just want to copy-paste it to import.
Result of the example command:
Code:
'banana:cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87K7XCyj5v'

Exporting a wallet in plaintext likewise doesn't really work because the keys are 33 hex bytes long -- there appears to be either a checksum or something else appended or added to them.  When I overwrite one of these hex numbers with my own hex private key, leaving either the front byte in place, and then reload the wallet, I get an error, which is why I think there must be a checksum.  The front byte is always 02 or 03, and I've tried both, and still get errors either way.
Those exported keys are most likely "public keys" based from your description.

███████████████████████████
███████▄████████████▄██████
████████▄████████▄████████
███▀█████▀▄███▄▀█████▀███
█████▀█▀▄██▀▀▀██▄▀█▀█████
███████▄███████████▄███████
███████████████████████████
███████▀███████████▀███████
████▄██▄▀██▄▄▄██▀▄██▄████
████▄████▄▀███▀▄████▄████
██▄███▀▀█▀██████▀█▀███▄███
██▀█▀████████████████▀█▀███
███████████████████████████
.
.Duelbits PREDICT..
█████████████████████████
█████████████████████████
███████████▀▀░░░░▀▀██████
██████████░░▄████▄░░████
█████████░░████████░░████
█████████░░████████░░████
█████████▄▀██████▀▄████
████████▀▀░░░▀▀▀▀░░▄█████
██████▀░░░░██▄▄▄▄████████
████▀░░░░▄███████████████
█████▄▄█████████████████
█████████████████████████
█████████████████████████
.
.WHERE EVERYTHING IS A MARKET..
█████
██
██







██
██
██████
Will Bitcoin hit $200,000
before January 1st 2027?

    No @1.15         Yes @6.00    
█████
██
██







██
██
██████

  CHECK MORE > 
Abdussamad
Legendary
*
Offline Offline

Activity: 3897
Merit: 1680



View Profile
October 29, 2025, 08:47:23 AM
Merited by vapourminer (2), hugeblack (2), ABCbits (1), nc50lc (1)
 #4

true shouldn't be in quotes. It's a boolean argument not a string. It should be just True or False.
nc50lc
Legendary
*
Offline Offline

Activity: 3024
Merit: 8229


Self-proclaimed Genius


View Profile
October 30, 2025, 04:25:33 AM
Merited by hugeblack (1)
 #5

true shouldn't be in quotes. It's a boolean argument not a string. It should be just True or False.
Oh, right, thanks!
I was also playing with "banana" arg there as it seem to accept any quoted string as True and blank as False.

Anyways, the command works but if @PorkyP wants to properly set the args, use True or False for the compressed flag.

███████████████████████████
███████▄████████████▄██████
████████▄████████▄████████
███▀█████▀▄███▄▀█████▀███
█████▀█▀▄██▀▀▀██▄▀█▀█████
███████▄███████████▄███████
███████████████████████████
███████▀███████████▀███████
████▄██▄▀██▄▄▄██▀▄██▄████
████▄████▄▀███▀▄████▄████
██▄███▀▀█▀██████▀█▀███▄███
██▀█▀████████████████▀█▀███
███████████████████████████
.
.Duelbits PREDICT..
█████████████████████████
█████████████████████████
███████████▀▀░░░░▀▀██████
██████████░░▄████▄░░████
█████████░░████████░░████
█████████░░████████░░████
█████████▄▀██████▀▄████
████████▀▀░░░▀▀▀▀░░▄█████
██████▀░░░░██▄▄▄▄████████
████▀░░░░▄███████████████
█████▄▄█████████████████
█████████████████████████
█████████████████████████
.
.WHERE EVERYTHING IS A MARKET..
█████
██
██







██
██
██████
Will Bitcoin hit $200,000
before January 1st 2027?

    No @1.15         Yes @6.00    
█████
██
██







██
██
██████

  CHECK MORE > 
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!