Custom Function X to Base58Check "X2B58CH"

fasbit

New Member
Joined
Feb 5, 2015
Messages
3
General:
I need a custom function to convert X to Base58 Check, where X meets format requirements.

Specific:
The Bitcoin algorithm uses a multistep process of converting a very large random number (32 byte HEX) called a ECDSA Key into a very specific new format called a public address. The last step of the process involved involves converting X into "base58check." Assuming I have X in the proper format, I need excel to convert X into Base58Check.

Read about Bitcoin Base58 Specifics HERE
Technical Background of Bitcoin Address HERE

In the 2nd link above there is a 10 step process (numbered 0 to 9) to create a Bitcoin Public Key. I have managed to get excel to do the entire process down through and including step 8. Step 9 is the stumbling block. The very last step.

Here is a Java site that does this process very nicely: Brainwallet
(The java code is available on Git Hub)
Here is a link to How this is done in C: Bitcoin
And here in Perl: Perl


HELP!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi and welcome to the MrExcel Message Board.

I am not sure that I am understanding this correctly because one of your links suggests that Base58 is not the same as Base58Check.

However, the difference between step 8 and 9 in your other link suggests that all that is required is a Hex to Base58 conversion. Namely:

00010966776006953D5567439E5E39F86A0D273BEED61967F6
Needs converting to ...

16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

If that is what you want then here is a macro function to do that:
Code:
Option Explicit
Function Base58(Base16 As String) As String

    Const Base As Long = 58
    Const lkup As String = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    
    Dim LD_Numerator As String      ' Long Division: Numerator
    Dim LD_Answer As String         ' Long Division: Answer
    
    Dim LDW_Quotient As String      ' Long Division WIP: Quotient
    Dim LDW_Dividend As String      ' Long Division WIP: Dividend
    Dim LDW_Remainder As String     ' Long Division WIP: Remainder
    Dim LDW_NextDigit As String     ' Long Division WIP: Next Digit to be copied down

    Dim i As Long

    LD_Numerator = Base16
    
    Do While (LD_Numerator <> String(Len(Base16), "0"))

        i = 1
        LDW_NextDigit = Mid(LD_Numerator, i, 1)
        LDW_Dividend = LDW_NextDigit
        LD_Answer = ""
        
        ' Long Division
        Do While (i <= Len(LD_Numerator))
         
            LDW_Quotient = Hex(Int(CLng("&H" & LDW_Dividend) / Base))
            If CLng("&H" & LDW_Dividend) >= Base Then
                LD_Answer = LD_Answer & Hex(CLng("&H" & LDW_Quotient))
            Else
                LD_Answer = LD_Answer & "0"
            End If
            LDW_Remainder = Hex(CLng("&H" & LDW_Dividend) - (CLng("&H" & LDW_Quotient) * Base))
            i = i + 1
            LDW_NextDigit = Mid(LD_Numerator, i, 1)
            LDW_Dividend = LDW_Remainder & LDW_NextDigit
           
        Loop
    
        LD_Numerator = LD_Answer
        
        Base58 = Mid(lkup, CLng("&H" & LDW_Remainder) + 1, 1) & Base58
        
    Loop

End Function
Sub Base58Test()
    Debug.Print Base58("00010966776006953D5567439E5E39F86A0D273BEED61967F6")
End Sub

The Base58Test macro is just to show how the function can be used.

The macro uses the standard technique of repeatedly dividing 58 into the number and remembering the remainders. The only complication was that the arithmetic needed to be in Hex.

Please note: This code is in the "well it ran at least once without falling over" category :)
So I leave it to you to give it a thorough testing.

Regards,
 
Upvote 0
First of all let me apologize for not being clear on Base58Check v. Base58.

I have tested your macro and it faithfully reproduces Base58 strings. The macro is "spot on."

However, the necessary product for Bitcoin is Base58Check string, which is b58 + a validity checker This Link under "features of Base58Check" has the list.

You can just throw in a random hex number on brainwallet converter and togle between the two to see the difference.

As far as step 9 is concerned, it looks like this step actually uses B58 and not B58Check so your function works for this step!
 
Upvote 0
Hi.

OK, if you are happy then so am I :)

Thanks for expanding on the difference between Base58Check and Base58.
 
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top