Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: tkbx on March 21, 2014, 07:24:31 AM



Title: Base58?
Post by: tkbx on March 21, 2014, 07:24:31 AM
Where can I find a Python function to convert bytes to base58 for addresses and wallet import format private keys?

https://i.imgur.com/pNAebOE.png

The wiki only has what appears to be some kind of pseudocode.


Title: Re: Base58?
Post by: flatfly on March 21, 2014, 08:29:09 AM
I have some really tight code for this on my home PC. Will try to remember to post it this when I get a chance this weekend - that is, if nobody beats me to it. :)  There are literally dozens of implementations around.
If you can't wait, you can try digging through the Electrum and Armory repos.


Title: Re: Base58?
Post by: deepceleron on March 21, 2014, 09:17:20 AM
http://we.lovebitco.in/paperwal.py

Has two functions o_b58():with bitcoin address checksum and b58encode():non checksum. You can use it as an import library. Since it makes a bitcoin address, everything library-wise that you need to make a paper wallet is obviously included.

https://bitcointalk.org/index.php?topic=361092.0


Title: Re: Base58?
Post by: tkbx on March 21, 2014, 09:24:45 AM
http://we.lovebitco.in/paperwal.py

Has two functions o_b58():with bitcoin address checksum and b58encode():non checksum. You can use it as an import library. Since it makes a bitcoin address, everything library-wise that you need to make a paper wallet is obviously included.

https://bitcointalk.org/index.php?topic=361092.0
Perfect, thanks

Just in case anyone finds this question by googling and wants the solution:

Code:
def b58encode(v):
""" gavin bitcointool - encode v, which is a string of bytes, to base58.
"""
_b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
_b58base = len(_b58chars)

#(c style int->base256)
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
zeropad = 0
for c in v:
if c == '\x00':
zeropad += 1
else:
break
return '1'*zeropad + result


Title: Re: Base58?
Post by: jadescorpio on March 22, 2014, 12:11:58 PM
Why do you need to convert bytes to base58 for addresses and wallet import format private keys? How can it be useful?


Title: Re: Base58?
Post by: KawalGrover on March 23, 2014, 02:48:08 PM
Why do you need to convert bytes to base58 for addresses and wallet import format private keys? How can it be useful?


From the bitcoin wiki:

// Why base-58 instead of standard base-64 encoding?
// - Don't want 0OIl characters that look the same in some fonts and
//      could be used to create visually identical looking account numbers.
// - A string with non-alphanumeric characters is not as easily accepted as an account number.
// - E-mail usually won't line-break if there's no punctuation to break at.
// - Doubleclicking selects the whole number as one word if it's all alphanumeric.