2022-01-25 16:20:32 +00:00
|
|
|
package oid
|
2021-11-08 10:04:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2022-04-05 11:13:34 +00:00
|
|
|
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
|
2021-11-08 10:04:45 +00:00
|
|
|
)
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// ID represents NeoFS object identifier in a container.
|
|
|
|
//
|
|
|
|
// ID is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/refs.ObjectID
|
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
//
|
|
|
|
// Note that direct typecast is not safe and may result in loss of compatibility:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// _ = ID([32]byte{}) // not recommended
|
2022-04-11 16:25:14 +00:00
|
|
|
type ID [sha256.Size]byte
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// ReadFromV2 reads ID from the refs.ObjectID message. Returns an error if
|
|
|
|
// the message is malformed according to the NeoFS API V2 protocol.
|
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
|
|
|
func (id *ID) ReadFromV2(m refs.ObjectID) error {
|
|
|
|
return id.Decode(m.GetValue())
|
|
|
|
}
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// WriteToV2 writes ID to the refs.ObjectID message.
|
|
|
|
// The message must not be nil.
|
2021-11-08 10:04:45 +00:00
|
|
|
//
|
2022-04-11 16:25:14 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (id ID) WriteToV2(m *refs.ObjectID) {
|
|
|
|
m.SetValue(id[:])
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// Encode encodes ID into 32 bytes of dst. Panics if
|
|
|
|
// dst length is less than 32.
|
2021-11-08 10:04:45 +00:00
|
|
|
//
|
2022-04-11 16:25:14 +00:00
|
|
|
// Zero ID is all zeros.
|
2021-11-08 10:04:45 +00:00
|
|
|
//
|
2022-04-11 16:25:14 +00:00
|
|
|
// See also Decode.
|
|
|
|
func (id ID) Encode(dst []byte) {
|
|
|
|
if l := len(dst); l < sha256.Size {
|
|
|
|
panic(fmt.Sprintf("destination length is less than %d bytes: %d", sha256.Size, l))
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(dst, id[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode decodes src bytes into ID.
|
|
|
|
//
|
|
|
|
// Decode expects that src has 32 bytes length. If the input is malformed,
|
|
|
|
// Decode returns an error describing format violation. In this case ID
|
|
|
|
// remains unchanged.
|
|
|
|
//
|
|
|
|
// Decode doesn't mutate src.
|
|
|
|
//
|
|
|
|
// See also Encode.
|
|
|
|
func (id *ID) Decode(src []byte) error {
|
|
|
|
if len(src) != 32 {
|
|
|
|
return fmt.Errorf("invalid length %d", len(src))
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(id[:], src)
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSHA256 sets object identifier value to SHA256 checksum.
|
|
|
|
func (id *ID) SetSHA256(v [sha256.Size]byte) {
|
2022-04-11 16:25:14 +00:00
|
|
|
copy(id[:], v[:])
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// Equals defines a comparison relation between two ID instances.
|
|
|
|
//
|
|
|
|
// Note that comparison using '==' operator is not recommended since it MAY result
|
|
|
|
// in loss of compatibility.
|
|
|
|
func (id ID) Equals(id2 ID) bool {
|
|
|
|
return id == id2
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// EncodeToString encodes ID into NeoFS API protocol string.
|
|
|
|
//
|
|
|
|
// Zero ID is base58 encoding of 32 zeros.
|
2021-11-08 10:04:45 +00:00
|
|
|
//
|
2022-04-11 16:25:14 +00:00
|
|
|
// See also DecodeString.
|
|
|
|
func (id ID) EncodeToString() string {
|
|
|
|
return base58.Encode(id[:])
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// DecodeString decodes string into ID according to NeoFS API protocol. Returns
|
|
|
|
// an error if s is malformed.
|
|
|
|
//
|
|
|
|
// See also DecodeString.
|
|
|
|
func (id *ID) DecodeString(s string) error {
|
2021-11-08 10:04:45 +00:00
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
2022-04-11 16:25:14 +00:00
|
|
|
return fmt.Errorf("decode base58: %w", err)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
return id.Decode(data)
|
|
|
|
}
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
// 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
|
|
|
|
// be used to encode ID into NeoFS protocol string.
|
|
|
|
func (id ID) String() string {
|
|
|
|
return id.EncodeToString()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateIDSignature signs object id with provided key.
|
2023-04-25 08:31:27 +00:00
|
|
|
func (id ID) CalculateIDSignature(signer neofscrypto.Signer) (neofscrypto.Signature, error) {
|
2022-04-05 11:13:34 +00:00
|
|
|
data, err := id.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return neofscrypto.Signature{}, fmt.Errorf("marshal ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sig neofscrypto.Signature
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2023-04-25 08:31:27 +00:00
|
|
|
return sig, sig.Calculate(signer, data)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals ID into a protobuf binary form.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (id ID) Marshal() ([]byte, error) {
|
|
|
|
var v2 refs.ObjectID
|
|
|
|
v2.SetValue(id[:])
|
|
|
|
|
2022-05-30 19:05:35 +00:00
|
|
|
return v2.StableMarshal(nil), nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of ID.
|
|
|
|
func (id *ID) Unmarshal(data []byte) error {
|
2022-04-11 16:25:14 +00:00
|
|
|
var v2 refs.ObjectID
|
|
|
|
if err := v2.Unmarshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(id[:], v2.GetValue())
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes ID to protobuf JSON format.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (id ID) MarshalJSON() ([]byte, error) {
|
|
|
|
var v2 refs.ObjectID
|
|
|
|
v2.SetValue(id[:])
|
|
|
|
|
|
|
|
return v2.MarshalJSON()
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes ID from protobuf JSON format.
|
|
|
|
func (id *ID) UnmarshalJSON(data []byte) error {
|
2022-04-11 16:25:14 +00:00
|
|
|
var v2 refs.ObjectID
|
|
|
|
if err := v2.UnmarshalJSON(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(id[:], v2.GetValue())
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|