2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package signing
|
|
|
|
|
|
|
|
import (
|
2019-06-05 14:47:01 +01:00
|
|
|
"context"
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error is the default error class for signing package.
|
|
|
|
var Error = errs.Class("signing")
|
|
|
|
|
|
|
|
// Signer is able to sign data and verify own signature belongs.
|
|
|
|
type Signer interface {
|
|
|
|
ID() storj.NodeID
|
2019-06-05 14:47:01 +01:00
|
|
|
HashAndSign(ctx context.Context, data []byte) ([]byte, error)
|
|
|
|
HashAndVerifySignature(ctx context.Context, data, signature []byte) error
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignOrderLimit signs the order limit using the specified signer.
|
|
|
|
// Signer is a satellite.
|
2019-07-01 16:54:11 +01:00
|
|
|
func SignOrderLimit(ctx context.Context, satellite Signer, unsigned *pb.OrderLimit) (_ *pb.OrderLimit, err error) {
|
2019-06-05 14:47:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodeOrderLimit(ctx, unsigned)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
2019-06-05 14:47:01 +01:00
|
|
|
signed.SatelliteSignature, err = satellite.HashAndSign(ctx, bytes)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignOrder signs the order using the specified signer.
|
|
|
|
// Signer is an uplink.
|
2019-07-01 16:54:11 +01:00
|
|
|
func SignOrder(ctx context.Context, uplink Signer, unsigned *pb.Order) (_ *pb.Order, err error) {
|
2019-06-05 14:47:01 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodeOrder(ctx, unsigned)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
2019-06-05 14:47:01 +01:00
|
|
|
signed.UplinkSignature, err = uplink.HashAndSign(ctx, bytes)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignPieceHash signs the piece hash using the specified signer.
|
|
|
|
// Signer is either uplink or storage node.
|
2019-06-05 14:47:01 +01:00
|
|
|
func SignPieceHash(ctx context.Context, signer Signer, unsigned *pb.PieceHash) (_ *pb.PieceHash, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodePieceHash(ctx, unsigned)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
2019-06-05 14:47:01 +01:00
|
|
|
signed.Signature, err = signer.HashAndSign(ctx, bytes)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|
2019-05-30 20:52:33 +01:00
|
|
|
|
|
|
|
// SignVoucher signs the voucher using the specified signer
|
|
|
|
// Signer is a satellite
|
2019-06-05 14:47:01 +01:00
|
|
|
func SignVoucher(ctx context.Context, signer Signer, unsigned *pb.Voucher) (_ *pb.Voucher, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodeVoucher(ctx, unsigned)
|
2019-05-30 20:52:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
2019-06-05 14:47:01 +01:00
|
|
|
signed.SatelliteSignature, err = signer.HashAndSign(ctx, bytes)
|
2019-05-30 20:52:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|