From 64e23a8d0ced5a34de8f4b9a69c360777e73266d Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 29 Jun 2016 01:27:37 +0100 Subject: [PATCH] Simplify exposed method API This package serves a single purpose, convert between integers and base58 strings the same as a Flickr does. It has no need for supporting custom alphabets passed in a runtime. --- base58.go | 50 +++++++++++++------------------------------------- base58_test.go | 35 ----------------------------------- 2 files changed, 13 insertions(+), 72 deletions(-) diff --git a/base58.go b/base58.go index 3d9f4f2..dd1c181 100644 --- a/base58.go +++ b/base58.go @@ -8,46 +8,32 @@ import ( // Alphabet is the default alphabet. const Alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ" +const base = len(Alphabet) // Encode converts a base10 integer to a base58 string using the default // alphabet. func Encode(num int) string { - return EncodeWithAlphabet(num, Alphabet) + str := "" + for num >= base { + mod := num % base + str = string(Alphabet[mod]) + str + num = (num - mod) / base + } + + return string(Alphabet[num]) + str } // Decode converts a base58 string to a base10 integer using the default // alphabet. func Decode(str string) (int, error) { - return DecodeWithAlphabet(str, Alphabet) -} - -// EncodeWithAlphabet converts a base10 integer to a base58 string with the -// given alphabet. -func EncodeWithAlphabet(num int, alphabet string) string { - base := len(alphabet) - - str := "" - for num >= base { - mod := num % base - str = string(alphabet[mod]) + str - num = (num - mod) / base - } - - return string(alphabet[num]) + str -} - -// DecodeWithAlphabet converts a base58 string to a base10 integer with the -// given alphabet. -func DecodeWithAlphabet(str string, alphabet string) (int, error) { - base := len(alphabet) num := 0 multi := 1 for i := len(str); i > 0; i-- { char := string(str[i-1]) - index := strings.Index(alphabet, char) + index := strings.Index(Alphabet, char) if index == -1 { - return -1, decodeError(str, alphabet) + return -1, decodeError(str) } num += multi * index multi = multi * base @@ -56,17 +42,7 @@ func DecodeWithAlphabet(str string, alphabet string) (int, error) { return num, nil } -func decodeError(str string, alphabet string) error { - var msg string - - if alphabet == Alphabet { - msg = fmt.Sprintf("\"%s\" is not a valid base58 string.", str) - } else { - msg = fmt.Sprintf( - "\"%s\" is not a valid input for alphabet \"%s\".", - str, alphabet, - ) - } - +func decodeError(str string) error { + msg := fmt.Sprintf("\"%s\" is not a valid base58 string.", str) return errors.New(msg) } diff --git a/base58_test.go b/base58_test.go index 72f89b1..0e53eaf 100644 --- a/base58_test.go +++ b/base58_test.go @@ -177,13 +177,6 @@ var examples = map[string]int{ "6hJPEq": 3471202816, "6hGMH7": 3470806020, "6hGp5L": 3470729904, } -var myAlphabet = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789" -var myExamples = map[string]int{ - "frMj9u": 3469905409, "fr3bSF": 3472804260, "frYrT8": 3472074356, - "frUXLH": 3471394398, "frTYPz": 3471202816, "frRWSg": 3470806020, - "frRyeV": 3470729904, "frQp15": 3470507135, "frP2SC": 3470432637, -} - type Base58Suite struct { suite.Suite } @@ -225,34 +218,6 @@ func (s *Base58Suite) TestDecodeError() { assert.Equal(errors.New(errMsg), err) } -func (s *Base58Suite) TestEncodeWithAlphabet() { - assert := assert.New(s.T()) - - for str, num := range myExamples { - assert.Equal(str, EncodeWithAlphabet(num, myAlphabet)) - } -} - -func (s *Base58Suite) TestDecodeWithAlphabet() { - assert := assert.New(s.T()) - - for str, num := range myExamples { - result, _ := DecodeWithAlphabet(str, myAlphabet) - assert.Equal(num, result) - } -} - -func (s *Base58Suite) TestDecodeWithAlphabetError() { - assert := assert.New(s.T()) - errMsg := "\"AaBbCc\" is not a valid input for alphabet " + - "\"abcdefghjklmnpqrstuvwxyz\"." - - result, err := DecodeWithAlphabet("AaBbCc", "abcdefghjklmnpqrstuvwxyz") - - assert.Equal(-1, result) - assert.Equal(errors.New(errMsg), err) -} - func TestBase58Suite(t *testing.T) { suite.Run(t, new(Base58Suite)) }