forked from TrueCloudLab/neoneo-go
5ef08f60ae
It's not needed any more with Go 1.13 as we have wrapping/unwrapping in base packages. All errors.Wrap calls are replaced with fmt.Errorf, some strings are improved along the way.
39 lines
846 B
Go
39 lines
846 B
Go
package base58
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
|
|
"github.com/mr-tron/base58"
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
)
|
|
|
|
// CheckDecode implements a base58-encoded string decoding with hash-based
|
|
// checksum check.
|
|
func CheckDecode(s string) (b []byte, err error) {
|
|
b, err = base58.Decode(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(b) < 5 {
|
|
return nil, errors.New("invalid base-58 check string: missing checksum")
|
|
}
|
|
|
|
if !bytes.Equal(hash.Checksum(b[:len(b)-4]), b[len(b)-4:]) {
|
|
return nil, errors.New("invalid base-58 check string: invalid checksum")
|
|
}
|
|
|
|
// Strip the 4 byte long hash.
|
|
b = b[:len(b)-4]
|
|
|
|
return b, nil
|
|
}
|
|
|
|
// CheckEncode encodes given byte slice into a base58 string with hash-based
|
|
// checksum appended to it.
|
|
func CheckEncode(b []byte) string {
|
|
b = append(b, hash.Checksum(b)...)
|
|
|
|
return base58.Encode(b)
|
|
}
|