2021-03-24 08:27:30 +00:00
|
|
|
package reputation
|
|
|
|
|
2021-04-02 09:20:09 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/reputation"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
2021-03-24 08:27:30 +00:00
|
|
|
// PeerID represents peer ID compatible with NeoFS API v2.
|
2021-04-02 09:20:09 +00:00
|
|
|
type PeerID reputation.PeerID
|
2021-03-24 08:27:30 +00:00
|
|
|
|
|
|
|
// NewPeerID creates and returns blank PeerID.
|
2021-06-08 16:57:28 +00:00
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - publicKey: nil.
|
2021-03-24 08:27:30 +00:00
|
|
|
func NewPeerID() *PeerID {
|
2021-04-02 09:20:09 +00:00
|
|
|
return PeerIDFromV2(new(reputation.PeerID))
|
2021-03-24 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:20:09 +00:00
|
|
|
// PeerIDFromV2 converts NeoFS API v2 reputation.PeerID message to PeerID.
|
2021-06-08 16:55:32 +00:00
|
|
|
//
|
|
|
|
// Nil reputation.PeerID converts to nil.
|
2021-04-02 09:20:09 +00:00
|
|
|
func PeerIDFromV2(id *reputation.PeerID) *PeerID {
|
|
|
|
return (*PeerID)(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPublicKey sets peer ID as a compressed public key.
|
|
|
|
func (x *PeerID) SetPublicKey(v [crypto.PublicKeyCompressedSize]byte) {
|
|
|
|
(*reputation.PeerID)(x).
|
2021-05-07 09:34:24 +00:00
|
|
|
SetPublicKey(v[:])
|
2021-04-02 09:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts PeerID to NeoFS API v2 reputation.PeerID message.
|
2021-06-08 16:55:32 +00:00
|
|
|
//
|
|
|
|
// Nil PeerID converts to nil.
|
2021-04-02 09:20:09 +00:00
|
|
|
func (x *PeerID) ToV2() *reputation.PeerID {
|
|
|
|
return (*reputation.PeerID)(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if identifiers are identical.
|
|
|
|
func (x *PeerID) Equal(x2 *PeerID) bool {
|
|
|
|
return bytes.Equal(
|
2021-05-07 09:34:24 +00:00
|
|
|
(*reputation.PeerID)(x).GetPublicKey(),
|
|
|
|
(*reputation.PeerID)(x2).GetPublicKey(),
|
2021-04-02 09:20:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses PeerID from base58 string.
|
|
|
|
func (x *PeerID) Parse(s string) error {
|
|
|
|
data, err := base58.Decode(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 09:34:24 +00:00
|
|
|
(*reputation.PeerID)(x).SetPublicKey(data)
|
2021-04-02 09:20:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns base58 string representation of PeerID.
|
|
|
|
func (x *PeerID) String() string {
|
|
|
|
return base58.Encode(
|
|
|
|
(*reputation.PeerID)(x).
|
2021-05-07 09:34:24 +00:00
|
|
|
GetPublicKey(),
|
2021-04-02 09:20:09 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals PeerID into a protobuf binary form.
|
|
|
|
//
|
|
|
|
// Buffer is allocated when the argument is empty.
|
|
|
|
// Otherwise, the first buffer is used.
|
|
|
|
func (x *PeerID) Marshal(b ...[]byte) ([]byte, error) {
|
|
|
|
var buf []byte
|
|
|
|
if len(b) > 0 {
|
|
|
|
buf = b[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*reputation.PeerID)(x).
|
|
|
|
StableMarshal(buf)
|
2021-03-24 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:20:09 +00:00
|
|
|
// Unmarshal unmarshals protobuf binary representation of PeerID.
|
|
|
|
func (x *PeerID) Unmarshal(data []byte) error {
|
|
|
|
return (*reputation.PeerID)(x).
|
|
|
|
Unmarshal(data)
|
2021-03-24 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:20:09 +00:00
|
|
|
// MarshalJSON encodes PeerID to protobuf JSON format.
|
|
|
|
func (x *PeerID) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*reputation.PeerID)(x).
|
|
|
|
MarshalJSON()
|
2021-03-24 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:20:09 +00:00
|
|
|
// UnmarshalJSON decodes PeerID from protobuf JSON format.
|
|
|
|
func (x *PeerID) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*reputation.PeerID)(x).
|
|
|
|
UnmarshalJSON(data)
|
2021-03-24 08:27:30 +00:00
|
|
|
}
|