2020-08-31 07:46:10 +00:00
|
|
|
package owner
|
|
|
|
|
|
|
|
import (
|
2021-05-17 12:56:17 +00:00
|
|
|
"errors"
|
2020-11-16 14:55:48 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-01 14:28:40 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
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
|
|
|
)
|
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// ID represents v2-compatible owner identifier.
|
|
|
|
type ID refs.OwnerID
|
|
|
|
|
2020-11-16 13:33:41 +00:00
|
|
|
var errInvalidIDString = errors.New("incorrect format of the string owner ID")
|
2020-10-14 19:10:34 +00:00
|
|
|
|
2020-09-10 09:57:29 +00:00
|
|
|
// NewIDFromV2 wraps v2 OwnerID message to ID.
|
|
|
|
func NewIDFromV2(idV2 *refs.OwnerID) *ID {
|
|
|
|
return (*ID)(idV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewID creates and initializes blank ID.
|
|
|
|
//
|
|
|
|
// Works similar as NewIDFromV2(new(OwnerID)).
|
|
|
|
func NewID() *ID {
|
|
|
|
return NewIDFromV2(new(refs.OwnerID))
|
2020-08-31 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetNeo3Wallet sets owner identifier value to NEO3 wallet address.
|
2020-08-31 08:15:46 +00:00
|
|
|
func (id *ID) SetNeo3Wallet(v *NEO3Wallet) {
|
2020-09-10 09:57:29 +00:00
|
|
|
(*refs.OwnerID)(id).SetValue(v.Bytes())
|
2020-08-31 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 returns the v2 owner ID message.
|
2020-08-31 08:15:46 +00:00
|
|
|
func (id *ID) ToV2() *refs.OwnerID {
|
2020-09-10 09:57:29 +00:00
|
|
|
return (*refs.OwnerID)(id)
|
2020-08-31 07:46:10 +00:00
|
|
|
}
|
2020-09-01 14:28:40 +00:00
|
|
|
|
|
|
|
func (id *ID) String() string {
|
2020-09-10 09:57:29 +00:00
|
|
|
return base58.Encode((*refs.OwnerID)(id).GetValue())
|
2020-09-01 14:28:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 14:55:48 +00:00
|
|
|
func ScriptHashBE(id fmt.Stringer) ([]byte, error) {
|
2020-09-01 14:28:40 +00:00
|
|
|
addr, err := address.StringToUint160(id.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.BytesBE(), nil
|
|
|
|
}
|
2020-10-06 06:56:28 +00:00
|
|
|
|
|
|
|
// NewIDFromNeo3Wallet creates new owner identity from 25-byte neo wallet.
|
|
|
|
func NewIDFromNeo3Wallet(v *NEO3Wallet) *ID {
|
|
|
|
id := NewID()
|
|
|
|
id.SetNeo3Wallet(v)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
2020-10-14 19:10:34 +00:00
|
|
|
|
|
|
|
// Parse converts base58 string representation into ID.
|
|
|
|
func (id *ID) Parse(s string) error {
|
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not parse owner.ID from string: %w", err)
|
2020-10-14 19:10:34 +00:00
|
|
|
} else if len(data) != NEO3WalletSize {
|
2020-11-16 13:33:41 +00:00
|
|
|
return errInvalidIDString
|
2020-10-14 19:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*refs.OwnerID)(id).SetValue(data)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-13 13:14:00 +00:00
|
|
|
|
|
|
|
// Marshal marshals ID into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (id *ID) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*refs.OwnerID)(id).
|
|
|
|
StableMarshal(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of ID.
|
|
|
|
func (id *ID) Unmarshal(data []byte) error {
|
|
|
|
return (*refs.OwnerID)(id).
|
|
|
|
Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes ID to protobuf JSON format.
|
|
|
|
func (id *ID) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*refs.OwnerID)(id).
|
|
|
|
MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes ID from protobuf JSON format.
|
|
|
|
func (id *ID) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*refs.OwnerID)(id).
|
|
|
|
UnmarshalJSON(data)
|
|
|
|
}
|