user: Add exported error for user extracting from key

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
Evgenii Baidakov 2023-05-17 12:56:31 +04:00
parent cb4acec6a2
commit 8094342b1c
No known key found for this signature in database
GPG key ID: 8733EE3D72CDB4DE
2 changed files with 8 additions and 1 deletions

View file

@ -1,17 +1,20 @@
package user package user
import ( import (
"errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
) )
var ErrOwnerExtract = errors.New("decode owner failed")
// IDFromKey forms the ID using script hash calculated for the given key. // IDFromKey forms the ID using script hash calculated for the given key.
func IDFromKey(id *ID, key []byte) error { func IDFromKey(id *ID, key []byte) error {
var pk keys.PublicKey var pk keys.PublicKey
if err := pk.DecodeBytes(key); err != nil { if err := pk.DecodeBytes(key); err != nil {
return fmt.Errorf("IDFromKey %v", err) return fmt.Errorf("%w: %v", ErrOwnerExtract, err)
} }
id.SetScriptHash(pk.GetScriptHash()) id.SetScriptHash(pk.GetScriptHash())

View file

@ -20,4 +20,8 @@ func TestIDFromKey(t *testing.T) {
require.NoError(t, user.IDFromKey(&id, rawPub)) require.NoError(t, user.IDFromKey(&id, rawPub))
require.Equal(t, "NNLi44dJNXtDNSBkofB48aTVYtb1zZrNEs", id.EncodeToString()) require.Equal(t, "NNLi44dJNXtDNSBkofB48aTVYtb1zZrNEs", id.EncodeToString())
err := user.IDFromKey(&id, []byte{1})
require.Error(t, err)
require.ErrorIs(t, err, user.ErrOwnerExtract)
} }