diff --git a/pkg/crypto/address.go b/pkg/crypto/address.go index 3fb69017d..a289f7275 100644 --- a/pkg/crypto/address.go +++ b/pkg/crypto/address.go @@ -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 } diff --git a/pkg/crypto/keys/nep2.go b/pkg/crypto/keys/nep2.go index 5d8144439..abe478490 100644 --- a/pkg/crypto/keys/nep2.go +++ b/pkg/crypto/keys/nep2.go @@ -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 } diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index e166d8031..64cbb3f54 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -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 diff --git a/pkg/crypto/keys/wif.go b/pkg/crypto/keys/wif.go index 3e30fc47a..e23fbbd3c 100644 --- a/pkg/crypto/keys/wif.go +++ b/pkg/crypto/keys/wif.go @@ -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 } diff --git a/pkg/crypto/base58.go b/pkg/encoding/base58/base58.go similarity index 68% rename from pkg/crypto/base58.go rename to pkg/encoding/base58/base58.go index 9db748022..ccac93aca 100644 --- a/pkg/crypto/base58.go +++ b/pkg/encoding/base58/base58.go @@ -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) diff --git a/pkg/crypto/base58_test.go b/pkg/encoding/base58/base58_test.go similarity index 64% rename from pkg/crypto/base58_test.go rename to pkg/encoding/base58/base58_test.go index 5c1388754..e525265ae 100644 --- a/pkg/crypto/base58_test.go +++ b/pkg/encoding/base58/base58_test.go @@ -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) } diff --git a/pkg/encoding/base58/doc.go b/pkg/encoding/base58/doc.go new file mode 100644 index 000000000..fcac3bba2 --- /dev/null +++ b/pkg/encoding/base58/doc.go @@ -0,0 +1,4 @@ +/* +Package base58 wraps generic base58 encoder with NEO-specific checksumming. +*/ +package base58