[#44] signature: move package from neofs-api-go
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
372468c320
commit
1ac6b819c5
2 changed files with 137 additions and 0 deletions
71
signature/signature.go
Normal file
71
signature/signature.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package signature
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
)
|
||||
|
||||
// Signature represents v2-compatible signature.
|
||||
type Signature refs.Signature
|
||||
|
||||
// NewFromV2 wraps v2 Signature message to Signature.
|
||||
//
|
||||
// Nil refs.Signature converts to nil.
|
||||
func NewFromV2(sV2 *refs.Signature) *Signature {
|
||||
return (*Signature)(sV2)
|
||||
}
|
||||
|
||||
// New creates and initializes blank Signature.
|
||||
//
|
||||
// Defaults:
|
||||
// - key: nil;
|
||||
// - signature: nil.
|
||||
func New() *Signature {
|
||||
return NewFromV2(new(refs.Signature))
|
||||
}
|
||||
|
||||
// Key sets binary public key.
|
||||
func (s *Signature) Key() []byte {
|
||||
return (*refs.Signature)(s).GetKey()
|
||||
}
|
||||
|
||||
// SetKey returns binary public key.
|
||||
func (s *Signature) SetKey(v []byte) {
|
||||
(*refs.Signature)(s).SetKey(v)
|
||||
}
|
||||
|
||||
// Sign return signature value.
|
||||
func (s *Signature) Sign() []byte {
|
||||
return (*refs.Signature)(s).GetSign()
|
||||
}
|
||||
|
||||
// SetSign sets signature value.
|
||||
func (s *Signature) SetSign(v []byte) {
|
||||
(*refs.Signature)(s).SetSign(v)
|
||||
}
|
||||
|
||||
// ToV2 converts Signature to v2 Signature message.
|
||||
//
|
||||
// Nil Signature converts to nil.
|
||||
func (s *Signature) ToV2() *refs.Signature {
|
||||
return (*refs.Signature)(s)
|
||||
}
|
||||
|
||||
// Marshal marshals Signature into a protobuf binary form.
|
||||
func (s *Signature) Marshal() ([]byte, error) {
|
||||
return (*refs.Signature)(s).StableMarshal(nil)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals protobuf binary representation of Signature.
|
||||
func (s *Signature) Unmarshal(data []byte) error {
|
||||
return (*refs.Signature)(s).Unmarshal(data)
|
||||
}
|
||||
|
||||
// MarshalJSON encodes Signature to protobuf JSON format.
|
||||
func (s *Signature) MarshalJSON() ([]byte, error) {
|
||||
return (*refs.Signature)(s).MarshalJSON()
|
||||
}
|
||||
|
||||
// UnmarshalJSON decodes Signature from protobuf JSON format.
|
||||
func (s *Signature) UnmarshalJSON(data []byte) error {
|
||||
return (*refs.Signature)(s).UnmarshalJSON(data)
|
||||
}
|
66
signature/signature_test.go
Normal file
66
signature/signature_test.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
package signature
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSignatureEncoding(t *testing.T) {
|
||||
s := New()
|
||||
s.SetKey([]byte("key"))
|
||||
s.SetSign([]byte("sign"))
|
||||
|
||||
t.Run("binary", func(t *testing.T) {
|
||||
data, err := s.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
s2 := New()
|
||||
require.NoError(t, s2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, s, s2)
|
||||
})
|
||||
|
||||
t.Run("json", func(t *testing.T) {
|
||||
data, err := s.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
s2 := New()
|
||||
require.NoError(t, s2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, s, s2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewSignatureFromV2(t *testing.T) {
|
||||
t.Run("from nil", func(t *testing.T) {
|
||||
var x *refs.Signature
|
||||
|
||||
require.Nil(t, NewFromV2(x))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSignature_ToV2(t *testing.T) {
|
||||
t.Run("nil", func(t *testing.T) {
|
||||
var x *Signature
|
||||
|
||||
require.Nil(t, x.ToV2())
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewSignature(t *testing.T) {
|
||||
t.Run("default values", func(t *testing.T) {
|
||||
sg := New()
|
||||
|
||||
// check initial values
|
||||
require.Nil(t, sg.Key())
|
||||
require.Nil(t, sg.Sign())
|
||||
|
||||
// convert to v2 message
|
||||
sgV2 := sg.ToV2()
|
||||
|
||||
require.Nil(t, sgV2.GetKey())
|
||||
require.Nil(t, sgV2.GetSign())
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue