Cant Decode Bitcoin Base58 address to byte array

0 votes

I'm trying to decode bitcoin address from Base58 string into byte array, and to do that I rewrited original function from Satoshi repository (https://github.com/bitcoin/bitcoin/blob/master/src/base58.cpp)

Original code:

static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";


bool DecodeBase58(const char *psz, std::vector<unsigned char>& vch) {

    // Skip leading spaces.

    while (*psz && isspace(*psz))

        psz++;

    // Skip and count leading '1's.

    int zeroes = 0;

    while (*psz == '1') {

        zeroes++;

        psz++;

    }

    // Allocate enough space in big-endian base256 representation.

    std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up.

    // Process the characters.

    while (*psz && !isspace(*psz)) {

        // Decode base58 character

        const char *ch = strchr(pszBase58, *psz);

        if (ch == NULL)

            return false;

        // Apply "b256 = b256 * 58 + ch".

        int carry = ch - pszBase58;

        for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) {

            carry += 58 * (*it);

            *it = carry % 256;

            carry /= 256;

        }

        assert(carry == 0);

        psz++;

    }

    // Skip trailing spaces.

    while (isspace(*psz))

        psz++;

    if (*psz != 0)

        return false;

    // Skip leading zeroes in b256.

    std::vector<unsigned char>::iterator it = b256.begin();

    while (it != b256.end() && *it == 0)

        it++;

    // Copy result into output vector.

    vch.reserve(zeroes + (b256.end() - it));

    vch.assign(zeroes, 0x00);

    while (it != b256.end())

      vch.push_back(*(it++));

    return true;

}

The code I am using:

private static string Base58characters = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

public static bool Decode(string source, ref byte[] destination)

        {

            int i = 0;

            while (i < source.Length)

            {

                if (source[i] == 0 || !Char.IsWhiteSpace(source[i]))

                {

                    break;

                }

                i++;

            }

            int zeros = 0;

            while (source[i] == '1')

            {

                zeros++;

                i++;

            }

            byte[] b256 = new byte[(source.Length - i) * 733 / 1000 + 1];

            while (i < source.Length && !Char.IsWhiteSpace(source[i]))

            {

                int ch = Base58characters.IndexOf(source[i]);

                if (ch == -1) //null

                {

                    return false;

            }

            int carry = Base58characters.IndexOf(source[i]);

            for (int k = b256.Length - 1; k > 0; k--)

            {

                carry += 58 * b256[k];

                b256[k] = (byte)(carry % 256);

                carry /= 256;

            }

            i++;

        }

        while (i < source.Length && Char.IsWhiteSpace(source[i]))

        {

            i++;

        }

        if (i != source.Length)

        {

            return false;

        }

        int j = 0;

        while (j < b256.Length && b256[j] == 0)

        {

            j++;

        }

        destination = new byte[zeros + (b256.Length - j)];

        for (int kk = 0; kk < destination.Length; kk++)

        {

            if (kk < zeros)

            {

                destination[kk] = 0x00;

            }

            else

            {

                destination[kk] = b256[j++];

            }

        }

        return true;

    }

Function that I'm using for converting from byte-array to HexString

public static string ByteArrayToHexString(byte[] source)

        {

            return BitConverter.ToString(source).Replace("-", "");

        }
Aug 21, 2018 in Blockchain by slayer
• 29,050 points
91 views

1 answer to this question.

0 votes

In your for-loop:

for (int k = b256.Length - 1; k > 0; k--)

The loop condition should be k >= 0 so that you don't skip the first byte in b256.

answered Aug 21, 2018 by digger
• 27,640 points

Related Questions In Blockchain

0 votes
1 answer

How to convert byte array to hex string?

You are missing the padding in the ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,050 points
48 views
+3 votes
3 answers

How to validate bitcoin address?

I found these 2 scripts online: Javascript: <html> <head> <script type="text/javascript" ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,050 points
817 views
0 votes
1 answer

How to generate Bitcoin address?

Convert the hex string back to bytes ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,050 points
69 views
0 votes
2 answers

How to check if bitcoin address is valid?

For Java, have a look at the ...READ MORE

answered Aug 20, 2018 in Blockchain by Omkar
• 67,460 points
287 views
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
75 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
140 views
0 votes
1 answer
0 votes
1 answer

How to extract h160 address of bitcoin blockchain?

You can extract the data using the ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 27,640 points
121 views
0 votes
1 answer

Cant connect Jsonrpc to bitcoin.

I tried the following code and it ...READ MORE

answered Aug 20, 2018 in Blockchain by digger
• 27,640 points
45 views