[#190] sdk-go: Pass user.ID by value

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-11-21 11:35:10 +03:00
parent 1c07098740
commit 157a9930e8
17 changed files with 53 additions and 42 deletions

View file

@ -12,6 +12,10 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
)
const idSize = 25
var zeroSlice = bytes.Repeat([]byte{0}, idSize)
// ID identifies users of the FrostFS system.
//
// ID is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.OwnerID
@ -30,8 +34,8 @@ type ID struct {
// See also WriteToV2.
func (x *ID) ReadFromV2(m refs.OwnerID) error {
w := m.GetValue()
if len(w) != 25 {
return fmt.Errorf("invalid length %d, expected 25", len(w))
if len(w) != idSize {
return fmt.Errorf("invalid length %d, expected %d", len(w), idSize)
}
if w[0] != address.NEO3Prefix {
@ -57,10 +61,10 @@ func (x ID) WriteToV2(m *refs.OwnerID) {
// SetScriptHash forms user ID from wallet address scripthash.
func (x *ID) SetScriptHash(scriptHash util.Uint160) {
if cap(x.w) < 25 {
x.w = make([]byte, 25)
} else if len(x.w) < 25 {
x.w = x.w[:25]
if cap(x.w) < idSize {
x.w = make([]byte, idSize)
} else if len(x.w) < idSize {
x.w = x.w[:idSize]
}
x.w[0] = address.Prefix
@ -114,3 +118,8 @@ func (x ID) String() string {
func (x ID) Equals(x2 ID) bool {
return bytes.Equal(x.w, x2.w)
}
// IsEmpty returns True, if ID is empty value.
func (x ID) IsEmpty() bool {
return bytes.Equal(zeroSlice, x.w)
}