Bitcoin Forum
June 13, 2024, 11:40:35 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: libBase58 C++ implementation  (Read 1503 times)
pandalion98 (OP)
Sr. Member
****
Offline Offline

Activity: 504
Merit: 250



View Profile WWW
August 30, 2014, 11:22:00 AM
 #1

I don't get it. That's it.

Help me understand  Grin


"Encoding Base58
---------------

Allocate a string to store the base58 content, create a size_t variable with the
size of that allocation, and call:
   bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
Note that you must pass a pointer to the string size variable, not the size
itself. When b58enc returns, the variable will be modified to contain the actual
number of bytes used (including the null terminator). If encoding fails for any
reason, or if the string buffer is not large enough for the result, b58enc will
return false. Otherwise, it returns true to indicate success."

Foxpup
Legendary
*
Offline Offline

Activity: 4396
Merit: 3062


Vile Vixen and Miss Bitcointalk 2021-2023


View Profile
August 30, 2014, 11:48:49 AM
 #2

I don't get it either. I would have expected it to take only the last two arguments and return a pointer to a newly created string (or a null pointer in the event of failure). Requiring a string to already be allocated makes no damn sense, since there's no way to know how large the string needs to be before the function is called (an upper bound can be computed, but that's inelegant and slightly wasteful). I also don't see why it needs to modify b58sz, since the resulting string is null terminated, so its length can be trivially determined anyway. The whole thing makes no sense.

Will pretend to do unspeakable things (while actually eating a taco) for bitcoins: 1K6d1EviQKX3SVKjPYmJGyWBb1avbmCFM4
I am not on the scammers' paradise known as Telegram! Do not believe anyone claiming to be me off-forum without a signed message from the above address! Accept no excuses and make no exceptions!
pandalion98 (OP)
Sr. Member
****
Offline Offline

Activity: 504
Merit: 250



View Profile WWW
August 30, 2014, 11:51:34 AM
 #3

I don't get it either. I would have expected it to take only the last two arguments and return a pointer to a newly created string (or a null pointer in the event of failure). Requiring a string to already be allocated makes no damn sense, since there's no way to know how large the string needs to be before the function is called (an upper bound can be computed, but that's inelegant and slightly wasteful). I also don't see why it needs to modify b58sz, since the resulting string is null terminated, so its length can be trivially determined anyway. The whole thing makes no sense.
Yup. It doesn't make sense.

Anyone know a simple base58 library/header for c/c++?
pandalion98 (OP)
Sr. Member
****
Offline Offline

Activity: 504
Merit: 250



View Profile WWW
August 30, 2014, 12:06:31 PM
 #4

I managed to get to Step 6 of Private key to WIF.
see here: https://en.bitcoin.it/wiki/Wallet_import_format

Here's the code that did it. It's pretty messy, but it works and it's fast. (the cout function is there to confirm if it's really working):
Post some suggestions if you have some time  Cheesy

Code:
#include <iostream>
#include <iomanip>
#include <sstream>
#include "sha256.h"    //from http://www.zedwood.com/article/cpp-sha256-function
#include <ctype.h>
//extern "C"
//{
//#include "b58\CBBase58.h"    //failed attempt
//}


using namespace std;
   int a = 0;
   int b = 1;
   int h = 0;
   string j = "80";

int main()
{
   do
   {
        h++;
        stringstream streamm;

        streamm << j << setfill('0') << setw(64) << std::hex << h;
        string ggg = streamm.str();
        string g = sha256(streamm.str());
        string gg = sha256(g);

        string str = gg;
        stringstream xx;
        stringstream yy;
        xx << str[0] << str[1] << str[2] << str[3] << str[4];
        string xxx = xx.str();
        yy << ggg << xxx;
        string yyy = yy.str();
        stringstream XD;
        XD << yyy << endl;

        string lol123;
        XD >> lol123;
        cout << lol123 << endl;


   }
   while ( a < b );
   return 0;
}
hhanh00
Sr. Member
****
Offline Offline

Activity: 467
Merit: 266


View Profile
August 30, 2014, 01:37:08 PM
 #5

I don't get it. That's it.

Help me understand  Grin


"Encoding Base58
---------------

Allocate a string to store the base58 content, create a size_t variable with the
size of that allocation, and call:
   bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
Note that you must pass a pointer to the string size variable, not the size
itself. When b58enc returns, the variable will be modified to contain the actual
number of bytes used (including the null terminator). If encoding fails for any
reason, or if the string buffer is not large enough for the result, b58enc will
return false. Otherwise, it returns true to indicate success."



It's good old fashioned C 'optimized' API. If you have a buffer already allocated of size_t, you can try to use it.
However, it's too short --> you get false. And you need to try with a bigger buffer.
If it's ok, it returns the effective size that was used. The string is null terminated but by giving you the size back, you avoid a strlen which scans the string.
Believe me or not, but these kinds of cubblesome API used to be the norm when memory was precious.

pandalion98 (OP)
Sr. Member
****
Offline Offline

Activity: 504
Merit: 250



View Profile WWW
August 31, 2014, 05:51:52 AM
 #6

I don't get it. That's it.

Help me understand  Grin


"Encoding Base58
---------------

Allocate a string to store the base58 content, create a size_t variable with the
size of that allocation, and call:
   bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
Note that you must pass a pointer to the string size variable, not the size
itself. When b58enc returns, the variable will be modified to contain the actual
number of bytes used (including the null terminator). If encoding fails for any
reason, or if the string buffer is not large enough for the result, b58enc will
return false. Otherwise, it returns true to indicate success."



It's good old fashioned C 'optimized' API. If you have a buffer already allocated of size_t, you can try to use it.
However, it's too short --> you get false. And you need to try with a bigger buffer.
If it's ok, it returns the effective size that was used. The string is null terminated but by giving you the size back, you avoid a strlen which scans the string.
Believe me or not, but these kinds of cubblesome API used to be the norm when memory was precious.


Amazing...
pandalion98 (OP)
Sr. Member
****
Offline Offline

Activity: 504
Merit: 250



View Profile WWW
September 01, 2014, 12:54:38 PM
 #7

Can someone provide a really short sample code  Grin
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!