base58: move into its own package
It doesn't belong to crypto in any way other than it uses hash function internally.
This commit is contained in:
parent
5dd8d29534
commit
369ac01a27
7 changed files with 30 additions and 23 deletions
46
pkg/encoding/base58/base58.go
Normal file
46
pkg/encoding/base58/base58.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package base58
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] != '1' {
|
||||
break
|
||||
}
|
||||
b = append([]byte{0x00}, b...)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
32
pkg/encoding/base58/base58_test.go
Normal file
32
pkg/encoding/base58/base58_test.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package base58
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckEncodeDecode(t *testing.T) {
|
||||
var b58CsumEncoded = "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o"
|
||||
var b58CsumDecodedHex = "802bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e01"
|
||||
|
||||
b58CsumDecoded, _ := hex.DecodeString(b58CsumDecodedHex)
|
||||
encoded := CheckEncode(b58CsumDecoded)
|
||||
decoded, err := CheckDecode(b58CsumEncoded)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, encoded, b58CsumEncoded)
|
||||
assert.Equal(t, decoded, b58CsumDecoded)
|
||||
}
|
||||
|
||||
func TestCheckDecodeFailures(t *testing.T) {
|
||||
badbase58 := "BASE%*"
|
||||
_, err := CheckDecode(badbase58)
|
||||
assert.NotNil(t, err)
|
||||
shortbase58 := "THqY"
|
||||
_, err = CheckDecode(shortbase58)
|
||||
assert.NotNil(t, err)
|
||||
badcsum := "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9A"
|
||||
_, err = CheckDecode(badcsum)
|
||||
assert.NotNil(t, err)
|
||||
}
|
4
pkg/encoding/base58/doc.go
Normal file
4
pkg/encoding/base58/doc.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
/*
|
||||
Package base58 wraps generic base58 encoder with NEO-specific checksumming.
|
||||
*/
|
||||
package base58
|
Loading…
Add table
Add a link
Reference in a new issue