mirror of
https://github.com/jimeh/go-base58.git
synced 2026-02-19 08:06:39 +00:00
Simplify error message from Decode()
This commit is contained in:
10
base58.go
10
base58.go
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user