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
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
// SignUplinkOrder signs the order using the specified signer.
|
2019-03-18 10:55:06 +00:00
|
|
|
// Signer is an uplink.
|
2019-07-11 21:51:40 +01:00
|
|
|
func SignUplinkOrder(ctx context.Context, privateKey storj.PiecePrivateKey, 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-07-11 21:51:40 +01:00
|
|
|
signed.UplinkSignature, err = privateKey.Sign(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
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
// SignUplinkPieceHash signs the piece hash using the specified signer.
|
|
|
|
// Signer is either uplink or storage node.
|
|
|
|
func SignUplinkPieceHash(ctx context.Context, privateKey storj.PiecePrivateKey, unsigned *pb.PieceHash) (_ *pb.PieceHash, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodePieceHash(ctx, unsigned)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
|
|
|
signed.Signature, err = privateKey.Sign(bytes)
|
|
|
|
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
|
|
|
|
}
|
2019-07-16 11:39:23 +01:00
|
|
|
|
|
|
|
// SignStreamID signs the stream ID using the specified signer
|
|
|
|
// Signer is a satellite
|
|
|
|
func SignStreamID(ctx context.Context, signer Signer, unsigned *pb.SatStreamID) (_ *pb.SatStreamID, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodeStreamID(ctx, unsigned)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
|
|
|
signed.SatelliteSignature, err = signer.HashAndSign(ctx, bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|
2019-07-22 15:45:18 +01:00
|
|
|
|
|
|
|
// SignSegmentID signs the segment ID using the specified signer
|
|
|
|
// Signer is a satellite
|
|
|
|
func SignSegmentID(ctx context.Context, signer Signer, unsigned *pb.SatSegmentID) (_ *pb.SatSegmentID, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bytes, err := EncodeSegmentID(ctx, unsigned)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signed := *unsigned
|
|
|
|
signed.SatelliteSignature, err = signer.HashAndSign(ctx, bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &signed, nil
|
|
|
|
}
|