crypto/keys: implement NewPublicKeyFromBytes()
It is convenient to have a single function instead of allocating new `PublicKey` and using `DecodeBytes()` on it.
This commit is contained in:
parent
405fa9c411
commit
1611ede58c
5 changed files with 20 additions and 11 deletions
|
@ -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) {
|
if amount != -util.Satoshi() && (int64(amount)%int64(math.Pow10(int(MaxAssetPrecision-precision))) != 0) {
|
||||||
return errors.New("given asset amount has fractional component")
|
return errors.New("given asset amount has fractional component")
|
||||||
}
|
}
|
||||||
owner := &keys.PublicKey{}
|
owner, err := keys.NewPublicKeyFromBytes(v.Estack().Pop().Bytes())
|
||||||
err := owner.DecodeBytes(v.Estack().Pop().Bytes())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gherr.Wrap(err, "failed to get owner key")
|
return gherr.Wrap(err, "failed to get owner key")
|
||||||
}
|
}
|
||||||
|
|
|
@ -324,8 +324,7 @@ func runtimeCheckWitness(ic *interop.Context, v *vm.VM) error {
|
||||||
hashOrKey := v.Estack().Pop().Bytes()
|
hashOrKey := v.Estack().Pop().Bytes()
|
||||||
hash, err := util.Uint160DecodeBytesBE(hashOrKey)
|
hash, err := util.Uint160DecodeBytesBE(hashOrKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
key := &keys.PublicKey{}
|
key, err := keys.NewPublicKeyFromBytes(hashOrKey)
|
||||||
err = key.DecodeBytes(hashOrKey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("parameter given is neither a key nor a hash")
|
return errors.New("parameter given is neither a key nor a hash")
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,14 +83,15 @@ func NewPublicKeyFromString(s string) (*PublicKey, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return NewPublicKeyFromBytes(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPublicKeyFromBytes returns public key created from b.
|
||||||
|
func NewPublicKeyFromBytes(b []byte) (*PublicKey, error) {
|
||||||
pubKey := new(PublicKey)
|
pubKey := new(PublicKey)
|
||||||
r := io.NewBinReaderFromBuf(b)
|
if err := pubKey.DecodeBytes(b); err != nil {
|
||||||
pubKey.DecodeBinary(r)
|
return nil, err
|
||||||
if r.Err != nil {
|
|
||||||
return nil, r.Err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pubKey, nil
|
return pubKey, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
func TestDecodeFromString(t *testing.T) {
|
||||||
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
str := "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"
|
||||||
pubKey, err := NewPublicKeyFromString(str)
|
pubKey, err := NewPublicKeyFromString(str)
|
||||||
|
|
|
@ -1595,8 +1595,8 @@ func (v *VM) bytesToPublicKey(b []byte) *keys.PublicKey {
|
||||||
if v.keys[s] != nil {
|
if v.keys[s] != nil {
|
||||||
pkey = v.keys[s]
|
pkey = v.keys[s]
|
||||||
} else {
|
} else {
|
||||||
pkey = &keys.PublicKey{}
|
var err error
|
||||||
err := pkey.DecodeBytes(b)
|
pkey, err = keys.NewPublicKeyFromBytes(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue