-
Notifications
You must be signed in to change notification settings - Fork 397
/
encode.go
110 lines (92 loc) · 3.67 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package signing
import (
"context"
"github.com/gogo/protobuf/proto"
"storj.io/storj/pkg/pb"
)
// EncodeOrderLimit encodes order limit into bytes for signing. Removes signature from serialized limit.
func EncodeOrderLimit(ctx context.Context, limit *pb.OrderLimit) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
// protobuf has problems with serializing types with nullable=false
// this uses a different message for signing, such that the rest of the code
// doesn't have to deal with pointers for those particular fields.
signing := pb.OrderLimitSigning{}
signing.SerialNumber = limit.SerialNumber
signing.SatelliteId = limit.SatelliteId
if limit.DeprecatedUplinkId != nil && !limit.DeprecatedUplinkId.IsZero() {
signing.DeprecatedUplinkId = limit.DeprecatedUplinkId
}
if !limit.UplinkPublicKey.IsZero() {
signing.UplinkPublicKey = &limit.UplinkPublicKey
}
signing.StorageNodeId = limit.StorageNodeId
signing.PieceId = limit.PieceId
signing.Limit = limit.Limit
signing.Action = limit.Action
if !limit.PieceExpiration.IsZero() {
signing.PieceExpiration = &limit.PieceExpiration
}
if !limit.OrderExpiration.IsZero() {
signing.OrderExpiration = &limit.OrderExpiration
}
if !limit.OrderCreation.IsZero() {
signing.OrderCreation = &limit.OrderCreation
}
signing.SatelliteAddress = limit.SatelliteAddress
return proto.Marshal(&signing)
}
// EncodeOrder encodes order into bytes for signing. Removes signature from serialized order.
func EncodeOrder(ctx context.Context, order *pb.Order) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
// protobuf has problems with serializing types with nullable=false
// this uses a different message for signing, such that the rest of the code
// doesn't have to deal with pointers for those particular fields.
signing := pb.OrderSigning{}
signing.SerialNumber = order.SerialNumber
signing.Amount = order.Amount
return proto.Marshal(&signing)
}
// EncodePieceHash encodes piece hash into bytes for signing. Removes signature from serialized hash.
func EncodePieceHash(ctx context.Context, hash *pb.PieceHash) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
// protobuf has problems with serializing types with nullable=false
// this uses a different message for signing, such that the rest of the code
// doesn't have to deal with pointers for those particular fields.
signing := pb.PieceHashSigning{}
signing.PieceId = hash.PieceId
signing.Hash = hash.Hash
signing.PieceSize = hash.PieceSize
if !hash.Timestamp.IsZero() {
signing.Timestamp = &hash.Timestamp
}
return proto.Marshal(&signing)
}
// EncodeVoucher encodes voucher into bytes for signing.
func EncodeVoucher(ctx context.Context, voucher *pb.Voucher) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
signature := voucher.SatelliteSignature
voucher.SatelliteSignature = nil
out, err := proto.Marshal(voucher)
voucher.SatelliteSignature = signature
return out, err
}
// EncodeStreamID encodes stream ID into bytes for signing.
func EncodeStreamID(ctx context.Context, streamID *pb.SatStreamID) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
signature := streamID.SatelliteSignature
streamID.SatelliteSignature = nil
out, err := proto.Marshal(streamID)
streamID.SatelliteSignature = signature
return out, err
}
// EncodeSegmentID encodes segment ID into bytes for signing.
func EncodeSegmentID(ctx context.Context, segmentID *pb.SatSegmentID) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
signature := segmentID.SatelliteSignature
segmentID.SatelliteSignature = nil
out, err := proto.Marshal(segmentID)
segmentID.SatelliteSignature = signature
return out, err
}