forked from TrueCloudLab/frostfs-sdk-go
user: Add exported error for user extracting from key
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
parent
cb4acec6a2
commit
8094342b1c
2 changed files with 8 additions and 1 deletions
|
@ -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())
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue