2020-08-31 07:46:10 +00:00
|
|
|
package owner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-31 07:46:10 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ID represents owner identifier that
|
|
|
|
// supports different type of values.
|
|
|
|
type ID struct {
|
|
|
|
val []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNeo3Wallet sets owner identifier value to NEO3 wallet address.
|
2020-08-31 08:15:46 +00:00
|
|
|
func (id *ID) SetNeo3Wallet(v *NEO3Wallet) {
|
2020-08-31 07:46:10 +00:00
|
|
|
if id != nil {
|
|
|
|
id.val = v.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 returns the v2 owner ID message.
|
2020-08-31 08:15:46 +00:00
|
|
|
func (id *ID) ToV2() *refs.OwnerID {
|
2020-08-31 07:46:10 +00:00
|
|
|
if id != nil {
|
2020-08-31 08:15:46 +00:00
|
|
|
idV2 := new(refs.OwnerID)
|
2020-08-31 07:46:10 +00:00
|
|
|
idV2.SetValue(id.val)
|
|
|
|
|
|
|
|
return idV2
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
// IDFromV2 converts owner ID v2 structure to ID.
|
|
|
|
func IDFromV2(idV2 *refs.OwnerID) (*ID, error) {
|
2020-08-31 07:46:10 +00:00
|
|
|
if idV2 == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
val := idV2.GetValue()
|
|
|
|
if ln := len(val); ln != 25 {
|
|
|
|
return nil, errors.Errorf(
|
|
|
|
"could not convert %T to %T: expected length %d, received %d",
|
|
|
|
idV2, (*ID)(nil), sha256.Size, ln,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ID{
|
|
|
|
val: val,
|
|
|
|
}, nil
|
|
|
|
}
|