Skip to content

Commit

Permalink
[#55] refs: Add Scheme field to Signature
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
  • Loading branch information
fyrchik authored and cthulhu-rider committed Feb 25, 2022
1 parent 66e1fb8 commit a4349f6
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 85 deletions.
2 changes: 2 additions & 0 deletions refs/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (s *Signature) ToGRPCMessage() grpc.Message {

m.SetKey(s.key)
m.SetSign(s.sign)
m.SetScheme(refs.SignatureScheme(s.scheme))
}

return m
Expand All @@ -273,6 +274,7 @@ func (s *Signature) FromGRPCMessage(m grpc.Message) error {

s.key = v.GetKey()
s.sign = v.GetSign()
s.scheme = SignatureScheme(v.GetScheme())

return nil
}
Expand Down
20 changes: 20 additions & 0 deletions refs/grpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ func (x *Signature) SetSign(v []byte) {
}
}

// SetScheme sets signature scheme.
func (x *Signature) SetScheme(s SignatureScheme) {
if x != nil {
x.Scheme = s
}
}

// FromString parses SignatureScheme from a string representation,
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (x *SignatureScheme) FromString(s string) bool {
i, ok := SignatureScheme_value[s]
if ok {
*x = SignatureScheme(i)
}

return ok
}

// FromString parses ChecksumType from a string representation,
// It is a reverse action to String().
//
Expand Down
151 changes: 112 additions & 39 deletions refs/grpc/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions refs/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const (
checksumTypeField = 1
checksumValueField = 2

signatureKeyField = 1
signatureValueField = 2
signatureKeyField = 1
signatureValueField = 2
signatureSchemeField = 3

versionMajorField = 1
versionMinorField = 2
Expand Down Expand Up @@ -250,7 +251,14 @@ func (s *Signature) StableMarshal(buf []byte) ([]byte, error) {

offset += n

_, err = proto.BytesMarshal(signatureValueField, buf[offset:], s.sign)
n, err = proto.BytesMarshal(signatureValueField, buf[offset:], s.sign)
if err != nil {
return nil, err
}

offset += n

_, err = proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme))
if err != nil {
return nil, err
}
Expand All @@ -265,6 +273,7 @@ func (s *Signature) StableSize() (size int) {

size += proto.BytesSize(signatureKeyField, s.key)
size += proto.BytesSize(signatureValueField, s.sign)
size += proto.EnumSize(signatureSchemeField, int32(s.scheme))

return size
}
Expand Down
21 changes: 21 additions & 0 deletions refs/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ func (t *ChecksumType) FromString(s string) bool {

return ok
}

// String returns string representation of SignatureScheme.
func (t SignatureScheme) String() string {
return refs.SignatureScheme(t).String()
}

// FromString parses SignatureScheme from a string representation.
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (t *SignatureScheme) FromString(s string) bool {
var g refs.SignatureScheme

ok := g.FromString(s)

if ok {
*t = SignatureScheme(g)
}

return ok
}
3 changes: 3 additions & 0 deletions refs/test/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package refstest

import (
"math/rand"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
)

Expand Down Expand Up @@ -88,6 +90,7 @@ func GenerateSignature(empty bool) *refs.Signature {
if !empty {
m.SetKey([]byte{1})
m.SetSign([]byte{2})
m.SetScheme(refs.SignatureScheme(rand.Int31() % 3))
}

return m
Expand Down
22 changes: 22 additions & 0 deletions refs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ type Checksum struct {

type ChecksumType uint32

type SignatureScheme uint32

const (
UnspecifiedScheme SignatureScheme = iota
ECDSA_SHA512
ECDSA_RFC6979_SHA256
)

type Signature struct {
key, sign []byte
scheme SignatureScheme
}

type SubnetID struct {
Expand Down Expand Up @@ -175,6 +184,19 @@ func (s *Signature) SetSign(v []byte) {
}
}

func (s *Signature) GetScheme() SignatureScheme {
if s != nil {
return s.scheme
}
return UnspecifiedScheme
}

func (s *Signature) SetScheme(scheme SignatureScheme) {
if s != nil {
s.scheme = scheme
}
}

func (s *SubnetID) SetValue(id uint32) {
if s != nil {
s.value = id
Expand Down
Loading

0 comments on commit a4349f6

Please sign in to comment.