diff --git a/cmd/chain/genesis/cmd.go b/cmd/chain/genesis/cmd.go index c79f52b..c36507c 100644 --- a/cmd/chain/genesis/cmd.go +++ b/cmd/chain/genesis/cmd.go @@ -4,12 +4,19 @@ package genesis import ( + "errors" + "fmt" "os" - "github.com/ava-labs/xsvm/genesis" "github.com/spf13/cobra" + + "github.com/ava-labs/avalanchego/utils/formatting" + + "github.com/ava-labs/xsvm/genesis" ) +var errUnknownEncoding = errors.New("unknown encoding") + func Command() *cobra.Command { c := &cobra.Command{ Use: "genesis", @@ -23,16 +30,28 @@ func Command() *cobra.Command { func genesisFunc(c *cobra.Command, args []string) error { flags := c.Flags() - g, err := ParseFlags(flags, args) + config, err := ParseFlags(flags, args) if err != nil { return err } - genesisBytes, err := genesis.Codec.Marshal(genesis.Version, g) + genesisBytes, err := genesis.Codec.Marshal(genesis.Version, config.Genesis) if err != nil { return err } - _, err = os.Stdout.Write(genesisBytes) - return err + switch config.Encoding { + case binaryEncoding: + _, err = os.Stdout.Write(genesisBytes) + return err + case hexEncoding: + encoded, err := formatting.Encode(formatting.Hex, genesisBytes) + if err != nil { + return err + } + _, err = fmt.Println(encoded) + return err + default: + return fmt.Errorf("%w: %q", errUnknownEncoding, config.Encoding) + } } diff --git a/cmd/chain/genesis/flags.go b/cmd/chain/genesis/flags.go index 600a68d..62c989f 100644 --- a/cmd/chain/genesis/flags.go +++ b/cmd/chain/genesis/flags.go @@ -4,6 +4,7 @@ package genesis import ( + "fmt" "math" "time" @@ -16,22 +17,28 @@ import ( ) const ( - TimeKey = "time" - AddressKey = "address" - BalanceKey = "balance" + TimeKey = "time" + AddressKey = "address" + BalanceKey = "balance" + EncodingKey = "encoding" + + binaryEncoding = "binary" + hexEncoding = "hex" ) func AddFlags(flags *pflag.FlagSet) { flags.Int64(TimeKey, time.Now().Unix(), "Unix timestamp to include in the genesis") flags.String(AddressKey, genesis.EWOQKey.Address().String(), "Address to fund in the genesis") flags.Uint64(BalanceKey, math.MaxUint64, "Amount to provide the funded address in the genesis") + flags.String(EncodingKey, hexEncoding, fmt.Sprintf("Encoding to use for the genesis. Available values: %s or %s", hexEncoding, binaryEncoding)) } -func ParseFlags(flags *pflag.FlagSet, args []string) (*xsgenesis.Genesis, error) { - if err := flags.Parse(args); err != nil { - return nil, err - } +type Config struct { + Genesis *xsgenesis.Genesis + Encoding string +} +func ParseFlags(flags *pflag.FlagSet, args []string) (*Config, error) { if err := flags.Parse(args); err != nil { return nil, err } @@ -56,13 +63,21 @@ func ParseFlags(flags *pflag.FlagSet, args []string) (*xsgenesis.Genesis, error) return nil, err } - return &xsgenesis.Genesis{ - Timestamp: timestamp, - Allocations: []xsgenesis.Allocation{ - { - Address: addr, - Balance: balance, + encoding, err := flags.GetString(EncodingKey) + if err != nil { + return nil, err + } + + return &Config{ + Genesis: &xsgenesis.Genesis{ + Timestamp: timestamp, + Allocations: []xsgenesis.Allocation{ + { + Address: addr, + Balance: balance, + }, }, }, + Encoding: encoding, }, nil }