Implement signed data calculating function from SignedDataReader

This commit is contained in:
Leonard Lyubich 2020-05-11 17:28:44 +03:00
parent ab198b4049
commit e01fb0cc62
10 changed files with 126 additions and 149 deletions

View file

@ -3,17 +3,13 @@ package accounting
import ( import (
"encoding/binary" "encoding/binary"
"io" "io"
"github.com/nspcc-dev/neofs-api-go/service"
) )
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m BalanceRequest) SignedData() ([]byte, error) { func (m BalanceRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -37,13 +33,7 @@ func (m BalanceRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m GetRequest) SignedData() ([]byte, error) { func (m GetRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -71,13 +61,7 @@ func (m GetRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m PutRequest) SignedData() ([]byte, error) { func (m PutRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -125,13 +109,7 @@ func (m PutRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m ListRequest) SignedData() ([]byte, error) { func (m ListRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -155,13 +133,7 @@ func (m ListRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m DeleteRequest) SignedData() ([]byte, error) { func (m DeleteRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.

View file

@ -1,16 +1,14 @@
package bootstrap package bootstrap
import "io" import (
"io"
"github.com/nspcc-dev/neofs-api-go/service"
)
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m Request) SignedData() ([]byte, error) { func (m Request) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.

View file

@ -3,19 +3,15 @@ package container
import ( import (
"encoding/binary" "encoding/binary"
"io" "io"
service "github.com/nspcc-dev/neofs-api-go/service"
) )
var requestEndianness = binary.BigEndian var requestEndianness = binary.BigEndian
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m PutRequest) SignedData() ([]byte, error) { func (m PutRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -67,13 +63,7 @@ func (m PutRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m DeleteRequest) SignedData() ([]byte, error) { func (m DeleteRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -98,13 +88,7 @@ func (m DeleteRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m GetRequest) SignedData() ([]byte, error) { func (m GetRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -129,13 +113,7 @@ func (m GetRequest) ReadSignedData(p []byte) (int, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m ListRequest) SignedData() ([]byte, error) { func (m ListRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.

View file

@ -3,34 +3,27 @@ package object
import ( import (
"encoding/binary" "encoding/binary"
"io" "io"
"github.com/nspcc-dev/neofs-api-go/service"
) )
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
// //
// If payload is nil, ErrHeaderNotFound returns. // If payload is nil, ErrHeaderNotFound returns.
func (m PutRequest) SignedData() ([]byte, error) { func (m PutRequest) SignedData() ([]byte, error) {
sz := m.SignedDataSize() return service.SignedDataFromReader(m)
if sz < 0 {
return nil, ErrHeaderNotFound
}
data := make([]byte, sz)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m PutRequest) ReadSignedData(p []byte) error { func (m PutRequest) ReadSignedData(p []byte) (int, error) {
r := m.GetR() r := m.GetR()
if r == nil { if r == nil {
return ErrHeaderNotFound return 0, ErrHeaderNotFound
} }
_, err := r.MarshalTo(p) return r.MarshalTo(p)
return err
} }
// SignedDataSize returns the size of payload of the Put request. // SignedDataSize returns the size of payload of the Put request.
@ -47,26 +40,26 @@ func (m PutRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m GetRequest) SignedData() ([]byte, error) { func (m GetRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRequest) ReadSignedData(p []byte) error { func (m GetRequest) ReadSignedData(p []byte) (int, error) {
addr := m.GetAddress() addr := m.GetAddress()
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
off := copy(p, addr.CID.Bytes()) var off int
copy(p[off:], addr.ObjectID.Bytes()) off += copy(p[off:], addr.CID.Bytes())
return nil off += copy(p[off:], addr.ObjectID.Bytes())
return off, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -76,28 +69,28 @@ func (m GetRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m HeadRequest) SignedData() ([]byte, error) { func (m HeadRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m HeadRequest) ReadSignedData(p []byte) error { func (m HeadRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
if m.GetFullHeaders() { if m.GetFullHeaders() {
p[0] = 1 p[0] = 1
} }
off := 1 + copy(p[1:], m.Address.CID.Bytes()) off := 1
copy(p[off:], m.Address.ObjectID.Bytes()) off += copy(p[off:], m.Address.CID.Bytes())
return nil off += copy(p[off:], m.Address.ObjectID.Bytes())
return off, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -107,24 +100,24 @@ func (m HeadRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m DeleteRequest) SignedData() ([]byte, error) { func (m DeleteRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m DeleteRequest) ReadSignedData(p []byte) error { func (m DeleteRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
off := copy(p, m.OwnerID.Bytes()) var off int
copy(p[off:], addressBytes(m.Address)) off += copy(p[off:], m.OwnerID.Bytes())
return nil off += copy(p[off:], addressBytes(m.Address))
return off, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -134,27 +127,25 @@ func (m DeleteRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m GetRangeRequest) SignedData() ([]byte, error) { func (m GetRangeRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRangeRequest) ReadSignedData(p []byte) error { func (m GetRangeRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
n, err := (&m.Range).MarshalTo(p) n, err := (&m.Range).MarshalTo(p)
if err != nil { if err != nil {
return err return 0, err
} }
copy(p[n:], addressBytes(m.GetAddress())) n += copy(p[n:], addressBytes(m.GetAddress()))
return nil return n, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -164,17 +155,15 @@ func (m GetRangeRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m GetRangeHashRequest) SignedData() ([]byte, error) { func (m GetRangeHashRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m GetRangeHashRequest) ReadSignedData(p []byte) error { func (m GetRangeHashRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
var off int var off int
@ -185,7 +174,7 @@ func (m GetRangeHashRequest) ReadSignedData(p []byte) error {
off += copy(p[off:], m.GetSalt()) off += copy(p[off:], m.GetSalt())
return nil return off, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.
@ -203,17 +192,15 @@ func (m GetRangeHashRequest) SignedDataSize() int {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m SearchRequest) SignedData() ([]byte, error) { func (m SearchRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
return data, m.ReadSignedData(data)
} }
// ReadSignedData copies payload bytes to passed buffer. // ReadSignedData copies payload bytes to passed buffer.
// //
// If the buffer size is insufficient, io.ErrUnexpectedEOF returns. // If the buffer size is insufficient, io.ErrUnexpectedEOF returns.
func (m SearchRequest) ReadSignedData(p []byte) error { func (m SearchRequest) ReadSignedData(p []byte) (int, error) {
if len(p) < m.SignedDataSize() { if len(p) < m.SignedDataSize() {
return io.ErrUnexpectedEOF return 0, io.ErrUnexpectedEOF
} }
var off int var off int
@ -223,9 +210,9 @@ func (m SearchRequest) ReadSignedData(p []byte) error {
binary.BigEndian.PutUint32(p[off:], m.GetQueryVersion()) binary.BigEndian.PutUint32(p[off:], m.GetQueryVersion())
off += 4 off += 4
copy(p[off:], m.GetQuery()) off += copy(p[off:], m.GetQuery())
return nil return off, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.

View file

@ -43,3 +43,7 @@ const ErrNilDataWithTokenSignAccumulator = internal.Error("signed data with toke
// ErrNilSignatureKeySourceWithToken is returned by functions that expect // ErrNilSignatureKeySourceWithToken is returned by functions that expect
// a non-nil SignatureKeySourceWithToken, but received nil. // a non-nil SignatureKeySourceWithToken, but received nil.
const ErrNilSignatureKeySourceWithToken = internal.Error("key-signature source with token is nil") const ErrNilSignatureKeySourceWithToken = internal.Error("key-signature source with token is nil")
// ErrNilSignedDataReader is returned by functions that expect
// a non-nil SignedDataReader, but received nil.
const ErrNilSignedDataReader = internal.Error("signed data reader is nil")

View file

@ -123,11 +123,7 @@ func (m *Token) AddSignKey(sig []byte, _ *ecdsa.PublicKey) {
// SignedData returns token information in a binary representation. // SignedData returns token information in a binary representation.
func (m *Token) SignedData() ([]byte, error) { func (m *Token) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return SignedDataFromReader(m)
copyTokenSignedData(data, m)
return data, nil
} }
// ReadSignedData copies a binary representation of the token information to passed buffer. // ReadSignedData copies a binary representation of the token information to passed buffer.

18
service/utils.go Normal file
View file

@ -0,0 +1,18 @@
package service
// SignedDataFromReader allocates buffer and reads bytes from passed reader to it.
//
// If passed SignedDataReader is nil, ErrNilSignedDataReader returns.
func SignedDataFromReader(r SignedDataReader) ([]byte, error) {
if r == nil {
return nil, ErrNilSignedDataReader
}
data := make([]byte, r.SignedDataSize())
if _, err := r.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
}

34
service/utils_test.go Normal file
View file

@ -0,0 +1,34 @@
package service
import (
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
func TestSignedDataFromReader(t *testing.T) {
// nil SignedDataReader
_, err := SignedDataFromReader(nil)
require.EqualError(t, err, ErrNilSignedDataReader.Error())
rdr := &testSignedDataReader{
testSignedDataSrc: new(testSignedDataSrc),
}
// make reader to return an error
rdr.err = errors.New("test error")
_, err = SignedDataFromReader(rdr)
require.EqualError(t, err, rdr.err.Error())
// remove the error
rdr.err = nil
// fill the data
rdr.data = testData(t, 10)
res, err := SignedDataFromReader(rdr)
require.NoError(t, err)
require.Equal(t, rdr.data, res)
}

View file

@ -5,6 +5,7 @@ import (
"io" "io"
"github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/refs"
"github.com/nspcc-dev/neofs-api-go/service"
) )
const signedRequestDataSize = 0 + const signedRequestDataSize = 0 +
@ -31,14 +32,7 @@ func (m *CreateRequest) SetOwnerID(id OwnerID) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m CreateRequest) SignedData() ([]byte, error) { func (m CreateRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
_, err := m.ReadSignedData(data)
if err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.

View file

@ -2,6 +2,8 @@ package state
import ( import (
"io" "io"
"github.com/nspcc-dev/neofs-api-go/service"
) )
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
@ -41,13 +43,7 @@ func (m DumpVarsRequest) SignedData() ([]byte, error) {
// SignedData returns payload bytes of the request. // SignedData returns payload bytes of the request.
func (m ChangeStateRequest) SignedData() ([]byte, error) { func (m ChangeStateRequest) SignedData() ([]byte, error) {
data := make([]byte, m.SignedDataSize()) return service.SignedDataFromReader(m)
if _, err := m.ReadSignedData(data); err != nil {
return nil, err
}
return data, nil
} }
// SignedDataSize returns payload size of the request. // SignedDataSize returns payload size of the request.