frostfs-node/pkg/services/accounting/sign.go
Leonard Lyubich 5cf622881f [#11] accounting: Implement signing service
Adds accounting Service implementation that verifiers request signatures and
signs responses from internal Service.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-10-02 11:23:29 +03:00

43 lines
947 B
Go

package accounting
import (
"context"
"crypto/ecdsa"
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-api-go/v2/signature"
"github.com/pkg/errors"
)
type signService struct {
key *ecdsa.PrivateKey
svc accounting.Service
}
func NewSignService(key *ecdsa.PrivateKey, svc accounting.Service) accounting.Service {
return &signService{
key: key,
svc: svc,
}
}
func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
// verify request signatures
if err := signature.VerifyServiceMessage(req); err != nil {
return nil, errors.Wrap(err, "could not verify request")
}
// process request
resp, err := s.svc.Balance(ctx, req)
if err != nil {
return nil, err
}
// sign the response
if err := signature.SignServiceMessage(s.key, resp); err != nil {
return nil, errors.Wrap(err, "could not sign response")
}
return resp, nil
}