rm-hull / base58

Base58 and check algorithm that is compatible with what is used by the bitcoin network, in Clojure.
https://www.destructuring-bind.org/base58
MIT License
0 stars 1 forks source link

Some wrong decodings / decode returns string #1

Open dmichulke opened 7 years ago

dmichulke commented 7 years ago

Hi, I have been comparing your lib with that of bitcoinj and it turns out your lib works sometimes correctly but sometimes not. One of the ambiguities is IMO due to decode not returning a byte array, so one has to call getBytes on the resulting string. But there's another problem

(:require [base58.core :as base58]) ;; lower case is yours
(:import org.bitcoinj.core.Base58)  ;; uppercase is bitcoinj

Your Code without getBytes

(map int (base58/decode "6sKMMHVLyCQN7Juih2e9tbSmeE5Hu7L8XtBRgowJQvU7" ))
=>; (87 46 57 118 156 166 215 62 28 104 114 216 235 228 5 58 67 174 187 51 113 170 125 81 93 32 153 239 230 108 231 0)

Your Code with getBytes

(map int (.getBytes (base58/decode "6sKMMHVLyCQN7Juih2e9tbSmeE5Hu7L8XtBRgowJQvU7" )))
=>; (87 46 57 118 -62 -100 -62 -90 -61 -105 62 28 104 114 -61 -104 -61 -85 -61 -92 5 58 67 -62 -82 -62 -69 51 113 -62 -86 125 81 93 32 -62 -103 -61 -81 -61 -90 108 -61 -89 0)

Bitcoinj:

(map int (Base58/decode "6sKMMHVLyCQN7Juih2e9tbSmeE5Hu7L8XtBRgowJQvU7" ))
=>; (87 46 57 118 -100 -90 -41 62 28 104 114 -40 -21 -28 5 58 67 -82 -69 51 113 -86 125 81 93 32 -103 -17 -26 108 -25 0)

The differences occur in the 5th character

rm-hull commented 7 years ago

This sounds like a string encoding to byte array issue rather than a bug in the library. If I call getBytes on the decoded string with the LATIN charset, it yields the same numeric sequence as that returned from bitcoinj:

(map int 
  (.getBytes 
    (base58/decode "6sKMMHVLyCQN7Juih2e9tbSmeE5Hu7L8XtBRgowJQvU7")
    (java.nio.charset.Charset/forName "ISO-8859-1")))
;=> (87 46 57 118 -100 -90 -41 62 28 104 114 -40 -21 -28 5 58 67 -82 -69 51 113 -86 125 81 93 32 -103 -17 -26 108 -25 0) 

I think you might be right though about the ambiguity of not returning it as a byte array