2020-05-11 20:57:46 +02:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storj
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/base32"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// ErrSerialNumber is used when something goes wrong with a serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
var ErrSerialNumber = errs.Class("serial number error")
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// serialNumberEncoding is base32 without padding.
|
2020-05-11 20:57:46 +02:00
|
|
|
var serialNumberEncoding = base32.StdEncoding.WithPadding(base32.NoPadding)
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// SerialNumber is the unique identifier for pieces.
|
2020-05-11 20:57:46 +02:00
|
|
|
type SerialNumber [16]byte
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// SerialNumberFromString decodes an base32 encoded.
|
2020-05-11 20:57:46 +02:00
|
|
|
func SerialNumberFromString(s string) (SerialNumber, error) {
|
|
|
|
idBytes, err := serialNumberEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return SerialNumber{}, ErrNodeID.Wrap(err)
|
|
|
|
}
|
|
|
|
return SerialNumberFromBytes(idBytes)
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// SerialNumberFromBytes converts a byte slice into a serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func SerialNumberFromBytes(b []byte) (SerialNumber, error) {
|
|
|
|
if len(b) != len(SerialNumber{}) {
|
|
|
|
return SerialNumber{}, ErrSerialNumber.New("not enough bytes to make a serial number; have %d, need %d", len(b), len(NodeID{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
var id SerialNumber
|
|
|
|
copy(id[:], b)
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// IsZero returns whether serial number is unassigned.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) IsZero() bool {
|
|
|
|
return id == SerialNumber{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less returns whether id is smaller than other in lexicographic order.
|
|
|
|
func (id SerialNumber) Less(other SerialNumber) bool {
|
|
|
|
for k, v := range id {
|
|
|
|
if v < other[k] {
|
|
|
|
return true
|
|
|
|
} else if v > other[k] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// String representation of the serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) String() string { return serialNumberEncoding.EncodeToString(id.Bytes()) }
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Bytes returns bytes of the serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) Bytes() []byte { return id[:] }
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Marshal serializes a serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) Marshal() ([]byte, error) {
|
|
|
|
return id.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// MarshalTo serializes a serial number into the passed byte slice.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id *SerialNumber) MarshalTo(data []byte) (n int, err error) {
|
|
|
|
n = copy(data, id.Bytes())
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Unmarshal deserializes a serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id *SerialNumber) Unmarshal(data []byte) error {
|
|
|
|
var err error
|
|
|
|
*id, err = SerialNumberFromBytes(data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Size returns the length of a serial number (implements gogo's custom type interface).
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id *SerialNumber) Size() int {
|
|
|
|
return len(id)
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// MarshalJSON serializes a serial number to a json string as bytes.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + id.String() + `"`), nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// UnmarshalJSON deserializes a json string (as bytes) to a serial number.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id *SerialNumber) UnmarshalJSON(data []byte) error {
|
|
|
|
var err error
|
|
|
|
*id, err = SerialNumberFromString(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Value set a SerialNumber to a database field.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id SerialNumber) Value() (driver.Value, error) {
|
|
|
|
return id.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:08:11 +02:00
|
|
|
// Scan extracts a SerialNumber from a database field.
|
2020-05-11 20:57:46 +02:00
|
|
|
func (id *SerialNumber) Scan(src interface{}) (err error) {
|
|
|
|
b, ok := src.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return ErrSerialNumber.New("SerialNumber Scan expects []byte")
|
|
|
|
}
|
|
|
|
n, err := SerialNumberFromBytes(b)
|
|
|
|
*id = n
|
|
|
|
return err
|
|
|
|
}
|