|
| 1 | +package rofl |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 10 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl" |
| 11 | + |
| 12 | + buildRofl "github.com/oasisprotocol/cli/build/rofl" |
| 13 | + "github.com/oasisprotocol/cli/cmd/common" |
| 14 | + roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common" |
| 15 | + cliConfig "github.com/oasisprotocol/cli/config" |
| 16 | +) |
| 17 | + |
| 18 | +var setAdminCmd = &cobra.Command{ |
| 19 | + Use: "set-admin <new-admin>", |
| 20 | + Short: "Change the administrator of the application in ROFL", |
| 21 | + Aliases: []string{"change-admin"}, |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + Run: func(_ *cobra.Command, args []string) { |
| 24 | + txCfg := common.GetTransactionConfig() |
| 25 | + |
| 26 | + manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{ |
| 27 | + NeedAppID: true, |
| 28 | + NeedAdmin: true, |
| 29 | + }) |
| 30 | + |
| 31 | + var appID rofl.AppID |
| 32 | + if err := appID.UnmarshalText([]byte(deployment.AppID)); err != nil { |
| 33 | + cobra.CheckErr(fmt.Errorf("malformed ROFL app ID: %w", err)) |
| 34 | + } |
| 35 | + |
| 36 | + npa.MustHaveAccount() |
| 37 | + npa.MustHaveParaTime() |
| 38 | + |
| 39 | + if deployment.Policy == nil { |
| 40 | + cobra.CheckErr("no policy configured in the manifest") |
| 41 | + } |
| 42 | + |
| 43 | + oldAdminAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, deployment.Admin) |
| 44 | + if err != nil { |
| 45 | + cobra.CheckErr(fmt.Errorf("bad current administrator address: %w", err)) |
| 46 | + } |
| 47 | + |
| 48 | + newAdminAddr, newAdminEthAddr, err := common.ResolveLocalAccountOrAddress(npa.Network, args[0]) |
| 49 | + if err != nil { |
| 50 | + cobra.CheckErr(fmt.Errorf("invalid new admin address: %w", err)) |
| 51 | + } |
| 52 | + |
| 53 | + if *oldAdminAddr == *newAdminAddr { |
| 54 | + fmt.Println("New admin is the same as the current admin, nothing to do.") |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // When not in offline mode, connect to the given network endpoint. |
| 59 | + ctx := context.Background() |
| 60 | + var conn connection.Connection |
| 61 | + if !txCfg.Offline { |
| 62 | + conn, err = connection.Connect(ctx, npa.Network) |
| 63 | + cobra.CheckErr(err) |
| 64 | + } |
| 65 | + |
| 66 | + newAdminStr := newAdminAddr.String() |
| 67 | + if newAdminEthAddr != nil { |
| 68 | + newAdminStr = newAdminEthAddr.Hex() |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Printf("App ID: %s\n", deployment.AppID) |
| 72 | + fmt.Printf("Old admin: %s\n", common.PrettyAddress(oldAdminAddr.String())) |
| 73 | + fmt.Printf("New admin: %s\n", common.PrettyAddress(newAdminStr)) |
| 74 | + |
| 75 | + secrets := buildRofl.PrepareSecrets(deployment.Secrets) |
| 76 | + |
| 77 | + tx := rofl.NewUpdateTx(nil, &rofl.Update{ |
| 78 | + ID: appID, |
| 79 | + Policy: *deployment.Policy.AsDescriptor(), |
| 80 | + Admin: newAdminAddr, |
| 81 | + Metadata: manifest.GetMetadata(roflCommon.DeploymentName), |
| 82 | + Secrets: secrets, |
| 83 | + }) |
| 84 | + |
| 85 | + acc := common.LoadAccount(cliConfig.Global(), npa.AccountName) |
| 86 | + sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil) |
| 87 | + cobra.CheckErr(err) |
| 88 | + |
| 89 | + if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, nil) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Transaction succeeded — update the manifest with the new admin. |
| 94 | + deployment.Admin = args[0] |
| 95 | + if err = manifest.Save(); err != nil { |
| 96 | + cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err)) |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Printf("ROFL app admin changed to %s.\n", common.PrettyAddress(newAdminStr)) |
| 100 | + }, |
| 101 | +} |
| 102 | + |
| 103 | +func init() { |
| 104 | + common.AddAccountFlag(setAdminCmd) |
| 105 | + setAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags) |
| 106 | + setAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
| 107 | +} |
0 commit comments