[#283] pkg/owner: Implement Equal method of ID type

Implement `owner.ID.Equal` method which defines a comparison relationship
between two identifiers.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-28 10:25:30 +03:00 committed by Leonard Lyubich
parent 4f286f5175
commit 5fe5af5bf9
2 changed files with 32 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package owner
import (
"bytes"
"errors"
"fmt"
@ -40,6 +41,16 @@ func (id *ID) String() string {
return base58.Encode((*refs.OwnerID)(id).GetValue())
}
// Equal defines a comparison relation on ID's.
//
// ID's are equal if they have the same binary representation.
func (id *ID) Equal(id2 *ID) bool {
return bytes.Equal(
(*refs.ObjectID)(id).GetValue(),
(*refs.ObjectID)(id2).GetValue(),
)
}
func ScriptHashBE(id fmt.Stringer) ([]byte, error) {
addr, err := address.StringToUint160(id.String())
if err != nil {

View file

@ -1,4 +1,4 @@
package owner
package owner_test
import (
"crypto/rand"
@ -6,6 +6,8 @@ import (
"testing"
"github.com/mr-tron/base58"
. "github.com/nspcc-dev/neofs-api-go/pkg/owner"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require"
)
@ -93,3 +95,21 @@ func TestIDEncoding(t *testing.T) {
require.Equal(t, id, a2)
})
}
func TestID_Equal(t *testing.T) {
var (
data1 = []byte{1, 2, 3}
data2 = data1
data3 = append(data1, 255)
)
id1 := ownertest.GenerateFromBytes(data1)
require.True(t, id1.Equal(
ownertest.GenerateFromBytes(data2),
))
require.False(t, id1.Equal(
ownertest.GenerateFromBytes(data3),
))
}