-
Notifications
You must be signed in to change notification settings - Fork 1
/
register.go
64 lines (52 loc) · 1.71 KB
/
register.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
package paratime
import (
"context"
"encoding/json"
"fmt"
"os"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/spf13/cobra"
"github.com/oasisprotocol/cli/cmd/common"
cliConfig "github.com/oasisprotocol/cli/config"
)
var registerCmd = &cobra.Command{
Use: "register <descriptor.json>",
Short: "Register a new ParaTime or update an existing one",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)
txCfg := common.GetTransactionConfig()
filename := args[0]
if npa.Account == nil {
cobra.CheckErr("no accounts configured in your wallet")
}
// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var conn connection.Connection
if !txCfg.Offline {
var err error
conn, err = connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)
}
// Load runtime descriptor.
rawDescriptor, err := os.ReadFile(filename)
cobra.CheckErr(err)
// Parse runtime descriptor.
var descriptor registry.Runtime
if err = json.Unmarshal(rawDescriptor, &descriptor); err != nil {
cobra.CheckErr(fmt.Errorf("malformed ParaTime descriptor: %w", err))
}
// Prepare transaction.
tx := registry.NewRegisterRuntimeTx(0, nil, &descriptor)
acc := common.LoadAccount(cfg, npa.AccountName)
sigTx, err := common.SignConsensusTransaction(ctx, npa, acc, conn, tx)
cobra.CheckErr(err)
common.BroadcastOrExportTransaction(ctx, npa.ParaTime, conn, sigTx, nil, nil)
},
}
func init() {
registerCmd.Flags().AddFlagSet(common.SelectorNAFlags)
registerCmd.Flags().AddFlagSet(common.TxFlags)
}