Bitcoin Forum
September 16, 2019, 09:15:11 AM *
News: If you like a topic and you see an orange "bump" link, click it. More info.
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: Python snippet to create Bitcoin public/private key pairs  (Read 3590 times)
tyz
Legendary
*
Offline Offline

Activity: 1988
Merit: 1124



View Profile
May 14, 2015, 12:10:54 PM
 #1

I am looking for a Python snippet to create public/private key pairs for Bitcoin just by providing an arbitrary number between 1 and 10^77. Can someone provide such a snippet?
1568625311
Hero Member
*
Offline Offline

Posts: 1568625311

View Profile Personal Message (Offline)

Ignore
1568625311
Reply with quote  #2

1568625311
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
May 15, 2015, 01:11:03 AM
 #2

I don't have the snippet for you ready-made, but I'd take a look in the pycoin library, specifically the Key class:

https://github.com/richardkiss/pycoin
https://github.com/richardkiss/pycoin/blob/master/pycoin/key/Key.py

The Key class has several methods which do something pretty close to what you want.  Specifically, check out the Key.from_text(text) method and the subroutines in there.  I think you'll find it helpful (I hope).
tyz
Legendary
*
Offline Offline

Activity: 1988
Merit: 1124



View Profile
May 15, 2015, 05:12:38 PM
 #3

Thanks for sharing!
It took me some time to extract the right code lines but I got it.
It's not the best solution but it does it's job Smiley

I don't have the snippet for you ready-made, but I'd take a look in the pycoin library, specifically the Key class:

https://github.com/richardkiss/pycoin
https://github.com/richardkiss/pycoin/blob/master/pycoin/key/Key.py

The Key class has several methods which do something pretty close to what you want.  Specifically, check out the Key.from_text(text) method and the subroutines in there.  I think you'll find it helpful (I hope).
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
May 15, 2015, 07:02:32 PM
 #4

Thanks for sharing!
It took me some time to extract the right code lines but I got it.
It's not the best solution but it does it's job Smiley


I don't have the snippet for you ready-made, but I'd take a look in the pycoin library, specifically the Key class:

https://github.com/richardkiss/pycoin
https://github.com/richardkiss/pycoin/blob/master/pycoin/key/Key.py

The Key class has several methods which do something pretty close to what you want.  Specifically, check out the Key.from_text(text) method and the subroutines in there.  I think you'll find it helpful (I hope).

No problem, out of curiosity, which lines did you end up extracting and why did you find it less than optimal?
Juvenal
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
May 15, 2015, 10:10:50 PM
 #5

I am looking for a Python snippet to create public/private key pairs for Bitcoin just by providing an arbitrary number between 1 and 10^77. Can someone provide such a snippet?

Try this:
Quote
from ecdsa.ecdsa import *
from binascii import hexlify

def getKeys(secretNumber):
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = hexlify('\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y()))
  formattedPrivKey = hex(secretNumber)[2:-1]
  return (formattedPrivKey, formattedPubKey)


If you don't have python-ecdsa installed, you can
get it here: https://github.com/warner/python-ecdsa
tyz
Legendary
*
Offline Offline

Activity: 1988
Merit: 1124



View Profile
May 15, 2015, 11:28:33 PM
 #6

I run a test and it gives me the key in the following format. Seed number was 10000000000000000000.

Private:
8ac7230489e80000

Public:
041330daea0d10ec59a9f65dedd79651fad3388a2f16738678c2a1c67077038dab0e909efa28690 12fc532b4552ae7400c8760113b2fcc6f76b71a7077d43a5f9f


How can I get the format 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm (public key) and 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf (private key) ?

I am looking for a Python snippet to create public/private key pairs for Bitcoin just by providing an arbitrary number between 1 and 10^77. Can someone provide such a snippet?

Try this:
Quote
from ecdsa.ecdsa import *
from binascii import hexlify

def getKeys(secretNumber):
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = hexlify('\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y()))
  formattedPrivKey = hex(secretNumber)[2:-1]
  return (formattedPrivKey, formattedPubKey)


If you don't have python-ecdsa installed, you can
get it here: https://github.com/warner/python-ecdsa
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
May 16, 2015, 02:55:05 AM
 #7

There are a few more steps to go from a public-key to a bitcoin address, you have to do some hashing, append a checksum, then finally convert to bitcoin base58.  The details you want are here: https://en.bitcoin.it/wiki/Technical_background_of_Bitcoin_addresses

I run a test and it gives me the key in the following format. Seed number was 10000000000000000000.

Private:
8ac7230489e80000

Public:
041330daea0d10ec59a9f65dedd79651fad3388a2f16738678c2a1c67077038dab0e909efa28690 12fc532b4552ae7400c8760113b2fcc6f76b71a7077d43a5f9f


How can I get the format 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm (public key) and 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf (private key) ?

I am looking for a Python snippet to create public/private key pairs for Bitcoin just by providing an arbitrary number between 1 and 10^77. Can someone provide such a snippet?

Try this:
Quote
from ecdsa.ecdsa import *
from binascii import hexlify

def getKeys(secretNumber):
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = hexlify('\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y()))
  formattedPrivKey = hex(secretNumber)[2:-1]
  return (formattedPrivKey, formattedPubKey)


If you don't have python-ecdsa installed, you can
get it here: https://github.com/warner/python-ecdsa
Juvenal
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
May 16, 2015, 04:01:06 AM
 #8

How can I get the format 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm (public key) and 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf (private key) ?
Your "public key" is actually a bitcoin address, and your "private
key" has been WIF encoded.

Here is a revised snippet:

Quote
from ecdsa.ecdsa import *
from binascii import hexlify, unhexlify
import hashlib

__zeros = '0000000000000000000000000000000000000000000000000000000000000000'
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

def b58encode(v):
  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += (256**i) * ord(c)
  result = ''
  while long_value >= __b58base:
    div, mod = divmod(long_value, __b58base)
    result = __b58chars[mod] + result
    long_value = div
  result = __b58chars[long_value] + result
  nPad = 0
  for c in v:
    if c == '\0': nPad += 1
    else: break
  return (__b58chars[0]*nPad) + result


def getKeys(secretNumber):
  rpmd160hash = hashlib.new('ripemd160')
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = '\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y())
  rpmd160hash.update(hashlib.sha256(formattedPubKey).digest())
  btcAddress = '\x00'+rpmd160hash.digest()
  chksum = hashlib.sha256(hashlib.sha256(btcAddress).digest()).digest()[:4]
  btcAddress = b58encode(btcAddress+chksum)
  formattedPubKey = hexlify(formattedPubKey)
  formattedPrivKey = hexlify(int_to_string(secretNumber))
  formattedPrivKey = '80'+__zeros[0:64-len(formattedPrivKey)]+formattedPrivKey
  formattedPrivKey = unhexlify(formattedPrivKey)
  chksum = hashlib.sha256(hashlib.sha256(formattedPrivKey).digest()).digest()[:4]
  formattedPrivKey = b58encode(formattedPrivKey+chksum)
  return (btcAddress,formattedPrivKey, formattedPubKey)


secretNumber = 10000000000000000000
bitcoinAddress, privateKey, publicKey = getKeys(secretNumber)
print 'SecretNumber: ',secretNumber
print 'BitcoinAddress: '+bitcoinAddress
print 'PrivateKey: '+privateKey
print 'PublicKey: '+publicKey

tyz
Legendary
*
Offline Offline

Activity: 1988
Merit: 1124



View Profile
May 16, 2015, 11:11:19 AM
 #9

Thanks, the snippet works very well! Exactly what I have looked for.
Great work!
ThePiGuy
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile WWW
May 31, 2015, 03:54:20 AM
 #10

Step #2 - Convert to Brainfuck for fun because you know why not.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
June 01, 2015, 04:32:17 AM
 #11

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?
ThePiGuy
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile WWW
June 01, 2015, 04:37:21 PM
 #12

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
June 01, 2015, 05:27:02 PM
 #13

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.

I don't know anything about programming for Arduino, but I'm really surprised if you can't compile C code for it.  And since you already have all the bitcoin core, there's also a libbitcoin in C, don't you already have what you need to convert keys on arduino?  Also, isn't arduino just an arm7?  Can't you run python on linux on it?  Sorry for my ignorance.
ThePiGuy
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile WWW
June 01, 2015, 09:20:22 PM
 #14

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.

I don't know anything about programming for Arduino, but I'm really surprised if you can't compile C code for it.  And since you already have all the bitcoin core, there's also a libbitcoin in C, don't you already have what you need to convert keys on arduino?  Also, isn't arduino just an arm7?  Can't you run python on linux on it?  Sorry for my ignorance.

You are somewhat right, but it's not a 64-bit machine like Bitcoin Core is meant to run on though.  It's not quite that easy, because it's memory is on the order of 16 KB.

Brainfuck on the other hand, is SOOO easy to implement if all you need to do is generate an address.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
June 01, 2015, 09:23:28 PM
 #15

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.

I don't know anything about programming for Arduino, but I'm really surprised if you can't compile C code for it.  And since you already have all the bitcoin core, there's also a libbitcoin in C, don't you already have what you need to convert keys on arduino?  Also, isn't arduino just an arm7?  Can't you run python on linux on it?  Sorry for my ignorance.

You are somewhat right, but it's not a 64-bit machine like Bitcoin Core is meant to run on though.  It's not quite that easy, because it's memory is on the order of 16 KB.

Brainfuck on the other hand, is SOOO easy to implement if all you need to do is generate an address.

If it's SOOO easy, maybe you can do it yourself if you check out those python methods in that library I linked to above.  I've never written a line of brainfuck in my life so I don't think I can help you.

Cheers!
ThePiGuy
Newbie
*
Offline Offline

Activity: 25
Merit: 0


View Profile WWW
June 02, 2015, 03:22:02 AM
 #16

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.

I don't know anything about programming for Arduino, but I'm really surprised if you can't compile C code for it.  And since you already have all the bitcoin core, there's also a libbitcoin in C, don't you already have what you need to convert keys on arduino?  Also, isn't arduino just an arm7?  Can't you run python on linux on it?  Sorry for my ignorance.

You are somewhat right, but it's not a 64-bit machine like Bitcoin Core is meant to run on though.  It's not quite that easy, because it's memory is on the order of 16 KB.

Brainfuck on the other hand, is SOOO easy to implement if all you need to do is generate an address.

If it's SOOO easy, maybe you can do it yourself if you check out those python methods in that library I linked to above.  I've never written a line of brainfuck in my life so I don't think I can help you.

Cheers!

I didn't say coding in Brainfuck was easy...it's maybe the most confusing thing I've ever tried to do (but there are some real pros in it).  I just said that implementing an interpreter in it is really easy in just about any other language.
tspacepilot
Legendary
*
Offline Offline

Activity: 1456
Merit: 1053


I may write code in exchange for bitcoins.


View Profile
June 02, 2015, 04:54:14 AM
 #17

Step #2 - Convert to Brainfuck for fun because you know why not.

Are you talking about brainfuck the language http://en.wikipedia.org/wiki/Brainfuck?  Is there some kind of relevance for bumping this 2 week old thread?  Do you actually want code for generating keys in brainfuck or where you just bumping your post count?

No, I actually want someone to convert it to that language because not only would it be a pretty great challenge, but Brainfuck can easily be implemented on an Arduino (or literally any other platform), so it could actually serve to be a very useful function.

I don't know anything about programming for Arduino, but I'm really surprised if you can't compile C code for it.  And since you already have all the bitcoin core, there's also a libbitcoin in C, don't you already have what you need to convert keys on arduino?  Also, isn't arduino just an arm7?  Can't you run python on linux on it?  Sorry for my ignorance.

You are somewhat right, but it's not a 64-bit machine like Bitcoin Core is meant to run on though.  It's not quite that easy, because it's memory is on the order of 16 KB.

Brainfuck on the other hand, is SOOO easy to implement if all you need to do is generate an address.

If it's SOOO easy, maybe you can do it yourself if you check out those python methods in that library I linked to above.  I've never written a line of brainfuck in my life so I don't think I can help you.

Cheers!

I didn't say coding in Brainfuck was easy...it's maybe the most confusing thing I've ever tried to do (but there are some real pros in it).  I just said that implementing an interpreter in it is really easy in just about any other language.

So you can implement your own interpreter really easy (I'm guessing you're talking about a REPL http://en.wikipedia.org/wiki/Read–eval–print_loop) but you can't implement some string manipulation very easy?  I must still be misunderstanding something.  Anyway, I can't help you when it comes to coding in brainfuck.  Dunno what else to say.
tyz
Legendary
*
Offline Offline

Activity: 1988
Merit: 1124



View Profile
June 02, 2015, 03:17:14 PM
 #18

@ThePiGuy: the problem was solved. open an own thread with asking for someone who can implement keys generation into Bf language.
donGeilo
Full Member
***
Offline Offline

Activity: 169
Merit: 100



View Profile
July 31, 2016, 03:19:53 PM
 #19

I know this thread is quite old, but maybe somebody can help me,
how should this script look for compressed addresses???

How can I get the format 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm (public key) and 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf (private key) ?
Your "public key" is actually a bitcoin address, and your "private
key" has been WIF encoded.

Here is a revised snippet:

Quote
from ecdsa.ecdsa import *
from binascii import hexlify, unhexlify
import hashlib

__zeros = '0000000000000000000000000000000000000000000000000000000000000000'
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

def b58encode(v):
  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += (256**i) * ord(c)
  result = ''
  while long_value >= __b58base:
    div, mod = divmod(long_value, __b58base)
    result = __b58chars[mod] + result
    long_value = div
  result = __b58chars[long_value] + result
  nPad = 0
  for c in v:
    if c == '\0': nPad += 1
    else: break
  return (__b58chars[0]*nPad) + result


def getKeys(secretNumber):
  rpmd160hash = hashlib.new('ripemd160')
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = '\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y())
  rpmd160hash.update(hashlib.sha256(formattedPubKey).digest())
  btcAddress = '\x00'+rpmd160hash.digest()
  chksum = hashlib.sha256(hashlib.sha256(btcAddress).digest()).digest()[:4]
  btcAddress = b58encode(btcAddress+chksum)
  formattedPubKey = hexlify(formattedPubKey)
  formattedPrivKey = hexlify(int_to_string(secretNumber))
  formattedPrivKey = '80'+__zeros[0:64-len(formattedPrivKey)]+formattedPrivKey
  formattedPrivKey = unhexlify(formattedPrivKey)
  chksum = hashlib.sha256(hashlib.sha256(formattedPrivKey).digest()).digest()[:4]
  formattedPrivKey = b58encode(formattedPrivKey+chksum)
  return (btcAddress,formattedPrivKey, formattedPubKey)


secretNumber = 10000000000000000000
bitcoinAddress, privateKey, publicKey = getKeys(secretNumber)
print 'SecretNumber: ',secretNumber
print 'BitcoinAddress: '+bitcoinAddress
print 'PrivateKey: '+privateKey
print 'PublicKey: '+publicKey


xcbtrader
Hero Member
*****
Offline Offline

Activity: 867
Merit: 1006


View Profile
August 01, 2016, 01:38:14 PM
 #20

I know this thread is quite old, but maybe somebody can help me,
how should this script look for compressed addresses???

How can I get the format 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm (public key) and 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf (private key) ?
Your "public key" is actually a bitcoin address, and your "private
key" has been WIF encoded.

Here is a revised snippet:

Quote
from ecdsa.ecdsa import *
from binascii import hexlify, unhexlify
import hashlib

__zeros = '0000000000000000000000000000000000000000000000000000000000000000'
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

def b58encode(v):
  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += (256**i) * ord(c)
  result = ''
  while long_value >= __b58base:
    div, mod = divmod(long_value, __b58base)
    result = __b58chars[mod] + result
    long_value = div
  result = __b58chars[long_value] + result
  nPad = 0
  for c in v:
    if c == '\0': nPad += 1
    else: break
  return (__b58chars[0]*nPad) + result


def getKeys(secretNumber):
  rpmd160hash = hashlib.new('ripemd160')
  pubKey = Public_key(generator_secp256k1,generator_secp256k1*secretNumber)
  formattedPubKey = '\x04'+int_to_string(pubKey.point.x())+int_to_string(pubKey.point.y())
  rpmd160hash.update(hashlib.sha256(formattedPubKey).digest())
  btcAddress = '\x00'+rpmd160hash.digest()
  chksum = hashlib.sha256(hashlib.sha256(btcAddress).digest()).digest()[:4]
  btcAddress = b58encode(btcAddress+chksum)
  formattedPubKey = hexlify(formattedPubKey)
  formattedPrivKey = hexlify(int_to_string(secretNumber))
  formattedPrivKey = '80'+__zeros[0:64-len(formattedPrivKey)]+formattedPrivKey
  formattedPrivKey = unhexlify(formattedPrivKey)
  chksum = hashlib.sha256(hashlib.sha256(formattedPrivKey).digest()).digest()[:4]
  formattedPrivKey = b58encode(formattedPrivKey+chksum)
  return (btcAddress,formattedPrivKey, formattedPubKey)


secretNumber = 10000000000000000000
bitcoinAddress, privateKey, publicKey = getKeys(secretNumber)
print 'SecretNumber: ',secretNumber
print 'BitcoinAddress: '+bitcoinAddress
print 'PrivateKey: '+privateKey
print 'PublicKey: '+publicKey



This scrypt is very slow.
Please, use this:

https://github.com/vbuterin/pybitcointools

Exemple:
Code:
from bitcoin import *

priv = random_key()
pub = privtopub(priv)
addr = pubtoaddr(pub)
wifKey = encode_privkey(priv, 'wif')

print 'Bitcoin Address: ' + addr
print 'WIF Key: ' + wifKey

Pages: [1] 2 »  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!