2018-11-05 15:23:54 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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"
|
|
|
|
"crypto/ecdsa"
|
2018-11-15 19:06:09 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"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"
|
2018-11-15 19:06:09 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//GeneratePayerBandwidthAllocation creates a signed PayerBandwidthAllocation from a PayerBandwidthAllocation_Action
|
|
|
|
func GeneratePayerBandwidthAllocation(action pb.PayerBandwidthAllocation_Action, satelliteKey crypto.PrivateKey) (*pb.PayerBandwidthAllocation, error) {
|
2018-11-15 19:06:09 +00:00
|
|
|
satelliteKeyEcdsa, ok := satelliteKey.(*ecdsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("Satellite Private Key is not a valid *ecdsa.PrivateKey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate PayerBandwidthAllocation_Data
|
|
|
|
data, _ := proto.Marshal(
|
|
|
|
&pb.PayerBandwidthAllocation_Data{
|
2018-11-29 18:39:27 +00:00
|
|
|
SatelliteId: teststorj.NodeIDFromString("SatelliteID"),
|
|
|
|
UplinkId: teststorj.NodeIDFromString("UplinkID"),
|
2018-11-15 19:06:09 +00:00
|
|
|
ExpirationUnixSec: time.Now().Add(time.Hour * 24 * 10).Unix(),
|
|
|
|
SerialNumber: "SerialNumber",
|
|
|
|
Action: action,
|
|
|
|
CreatedUnixSec: time.Now().Unix(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sign the PayerBandwidthAllocation_Data with the "Satellite" Private Key
|
|
|
|
s, err := cryptopasta.Sign(data, satelliteKeyEcdsa)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("Failed to sign PayerBandwidthAllocation_Data with satellite Private Key: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine Signature and Data for PayerBandwidthAllocation
|
|
|
|
return &pb.PayerBandwidthAllocation{
|
|
|
|
Data: data,
|
|
|
|
Signature: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//GenerateRenterBandwidthAllocation creates a signed RenterBandwidthAllocation from a PayerBandwidthAllocation
|
|
|
|
func GenerateRenterBandwidthAllocation(pba *pb.PayerBandwidthAllocation, uplinkKey crypto.PrivateKey) (*pb.RenterBandwidthAllocation, error) {
|
2018-11-15 19:06:09 +00:00
|
|
|
// get "Uplink" Public Key
|
|
|
|
uplinkKeyEcdsa, ok := uplinkKey.(*ecdsa.PrivateKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, errs.New("Uplink Private Key is not a valid *ecdsa.PrivateKey")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubbytes, err := x509.MarshalPKIXPublicKey(&uplinkKeyEcdsa.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("Could not generate byte array from Uplink Public key: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate RenterBandwidthAllocation_Data
|
|
|
|
data, _ := proto.Marshal(
|
|
|
|
&pb.RenterBandwidthAllocation_Data{
|
|
|
|
PayerAllocation: pba,
|
|
|
|
PubKey: pubbytes, // TODO: Take this out. It will be kept in a database on the satellite
|
2018-11-29 18:39:27 +00:00
|
|
|
StorageNodeId: teststorj.NodeIDFromString("StorageNodeID"),
|
2018-11-15 19:06:09 +00:00
|
|
|
Total: int64(666),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sign the PayerBandwidthAllocation_Data with the "Uplink" Private Key
|
|
|
|
s, err := cryptopasta.Sign(data, uplinkKeyEcdsa)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("Failed to sign RenterBandwidthAllocation_Data with uplink Private Key: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine Signature and Data for RenterBandwidthAllocation
|
|
|
|
return &pb.RenterBandwidthAllocation{
|
|
|
|
Signature: s,
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|