Simplify error message from Decode()

This commit is contained in:
2016-07-02 22:57:16 +01:00
parent 997a6e9e34
commit 4813c34f36
2 changed files with 4 additions and 10 deletions

View File

@@ -2,7 +2,6 @@ package base58
import (
"errors"
"fmt"
"strings"
)
@@ -10,6 +9,8 @@ import (
const Alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
const base = len(Alphabet)
var errInvalidBase58 = errors.New("invalid base58")
// Encode converts a base10 integer to a base58 string using the default
// alphabet.
func Encode(num int) string {
@@ -33,7 +34,7 @@ func Decode(str string) (int, error) {
char := string(str[i-1])
index := strings.Index(Alphabet, char)
if index == -1 {
return -1, decodeError(str)
return -1, errInvalidBase58
}
num += multi * index
multi = multi * base
@@ -41,8 +42,3 @@ func Decode(str string) (int, error) {
return num, nil
}
func decodeError(str string) error {
msg := fmt.Sprintf("\"%s\" is not a valid base58 string.", str)
return errors.New(msg)
}

View File

@@ -1,7 +1,6 @@
package base58
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
@@ -206,12 +205,11 @@ func (s *Base58Suite) TestDecode() {
func (s *Base58Suite) TestDecodeError() {
assert := assert.New(s.T())
errMsg := "\"invalid@base58.string\" is not a valid base58 string."
result, err := Decode("invalid@base58.string")
assert.Equal(-1, result)
assert.Equal(errors.New(errMsg), err)
assert.Equal("invalid base58", err.Error())
}
func TestBase58Suite(t *testing.T) {