-
Notifications
You must be signed in to change notification settings - Fork 40
/
session_bandwidth.go
134 lines (109 loc) · 3.74 KB
/
session_bandwidth.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cli
import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
hub "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/vpn/client/common"
"github.com/sentinel-official/hub/x/vpn/types"
)
// nolint:funlen
func SignSessionBandwidthTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "sign-bandwidth",
Short: "Sign session bandwidth",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCLIContext().WithCodec(cdc)
_id := viper.GetString(flagSubscriptionID)
bandwidth := hub.Bandwidth{
Upload: sdk.NewInt(viper.GetInt64(flagUpload)),
Download: sdk.NewInt(viper.GetInt64(flagDownload)),
}
scs, err := common.QuerySessionsCountOfSubscription(ctx, _id)
if err != nil {
return err
}
id, err := hub.NewSubscriptionIDFromString(_id)
if err != nil {
return err
}
data := hub.NewBandwidthSignatureData(id, scs, bandwidth).Bytes()
passphrase, err := keys.GetPassphrase(ctx.FromName)
if err != nil {
return err
}
kb, err := keys.NewKeyBaseFromHomeFlag()
if err != nil {
return err
}
sigBytes, pubKey, err := kb.Sign(ctx.FromName, passphrase, data)
if err != nil {
return err
}
stdSignature := auth.StdSignature{
PubKey: pubKey,
Signature: sigBytes,
}
bytes, err := cdc.MarshalJSON(stdSignature)
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
},
}
cmd.Flags().String(flagSubscriptionID, "", "Subscription ID")
cmd.Flags().Int64(flagUpload, 0, "Upload in in bytes")
cmd.Flags().Int64(flagDownload, 0, "Download in bytes")
_ = cmd.MarkFlagRequired(flagSubscriptionID)
_ = cmd.MarkFlagRequired(flagUpload)
_ = cmd.MarkFlagRequired(flagDownload)
return cmd
}
func UpdateSessionInfoTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update-session-info",
Short: "Update session info",
RunE: func(cmd *cobra.Command, args []string) error {
txb := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
ctx := context.NewCLIContext().WithCodec(cdc)
id, err := hub.NewSubscriptionIDFromString(viper.GetString(flagSubscriptionID))
if err != nil {
return err
}
bandwidth := hub.Bandwidth{
Upload: sdk.NewInt(viper.GetInt64(flagUpload)),
Download: sdk.NewInt(viper.GetInt64(flagDownload)),
}
nodeOwnerSignatureStr := viper.GetString(flagNodeOwnerSign)
clientSignatureStr := viper.GetString(flagClientSign)
var nodeOwnerSignature auth.StdSignature
if err := cdc.UnmarshalJSON([]byte(nodeOwnerSignatureStr), &nodeOwnerSignature); err != nil {
return err
}
var clientSignature auth.StdSignature
if err := cdc.UnmarshalJSON([]byte(clientSignatureStr), &clientSignature); err != nil {
return err
}
msg := types.NewMsgUpdateSessionInfo(ctx.FromAddress, id, bandwidth, nodeOwnerSignature, clientSignature)
return utils.GenerateOrBroadcastMsgs(ctx, txb, []sdk.Msg{msg})
},
}
cmd.Flags().String(flagSubscriptionID, "", "Subscription ID")
cmd.Flags().Int64(flagUpload, 0, "Upload in in bytes")
cmd.Flags().Int64(flagDownload, 0, "Download in bytes")
cmd.Flags().String(flagNodeOwnerSign, "", "Signature of the node owner")
cmd.Flags().String(flagClientSign, "", "Signature of the client")
_ = cmd.MarkFlagRequired(flagSubscriptionID)
_ = cmd.MarkFlagRequired(flagUpload)
_ = cmd.MarkFlagRequired(flagDownload)
_ = cmd.MarkFlagRequired(flagNodeOwnerSign)
_ = cmd.MarkFlagRequired(flagClientSign)
return cmd
}