2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-05 15:23:54 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
package test
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2018-11-15 19:06:09 +00:00
|
|
|
"time"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-11-12 21:59:30 +00:00
|
|
|
"github.com/gtank/cryptopasta"
|
2019-01-09 15:02:03 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-07 15:19:05 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-05 15:23:54 +00:00
|
|
|
)
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//GeneratePayerBandwidthAllocation creates a signed PayerBandwidthAllocation from a PayerBandwidthAllocation_Action
|
2019-01-25 18:05:21 +00:00
|
|
|
func GeneratePayerBandwidthAllocation(action pb.PayerBandwidthAllocation_Action, satID *identity.FullIdentity, upID *identity.FullIdentity, expiration time.Duration) (*pb.PayerBandwidthAllocation, error) {
|
2019-01-09 15:02:03 +00:00
|
|
|
serialNum, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-15 19:06:09 +00:00
|
|
|
// Generate PayerBandwidthAllocation_Data
|
|
|
|
data, _ := proto.Marshal(
|
|
|
|
&pb.PayerBandwidthAllocation_Data{
|
2019-01-25 18:05:21 +00:00
|
|
|
SatelliteId: satID.ID,
|
|
|
|
UplinkId: upID.ID,
|
2019-01-15 04:44:56 +00:00
|
|
|
ExpirationUnixSec: time.Now().Add(expiration).Unix(),
|
2019-01-09 15:02:03 +00:00
|
|
|
SerialNumber: serialNum.String(),
|
2018-11-15 19:06:09 +00:00
|
|
|
Action: action,
|
|
|
|
CreatedUnixSec: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
// Sign the PayerBandwidthAllocation_Data with the "Satellite" Private Key
|
2019-01-25 18:05:21 +00:00
|
|
|
satPrivECDSA, ok := satID.Key.(*ecdsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, pb.Payer.Wrap(auth.ECDSA)
|
|
|
|
}
|
|
|
|
s, err := cryptopasta.Sign(data, satPrivECDSA)
|
2018-11-15 19:06:09 +00:00
|
|
|
if err != nil {
|
2019-01-25 18:05:21 +00:00
|
|
|
return nil, pb.Payer.Wrap(auth.Sign.Wrap(err))
|
2018-11-15 19:06:09 +00:00
|
|
|
}
|
|
|
|
// Combine Signature and Data for PayerBandwidthAllocation
|
|
|
|
return &pb.PayerBandwidthAllocation{
|
|
|
|
Data: data,
|
|
|
|
Signature: s,
|
2019-01-25 18:05:21 +00:00
|
|
|
Certs: satID.ChainRaw(),
|
2018-11-15 19:06:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//GenerateRenterBandwidthAllocation creates a signed RenterBandwidthAllocation from a PayerBandwidthAllocation
|
2019-01-25 18:05:21 +00:00
|
|
|
func GenerateRenterBandwidthAllocation(pba *pb.PayerBandwidthAllocation, storageNodeID storj.NodeID, upID *identity.FullIdentity, total int64) (*pb.RenterBandwidthAllocation, error) {
|
2018-11-15 19:06:09 +00:00
|
|
|
// Generate RenterBandwidthAllocation_Data
|
|
|
|
data, _ := proto.Marshal(
|
|
|
|
&pb.RenterBandwidthAllocation_Data{
|
|
|
|
PayerAllocation: pba,
|
2019-01-07 15:19:05 +00:00
|
|
|
StorageNodeId: storageNodeID,
|
2019-01-25 18:05:21 +00:00
|
|
|
Total: total,
|
2018-11-15 19:06:09 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
// Sign the PayerBandwidthAllocation_Data with the "Uplink" Private Key
|
2019-01-25 18:05:21 +00:00
|
|
|
upPrivECDSA, ok := upID.Key.(*ecdsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, pb.Payer.Wrap(auth.ECDSA)
|
|
|
|
}
|
|
|
|
s, err := cryptopasta.Sign(data, upPrivECDSA)
|
2018-11-15 19:06:09 +00:00
|
|
|
if err != nil {
|
2019-01-25 18:05:21 +00:00
|
|
|
return nil, pb.Payer.Wrap(auth.Sign.Wrap(err))
|
2018-11-15 19:06:09 +00:00
|
|
|
}
|
|
|
|
// Combine Signature and Data for RenterBandwidthAllocation
|
|
|
|
return &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: s,
|
|
|
|
Data: data,
|
2019-01-25 18:05:21 +00:00
|
|
|
Certs: upID.ChainRaw(),
|
2018-11-15 19:06:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|