2022-04-11 06:30:22 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ID identifies users of the FrostFS system.
|
2022-04-11 06:30:22 +00:00
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// ID is mutually compatible with github.com/TrueCloudLab/frostfs-api-go/v2/refs.OwnerID
|
2022-04-11 06:30:22 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration. Zero ID is not valid,
|
|
|
|
// so it MUST be initialized using some modifying function (e.g. SetScriptHash,
|
|
|
|
// IDFromKey, etc.).
|
|
|
|
type ID struct {
|
|
|
|
w []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromV2 reads ID from the refs.OwnerID message. Returns an error if
|
2022-12-29 10:46:18 +00:00
|
|
|
// the message is malformed according to the FrostFS API V2 protocol.
|
2022-04-11 06:30:22 +00:00
|
|
|
//
|
|
|
|
// 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 w[0] != address.NEO3Prefix {
|
|
|
|
return fmt.Errorf("invalid prefix byte 0x%X, expected 0x%X", w[0], address.NEO3Prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(w[21:], hash.Checksum(w[:21])) {
|
|
|
|
return errors.New("checksum mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
x.w = w
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToV2 writes ID to the refs.OwnerID message.
|
|
|
|
// The message must not be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x ID) WriteToV2(m *refs.OwnerID) {
|
|
|
|
m.SetValue(x.w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
|
|
|
x.w[0] = address.Prefix
|
|
|
|
copy(x.w[1:], scriptHash.BytesBE())
|
|
|
|
copy(x.w[21:], hash.Checksum(x.w[:21]))
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// WalletBytes returns FrostFS user ID as Neo3 wallet address in a binary format.
|
2022-04-11 06:30:22 +00:00
|
|
|
//
|
|
|
|
// Return value MUST NOT be mutated: to do this, first make a copy.
|
|
|
|
//
|
|
|
|
// See also Neo3 wallet docs.
|
|
|
|
func (x ID) WalletBytes() []byte {
|
|
|
|
return x.w
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// EncodeToString encodes ID into FrostFS API V2 protocol string.
|
2022-04-11 06:30:22 +00:00
|
|
|
//
|
|
|
|
// See also DecodeString.
|
|
|
|
func (x ID) EncodeToString() string {
|
|
|
|
return base58.Encode(x.w)
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// DecodeString decodes FrostFS API V2 protocol string. Returns an error
|
2022-04-11 06:30:22 +00:00
|
|
|
// if s is malformed.
|
|
|
|
//
|
|
|
|
// DecodeString always changes the ID.
|
|
|
|
//
|
|
|
|
// See also EncodeToString.
|
|
|
|
func (x *ID) DecodeString(s string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
x.w, err = base58.Decode(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode base58: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
//
|
|
|
|
// String is designed to be human-readable, and its format MAY differ between
|
|
|
|
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
|
2022-12-29 10:46:18 +00:00
|
|
|
// be used to encode ID into FrostFS protocol string.
|
2022-04-11 06:30:22 +00:00
|
|
|
func (x ID) String() string {
|
|
|
|
return x.EncodeToString()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals defines a comparison relation between two ID instances.
|
|
|
|
func (x ID) Equals(x2 ID) bool {
|
|
|
|
return bytes.Equal(x.w, x2.w)
|
|
|
|
}
|