storj/pkg/pointerdb/allocation.go

57 lines
1.5 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package pointerdb
import (
"context"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/pkg/auth"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pb"
)
// AllocationSigner structure
type AllocationSigner struct {
satelliteIdentity *identity.FullIdentity
bwExpiration int
}
// NewAllocationSigner creates new instance
func NewAllocationSigner(satelliteIdentity *identity.FullIdentity, bwExpiration int) *AllocationSigner {
return &AllocationSigner{
satelliteIdentity: satelliteIdentity,
bwExpiration: bwExpiration,
}
}
// PayerBandwidthAllocation returns generated payer bandwidth allocation
func (allocation *AllocationSigner) PayerBandwidthAllocation(ctx context.Context, peerIdentity *identity.PeerIdentity, action pb.BandwidthAction) (pba *pb.PayerBandwidthAllocation, err error) {
if peerIdentity == nil {
return nil, Error.New("missing peer identity")
}
serialNum, err := uuid.New()
if err != nil {
return nil, err
}
created := time.Now().Unix()
// convert ttl from days to seconds
ttl := allocation.bwExpiration
ttl *= 86400
pba = &pb.PayerBandwidthAllocation{
SatelliteId: allocation.satelliteIdentity.ID,
UplinkId: peerIdentity.ID,
CreatedUnixSec: created,
ExpirationUnixSec: created + int64(ttl),
Action: action,
SerialNumber: serialNum.String(),
}
if err := auth.SignMessage(pba, *allocation.satelliteIdentity); err != nil {
return nil, err
}
return pba, nil
}