2019-12-25 12:05:54 +00:00
|
|
|
package base58
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2019-01-25 11:20:35 +00:00
|
|
|
|
2019-09-09 10:09:46 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckDecode implements base58-encoded string decoding with a hash-based
|
2019-12-25 12:05:54 +00:00
|
|
|
// checksum check.
|
|
|
|
func CheckDecode(s string) (b []byte, err error) {
|
2019-09-09 10:09:46 +00:00
|
|
|
b, err = base58.Decode(s)
|
2018-03-02 15:24:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) < 5 {
|
2019-09-03 15:04:30 +00:00
|
|
|
return nil, errors.New("invalid base-58 check string: missing checksum")
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:50:45 +00:00
|
|
|
if !bytes.Equal(hash.Checksum(b[:len(b)-4]), b[len(b)-4:]) {
|
2019-09-03 15:04:30 +00:00
|
|
|
return nil, errors.New("invalid base-58 check string: invalid checksum")
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the 4 byte long hash.
|
|
|
|
b = b[:len(b)-4]
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CheckEncode encodes the given byte slice into a base58 string with a hash-based
|
2019-12-25 12:05:54 +00:00
|
|
|
// checksum appended to it.
|
|
|
|
func CheckEncode(b []byte) string {
|
2019-08-23 15:50:45 +00:00
|
|
|
b = append(b, hash.Checksum(b)...)
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2019-09-09 10:09:46 +00:00
|
|
|
return base58.Encode(b)
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|