2021-11-08 08:41:39 +00:00
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-30 16:08:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/reputation"
|
2021-11-08 08:41:39 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
)
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// PeerID represents unique identifier of the peer participating in the NeoFS
|
|
|
|
// reputation system.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// ID is mutually compatible with github.com/TrueCloudLab/frostfs-api-go/v2/reputation.PeerID
|
2022-06-30 16:08:34 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
type PeerID struct {
|
|
|
|
m reputation.PeerID
|
|
|
|
}
|
2021-11-08 08:41:39 +00:00
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// ReadFromV2 reads PeerID from the reputation.PeerID message. Returns an
|
|
|
|
// error if the message is malformed according to the NeoFS API V2 protocol.
|
2021-11-08 08:41:39 +00:00
|
|
|
//
|
2022-06-30 16:08:34 +00:00
|
|
|
// See also WriteToV2.
|
|
|
|
func (x *PeerID) ReadFromV2(m reputation.PeerID) error {
|
|
|
|
val := m.GetPublicKey()
|
|
|
|
if len(val) == 0 {
|
|
|
|
return errors.New("missing ID bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
x.m = m
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// WriteToV2 writes PeerID to the reputation.PeerID message.
|
|
|
|
// The message must not be nil.
|
2021-11-08 08:41:39 +00:00
|
|
|
//
|
2022-06-30 16:08:34 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x PeerID) WriteToV2(m *reputation.PeerID) {
|
|
|
|
*m = x.m
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// SetPublicKey sets PeerID as a binary-encoded public key which authenticates
|
|
|
|
// the participant of the NeoFS reputation system.
|
|
|
|
//
|
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
|
|
|
//
|
|
|
|
// See also CompareKey.
|
|
|
|
func (x *PeerID) SetPublicKey(key []byte) {
|
|
|
|
x.m.SetPublicKey(key)
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// PublicKey return public key set using SetPublicKey.
|
2021-11-08 08:41:39 +00:00
|
|
|
//
|
2022-06-30 16:08:34 +00:00
|
|
|
// Zero PeerID has zero key which is incorrect according to NeoFS API
|
|
|
|
// protocol.
|
|
|
|
//
|
|
|
|
// Return value MUST NOT be mutated, make a copy first.
|
|
|
|
func (x PeerID) PublicKey() []byte {
|
|
|
|
return x.m.GetPublicKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComparePeerKey checks if the given PeerID corresponds to the party
|
|
|
|
// authenticated by the given binary public key.
|
|
|
|
func ComparePeerKey(peer PeerID, key []byte) bool {
|
|
|
|
return bytes.Equal(peer.PublicKey(), key)
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// EncodeToString encodes ID into NeoFS API protocol string.
|
|
|
|
//
|
|
|
|
// Zero PeerID is base58 encoding of PeerIDSize zeros.
|
|
|
|
//
|
|
|
|
// See also DecodeString.
|
|
|
|
func (x PeerID) EncodeToString() string {
|
|
|
|
return base58.Encode(x.m.GetPublicKey())
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
// DecodeString decodes string into PeerID according to NeoFS API protocol.
|
|
|
|
// Returns an error if s is malformed.
|
|
|
|
//
|
|
|
|
// See also DecodeString.
|
|
|
|
func (x *PeerID) DecodeString(s string) error {
|
2021-11-08 08:41:39 +00:00
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
2022-06-30 16:08:34 +00:00
|
|
|
return fmt.Errorf("decode base58: %w", err)
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +00:00
|
|
|
x.m.SetPublicKey(data)
|
2021-11-08 08:41:39 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-30 16:08:34 +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 (x PeerID) String() string {
|
|
|
|
return x.EncodeToString()
|
2021-11-08 08:41:39 +00:00
|
|
|
}
|