From 1611ede58c62f4ab634e1b7743a46e7bd0f988ed Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 7 Apr 2020 10:55:56 +0300 Subject: [PATCH] crypto/keys: implement NewPublicKeyFromBytes() It is convenient to have a single function instead of allocating new `PublicKey` and using `DecodeBytes()` on it. --- pkg/core/interop_neo.go | 3 +-- pkg/core/interop_system.go | 3 +-- pkg/crypto/keys/publickey.go | 11 ++++++----- pkg/crypto/keys/publickey_test.go | 10 ++++++++++ pkg/vm/vm.go | 4 ++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pkg/core/interop_neo.go b/pkg/core/interop_neo.go index 378214447..5a6990f87 100644 --- a/pkg/core/interop_neo.go +++ b/pkg/core/interop_neo.go @@ -628,8 +628,7 @@ func assetCreate(ic *interop.Context, v *vm.VM) error { if amount != -util.Satoshi() && (int64(amount)%int64(math.Pow10(int(MaxAssetPrecision-precision))) != 0) { return errors.New("given asset amount has fractional component") } - owner := &keys.PublicKey{} - err := owner.DecodeBytes(v.Estack().Pop().Bytes()) + owner, err := keys.NewPublicKeyFromBytes(v.Estack().Pop().Bytes()) if err != nil { return gherr.Wrap(err, "failed to get owner key") } diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index bb6816f97..e19373b9f 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -324,8 +324,7 @@ func runtimeCheckWitness(ic *interop.Context, v *vm.VM) error { hashOrKey := v.Estack().Pop().Bytes() hash, err := util.Uint160DecodeBytesBE(hashOrKey) if err != nil { - key := &keys.PublicKey{} - err = key.DecodeBytes(hashOrKey) + key, err := keys.NewPublicKeyFromBytes(hashOrKey) if err != nil { return errors.New("parameter given is neither a key nor a hash") } diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index 9eaf4688a..358433837 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -83,14 +83,15 @@ func NewPublicKeyFromString(s string) (*PublicKey, error) { if err != nil { return nil, err } + return NewPublicKeyFromBytes(b) +} +// NewPublicKeyFromBytes returns public key created from b. +func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) { pubKey := new(PublicKey) - r := io.NewBinReaderFromBuf(b) - pubKey.DecodeBinary(r) - if r.Err != nil { - return nil, r.Err + if err := pubKey.DecodeBytes(b); err != nil { + return nil, err } - return pubKey, nil } diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index fda5c3b18..1415197a0 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -37,6 +37,16 @@ func TestEncodeDecodePublicKey(t *testing.T) { } } +func TestNewPublicKeyFromBytes(t *testing.T) { + priv, err := NewPrivateKey() + require.NoError(t, err) + + b := priv.PublicKey().Bytes() + pub, err := NewPublicKeyFromBytes(b) + require.NoError(t, err) + require.Equal(t, priv.PublicKey(), pub) +} + func TestDecodeFromString(t *testing.T) { str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c" pubKey, err := NewPublicKeyFromString(str) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 788de6c1f..9766fef15 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -1595,8 +1595,8 @@ func (v *VM) bytesToPublicKey(b []byte) *keys.PublicKey { if v.keys[s] != nil { pkey = v.keys[s] } else { - pkey = &keys.PublicKey{} - err := pkey.DecodeBytes(b) + var err error + pkey, err = keys.NewPublicKeyFromBytes(b) if err != nil { panic(err.Error()) }