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

View file

@ -5,8 +5,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/encoding/base58"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
"golang.org/x/text/unicode/norm" "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 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 // NEP2Decrypt decrypts an encrypted key using a given passphrase
// under the NEP-2 standard. // under the NEP-2 standard.
func NEP2Decrypt(key, passphrase string) (s string, err error) { func NEP2Decrypt(key, passphrase string) (s string, err error) {
b, err := crypto.Base58CheckDecode(key) b, err := base58.CheckDecode(key)
if err != nil { if err != nil {
return s, nil return s, nil
} }

View file

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

View file

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

View file

@ -1,4 +1,4 @@
package crypto package base58
import ( import (
"bytes" "bytes"
@ -8,8 +8,9 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Base58CheckDecode decodes the given string. // CheckDecode implements a base58-encoded string decoding with hash-based
func Base58CheckDecode(s string) (b []byte, err error) { // checksum check.
func CheckDecode(s string) (b []byte, err error) {
b, err = base58.Decode(s) b, err = base58.Decode(s)
if err != nil { if err != nil {
return nil, err return nil, err
@ -36,8 +37,9 @@ func Base58CheckDecode(s string) (b []byte, err error) {
return b, nil return b, nil
} }
// Base58CheckEncode encodes b into a base-58 check encoded string. // CheckEncode encodes given byte slice into a base58 string with hash-based
func Base58CheckEncode(b []byte) string { // checksum appended to it.
func CheckEncode(b []byte) string {
b = append(b, hash.Checksum(b)...) b = append(b, hash.Checksum(b)...)
return base58.Encode(b) return base58.Encode(b)

View file

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

View file

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