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:
Roman Khimov 2019-12-25 15:05:54 +03:00
parent 5dd8d29534
commit 369ac01a27
7 changed files with 30 additions and 23 deletions

View file

@ -1,6 +1,7 @@
package crypto
import (
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
"github.com/CityOfZion/neo-go/pkg/util"
)
@ -9,13 +10,13 @@ import (
func AddressFromUint160(u util.Uint160) string {
// Dont forget to prepend the Address version 0x17 (23) A
b := append([]byte{0x17}, u.BytesBE()...)
return Base58CheckEncode(b)
return base58.CheckEncode(b)
}
// Uint160DecodeAddress attempts to decode the given NEO address string
// into an Uint160.
func Uint160DecodeAddress(s string) (u util.Uint160, err error) {
b, err := Base58CheckDecode(s)
b, err := base58.CheckDecode(s)
if err != nil {
return u, err
}

View file

@ -5,8 +5,8 @@ import (
"errors"
"fmt"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
"golang.org/x/crypto/scrypt"
"golang.org/x/text/unicode/norm"
)
@ -72,13 +72,13 @@ func NEP2Encrypt(priv *PrivateKey, passphrase string) (s string, err error) {
return s, fmt.Errorf("invalid buffer length: expecting 39 bytes got %d", buf.Len())
}
return crypto.Base58CheckEncode(buf.Bytes()), nil
return base58.CheckEncode(buf.Bytes()), nil
}
// NEP2Decrypt decrypts an encrypted key using a given passphrase
// under the NEP-2 standard.
func NEP2Decrypt(key, passphrase string) (s string, err error) {
b, err := crypto.Base58CheckDecode(key)
b, err := base58.CheckDecode(key)
if err != nil {
return s, nil
}

View file

@ -9,8 +9,8 @@ import (
"fmt"
"math/big"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
"github.com/pkg/errors"
@ -246,7 +246,7 @@ func (p *PublicKey) Address() string {
b = append([]byte{0x17}, b...)
return crypto.Base58CheckEncode(b)
return base58.CheckEncode(b)
}
// Verify returns true if the signature is valid and corresponds

View file

@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
)
const (
@ -43,13 +43,13 @@ func WIFEncode(key []byte, version byte, compressed bool) (s string, err error)
buf.WriteByte(0x01)
}
s = crypto.Base58CheckEncode(buf.Bytes())
s = base58.CheckEncode(buf.Bytes())
return
}
// WIFDecode decodes the given WIF string into a WIF struct.
func WIFDecode(wif string, version byte) (*WIF, error) {
b, err := crypto.Base58CheckDecode(wif)
b, err := base58.CheckDecode(wif)
if err != nil {
return nil, err
}

View file

@ -1,4 +1,4 @@
package crypto
package base58
import (
"bytes"
@ -8,8 +8,9 @@ import (
"github.com/pkg/errors"
)
// Base58CheckDecode decodes the given string.
func Base58CheckDecode(s string) (b []byte, err error) {
// 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
@ -36,8 +37,9 @@ func Base58CheckDecode(s string) (b []byte, err error) {
return b, nil
}
// Base58CheckEncode encodes b into a base-58 check encoded string.
func Base58CheckEncode(b []byte) string {
// 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)

View file

@ -1,4 +1,4 @@
package crypto
package base58
import (
"encoding/hex"
@ -7,26 +7,26 @@ import (
"github.com/stretchr/testify/assert"
)
func TestBase58CheckEncodeDecode(t *testing.T) {
func TestCheckEncodeDecode(t *testing.T) {
var b58CsumEncoded = "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o"
var b58CsumDecodedHex = "802bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e01"
b58CsumDecoded, _ := hex.DecodeString(b58CsumDecodedHex)
encoded := Base58CheckEncode(b58CsumDecoded)
decoded, err := Base58CheckDecode(b58CsumEncoded)
encoded := CheckEncode(b58CsumDecoded)
decoded, err := CheckDecode(b58CsumEncoded)
assert.Nil(t, err)
assert.Equal(t, encoded, b58CsumEncoded)
assert.Equal(t, decoded, b58CsumDecoded)
}
func TestBase58CheckDecodeFailures(t *testing.T) {
func TestCheckDecodeFailures(t *testing.T) {
badbase58 := "BASE%*"
_, err := Base58CheckDecode(badbase58)
_, err := CheckDecode(badbase58)
assert.NotNil(t, err)
shortbase58 := "THqY"
_, err = Base58CheckDecode(shortbase58)
_, err = CheckDecode(shortbase58)
assert.NotNil(t, err)
badcsum := "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9A"
_, err = Base58CheckDecode(badcsum)
_, err = CheckDecode(badcsum)
assert.NotNil(t, err)
}

View file

@ -0,0 +1,4 @@
/*
Package base58 wraps generic base58 encoder with NEO-specific checksumming.
*/
package base58