Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add account entity init command #155

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions cmd/account/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/entity"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
Expand All @@ -17,11 +19,56 @@ import (
)

var (
entityOutputFile string

entityCmd = &cobra.Command{
Use: "entity",
Short: "Entity management in the network's registry",
}

entityInitCmd = &cobra.Command{
Use: "init",
Short: "Init an empty entity file",
Run: func(cmd *cobra.Command, args []string) {
cfg := cliConfig.Global()
npa := common.GetNPASelection(cfg)

if npa.Account == nil {
cobra.CheckErr("no accounts configured in your wallet")
}

// Load the account and ensure it corresponds to the entity.
acc := common.LoadAccount(cfg, npa.AccountName)
signer := acc.ConsensusSigner()
if signer == nil {
cobra.CheckErr(fmt.Errorf("account '%s' does not support signing consensus transactions", npa.AccountName))
}

descriptor := entity.Entity{
Versioned: cbor.NewVersioned(entity.LatestDescriptorVersion),
ID: signer.Public(),
Nodes: []signature.PublicKey{},
}

// Marshal to JSON, add explicit "nodes" field to hint user.
descriptorStr, err := json.Marshal(descriptor)
cobra.CheckErr(err)
var rawJSON map[string]interface{}
err = json.Unmarshal(descriptorStr, &rawJSON)
cobra.CheckErr(err)
rawJSON["nodes"] = []string{}
descriptorStr, err = common.PrettyJSONMarshal(rawJSON)
cobra.CheckErr(err)

if entityOutputFile == "" {
fmt.Printf("%s\n", descriptorStr)
} else {
err = os.WriteFile(entityOutputFile, descriptorStr, 0o644) //nolint: gosec
cobra.CheckErr(err)
}
},
}

entityRegisterCmd = &cobra.Command{
Use: "register <entity.json>",
Short: "Register or update account entity in registry",
Expand Down Expand Up @@ -118,12 +165,17 @@ var (
)

func init() {
entityInitCmd.Flags().StringVarP(&entityOutputFile, "output-file", "o", "", "output entity descriptor into specified JSON file")
entityInitCmd.Flags().AddFlagSet(common.AccountFlag)
entityInitCmd.Flags().AddFlagSet(common.YesFlag)

entityRegisterCmd.Flags().AddFlagSet(common.SelectorNAFlags)
entityRegisterCmd.Flags().AddFlagSet(common.TxFlags)

entityDeregisterCmd.Flags().AddFlagSet(common.SelectorNAFlags)
entityDeregisterCmd.Flags().AddFlagSet(common.TxFlags)

entityCmd.AddCommand(entityInitCmd)
entityCmd.AddCommand(entityRegisterCmd)
entityCmd.AddCommand(entityDeregisterCmd)
}
9 changes: 7 additions & 2 deletions cmd/common/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var (
)

var (
// AccountFlag corresponds to the --account selector flag.
AccountFlag *flag.FlagSet
// SelectorFlags contains the common selector flags for network/ParaTime/wallet.
SelectorFlags *flag.FlagSet
// SelectorNPFlags contains the common selector flags for network/ParaTime.
Expand Down Expand Up @@ -103,11 +105,14 @@ func (npa *NPASelection) PrettyPrintNetwork() (out string) {
}

func init() {
AccountFlag = flag.NewFlagSet("", flag.ContinueOnError)
AccountFlag.StringVar(&selectedAccount, "account", "", "explicitly set account to use")

SelectorFlags = flag.NewFlagSet("", flag.ContinueOnError)
SelectorFlags.StringVar(&selectedNetwork, "network", "", "explicitly set network to use")
SelectorFlags.StringVar(&selectedParaTime, "paratime", "", "explicitly set ParaTime to use")
SelectorFlags.BoolVar(&noParaTime, "no-paratime", false, "explicitly set that no ParaTime should be used")
SelectorFlags.StringVar(&selectedAccount, "account", "", "explicitly set account to use")
SelectorFlags.AddFlagSet(AccountFlag)

SelectorNPFlags = flag.NewFlagSet("", flag.ContinueOnError)
SelectorNPFlags.StringVar(&selectedNetwork, "network", "", "explicitly set network to use")
Expand All @@ -116,7 +121,7 @@ func init() {

SelectorNAFlags = flag.NewFlagSet("", flag.ContinueOnError)
SelectorNAFlags.StringVar(&selectedNetwork, "network", "", "explicitly set network to use")
SelectorNAFlags.StringVar(&selectedAccount, "account", "", "explicitly set account to use")
SelectorNAFlags.AddFlagSet(AccountFlag)

SelectorNFlags = flag.NewFlagSet("", flag.ContinueOnError)
SelectorNFlags.StringVar(&selectedNetwork, "network", "", "explicitly set network to use")
Expand Down
10 changes: 8 additions & 2 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (
)

var (
// YesFlag corresponds to the yes-to-all flag.
YesFlag *flag.FlagSet

// TxFlags contains the common consensus transaction flags.
TxFlags *flag.FlagSet

Expand Down Expand Up @@ -564,13 +567,16 @@ func WaitForEvent(
}

func init() {
YesFlag = flag.NewFlagSet("", flag.ContinueOnError)
YesFlag.BoolVarP(&txYes, "yes", "y", false, "answer yes to all questions")

RuntimeTxFlags = flag.NewFlagSet("", flag.ContinueOnError)
RuntimeTxFlags.BoolVar(&txOffline, "offline", false, "do not perform any operations requiring network access")
RuntimeTxFlags.Uint64Var(&txNonce, "nonce", invalidNonce, "override nonce to use")
RuntimeTxFlags.Uint64Var(&txGasLimit, "gas-limit", invalidGasLimit, "override gas limit to use (disable estimation)")
RuntimeTxFlags.StringVar(&txGasPrice, "gas-price", "", "override gas price to use")
RuntimeTxFlags.BoolVar(&txEncrypted, "encrypted", false, "encrypt transaction call data (requires online mode)")
RuntimeTxFlags.BoolVarP(&txYes, "yes", "y", false, "answer yes to all questions")
RuntimeTxFlags.AddFlagSet(YesFlag)
RuntimeTxFlags.BoolVar(&txUnsigned, "unsigned", false, "do not sign transaction")
RuntimeTxFlags.StringVar(&txFormat, "format", "json", "transaction output format (for offline/unsigned modes) [json, cbor]")
RuntimeTxFlags.StringVarP(&txOutputFile, "output-file", "o", "", "output transaction into specified file instead of broadcasting")
Expand All @@ -580,7 +586,7 @@ func init() {
TxFlags.Uint64Var(&txNonce, "nonce", invalidNonce, "override nonce to use")
TxFlags.Uint64Var(&txGasLimit, "gas-limit", invalidGasLimit, "override gas limit to use (disable estimation)")
TxFlags.StringVar(&txGasPrice, "gas-price", "", "override gas price to use")
TxFlags.BoolVarP(&txYes, "yes", "y", false, "answer yes to all questions")
TxFlags.AddFlagSet(YesFlag)
TxFlags.BoolVar(&txUnsigned, "unsigned", false, "do not sign transaction")
TxFlags.StringVar(&txFormat, "format", "json", "transaction output format (for offline/unsigned modes) [json, cbor]")
TxFlags.StringVarP(&txOutputFile, "output-file", "o", "", "output transaction into specified file instead of broadcasting")
Expand Down
23 changes: 23 additions & 0 deletions docs/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,29 @@ transaction.

### Entity Management {#entity}

#### Initialize Entity {#entity-init}

When setting up a validator node for the first time, you will need to provide
the path to the file containing your entity descriptor as well as register it in
the network registry. Use `account entity init` to generate the entity
descriptor file containing the public key of the selected account.

![code shell](../examples/account/entity-init.in.static)

![code json](../examples/account/entity-init.out.static)

By default, the file content will be printed to the standard output. You can use
`-o` parameter to store it to a file, for example:

![code shell](../examples/account/entity-init-o.y.in)

:::info

[Account](#account) selector is available for the
`account entity init` command.

:::

#### Register your Entity {#entity-register}

In order for validators to become part of the validator set and/or the compute
Expand Down
2 changes: 1 addition & 1 deletion examples/account/allow-paratime.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.Allow
Body:
Beneficiary: oasis1qqczuf3x6glkgjuf0xgtcpjjw95r3crf7y2323xd
Amount change: +10.0 TEST
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1278
Expand Down
2 changes: 1 addition & 1 deletion examples/account/allow.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.Allow
Body:
Beneficiary: oasis1qpl4axynedmdrrgrg7dpw3yxc4a8crevr5dkuksl
Amount change: +10.0 TEST
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1278
Expand Down
2 changes: 1 addition & 1 deletion examples/account/amend-commission-schedule.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Body:
(2) start: epoch 335000
minimum rate: 0.9%
maximum rate: 1.9%
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1361
Expand Down
2 changes: 1 addition & 1 deletion examples/account/burn.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ You are about to sign the following transaction:
Method: staking.Burn
Body:
Amount: 2.5 TEST
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1235
Expand Down
2 changes: 1 addition & 1 deletion examples/account/delegate.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.AddEscrow
Body:
To: oasis1qpkl3vykn9mf4xcq9eevmey4ffrzf0ajtcpvd7sk
Amount: 20.0 TEST
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1271
Expand Down
2 changes: 1 addition & 1 deletion examples/account/entity-deregister.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ You are about to sign the following transaction:
Method: registry.DeregisterEntity
Body:
{}
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1231
Expand Down
1 change: 1 addition & 0 deletions examples/account/entity-init-o.y.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis account entity init -o entity.json
Empty file.
1 change: 1 addition & 0 deletions examples/account/entity-init.in.static
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oasis account entity init
5 changes: 5 additions & 0 deletions examples/account/entity-init.out.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "Bx6gOixnxy15tCs09ua5DcKyX9uo2Forb32O6Hyjoc8=",
"nodes": [],
"v": 2
}
2 changes: 1 addition & 1 deletion examples/account/entity-register.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Body:
"signature": "DAwn+N8hKmQMbZda/fFJSEgErDAAdebXLfIPOpqUkJowJLUAL+nfrUMz5SVkKc0TnqQOavoSAVFz1yoRJ3QuBA=="
}
}
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 2471
Expand Down
2 changes: 1 addition & 1 deletion examples/account/node-unfreeze.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Body:
{
"node_id": "fasTG3pMOwLfFA7JX3R8Kxw1zFflqeY6NP/cpjcFu5I="
}
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1274
Expand Down
2 changes: 1 addition & 1 deletion examples/account/show.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Address: oasis1qp87hflmelnpqhzcqcw8rhzakq4elj7jzv090p3e

=== CONSENSUS LAYER (testnet) ===
Nonce: 1
Nonce: 2

Total: 0.0 TEST
Available: 0.0 TEST
Expand Down
2 changes: 1 addition & 1 deletion examples/account/undelegate.y.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Method: staking.ReclaimEscrow
Body:
From: oasis1qpkl3vykn9mf4xcq9eevmey4ffrzf0ajtcpvd7sk
Shares: 20000000000
Nonce: 1
Nonce: 2
Fee:
Amount: 0.0 TEST
Gas limit: 1275
Expand Down
Loading