From d7db0042ba18d342f92ed160fd8f18659dbb7186 Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Fri, 19 Jul 2024 02:01:17 +0300 Subject: [PATCH] feat: improve rollapp initialization (#817) --- cmd/config/export/bech32.go | 4 +- cmd/config/init/flags.go | 11 +++- cmd/config/init/format.go | 6 +- cmd/config/init/genesis.go | 1 - cmd/config/init/keys.go | 54 ++++++++------- cmd/config/init/output.go | 16 ++++- cmd/config/set/da.go | 15 +++-- cmd/consts/consts.go | 14 +++- cmd/eibc/utils.go | 2 +- cmd/keys/list/list.go | 47 +++++++------ cmd/migrate/v1_0_05.go | 2 +- cmd/rollapp/init/main.go | 2 +- cmd/rollapp/init/utils.go | 26 +++++--- cmd/tx/claim/claim.go | 2 +- cmd/tx/fund_faucet/fund_faucet.go | 3 +- cmd/utils/balance.go | 1 + cmd/utils/fetch_accounts_data.go | 6 +- cmd/utils/keys.go | 106 +++++++++++++++++------------- data_layer/celestia/celestia.go | 44 ++++++++++--- data_layer/da_layer.go | 9 ++- data_layer/damock/damock.go | 8 +-- relayer/config.go | 2 + relayer/create_ibc_channel.go | 5 +- 23 files changed, 240 insertions(+), 146 deletions(-) diff --git a/cmd/config/export/bech32.go b/cmd/config/export/bech32.go index 5263219f..84fa50c8 100644 --- a/cmd/config/export/bech32.go +++ b/cmd/config/export/bech32.go @@ -10,12 +10,12 @@ import ( ) func getBech32Prefix(rlpCfg config.RollappConfig) (string, error) { - rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ + rollappSeqAddrInfo, err := utils.GetAddressBinary(utils.KeyConfig{ Dir: filepath.Join(rlpCfg.Home, consts.ConfigDirName.Rollapp), ID: consts.KeysIds.RollappSequencer, }, rlpCfg.RollappBinary) if err != nil { return "", err } - return strings.Split(rollappSeqAddr, "1")[0], nil + return strings.Split(rollappSeqAddrInfo.Address, "1")[0], nil } diff --git a/cmd/config/init/flags.go b/cmd/config/init/flags.go index d1f1f32c..f2d7efef 100644 --- a/cmd/config/init/flags.go +++ b/cmd/config/init/flags.go @@ -16,6 +16,7 @@ import ( func AddFlags(cmd *cobra.Command) error { cmd.Flags(). StringP(FlagNames.HubID, "", consts.LocalHubName, fmt.Sprintf("The ID of the Dymension hub. %s", getAvailableHubsMessage())) + cmd.Flags(). StringP(FlagNames.RollappBinary, "", consts.Executables.RollappEVM, "The rollapp binary. Should be passed only if you built a custom rollapp") cmd.Flags(). @@ -41,6 +42,7 @@ func AddFlags(cmd *cobra.Command) error { func GetInitConfig( initCmd *cobra.Command, + withMockSettlement bool, ) (*config.RollappConfig, error) { var cfg config.RollappConfig rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), config.RollerConfigFileName) @@ -57,7 +59,14 @@ func GetInitConfig( // cfg.TokenSupply = initCmd.Flag(FlagNames.TokenSupply).Value.String() cfg.DA = config.DAType(strings.ToLower(string(cfg.DA))) - hubID := initCmd.Flag(FlagNames.HubID).Value.String() + var hubID string + + // TODO: hub id (and probably the rest of settlement config) should come from roller config + if withMockSettlement { + hubID = "mock" + } else { + hubID = initCmd.Flag(FlagNames.HubID).Value.String() + } hub, ok := consts.Hubs[hubID] diff --git a/cmd/config/init/format.go b/cmd/config/init/format.go index 02ed18b1..46dc351c 100644 --- a/cmd/config/init/format.go +++ b/cmd/config/init/format.go @@ -12,15 +12,15 @@ import ( func formatAddresses( rollappConfig config.RollappConfig, - addresses []utils.AddressData, -) []utils.AddressData { + addresses []utils.KeyInfo, +) []utils.KeyInfo { damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) requireFundingKeys := map[string]string{ consts.KeysIds.HubSequencer: fmt.Sprintf("Sequencer, %s Hub", rollappConfig.HubData.ID), consts.KeysIds.HubRelayer: fmt.Sprintf("Relayer, %s Hub", rollappConfig.HubData.ID), damanager.GetKeyName(): fmt.Sprintf("DA, %s Network", damanager.GetNetworkName()), } - filteredAddresses := make([]utils.AddressData, 0) + filteredAddresses := make([]utils.KeyInfo, 0) for _, address := range addresses { if newName, ok := requireFundingKeys[address.Name]; ok { address.Name = newName diff --git a/cmd/config/init/genesis.go b/cmd/config/init/genesis.go index f965defd..824fd290 100644 --- a/cmd/config/init/genesis.go +++ b/cmd/config/init/genesis.go @@ -100,7 +100,6 @@ func UpdateGenesisParams(home string) error { "test", ) - fmt.Println(addGenAccountCmd.String()) _, err = utils.ExecBashCommandWithStdout(addGenAccountCmd) if err != nil { return err diff --git a/cmd/config/init/keys.go b/cmd/config/init/keys.go index eedd90b5..51922935 100644 --- a/cmd/config/init/keys.go +++ b/cmd/config/init/keys.go @@ -10,38 +10,57 @@ import ( "github.com/dymensionxyz/roller/config" ) -func GenerateKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) { +func GenerateKeys(rollappConfig config.RollappConfig) ([]utils.KeyInfo, error) { + // var addresses []utils.KeyInfo + sequencerAddresses, err := generateSequencersKeys(rollappConfig) if err != nil { fmt.Println("failed to generate sequencerAddresses") return nil, err } + + // addresses = append(addresses, sequencerAddresses...) + // relayerAddresses, err := generateRelayerKeys(rollappConfig) // if err != nil { // return nil, err // } + // addresses = append(addresses, relayerAddresses...) + return sequencerAddresses, nil } -func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.AddressData, error) { +func generateSequencersKeys(initConfig config.RollappConfig) ([]utils.KeyInfo, error) { keys := getSequencerKeysConfig(initConfig) - addresses := make([]utils.AddressData, 0) + addresses := make([]utils.KeyInfo, 0) for _, key := range keys { - var address string + var address *utils.KeyInfo var err error address, err = CreateAddressBinary(key, initConfig.Home) if err != nil { return nil, err } - addresses = append(addresses, utils.AddressData{ - Addr: address, - Name: key.ID, + addresses = append(addresses, utils.KeyInfo{ + Address: address.Address, + Name: key.ID, + Mnemonic: address.Mnemonic, }) } return addresses, nil } func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfig { + fmt.Println(rollappConfig.HubData) + if rollappConfig.HubData.ID == consts.MockHubID { + return []utils.KeyConfig{ + { + Dir: consts.ConfigDirName.Rollapp, + ID: consts.KeysIds.RollappSequencer, + ChainBinary: rollappConfig.RollappBinary, + Type: rollappConfig.VMType, + }, + } + } return []utils.KeyConfig{ { Dir: consts.ConfigDirName.HubKeys, @@ -76,36 +95,21 @@ func getSequencerKeysConfig(rollappConfig config.RollappConfig) []utils.KeyConfi // } // } -func CreateAddressBinary(keyConfig utils.KeyConfig, home string) (string, error) { - args := []string{ - "keys", "add", keyConfig.ID, "--keyring-backend", "test", - "--keyring-dir", filepath.Join(home, keyConfig.Dir), - "--output", "json", - } - createKeyCommand := exec.Command(keyConfig.ChainBinary, args...) - out, err := utils.ExecBashCommandWithStdout(createKeyCommand) - if err != nil { - return "", err - } - return utils.ParseAddressFromOutput(out) -} - -func CreateAddressBinaryWithSensitiveOutput( +func CreateAddressBinary( keyConfig utils.KeyConfig, home string, -) (*utils.SensitiveKeyInfo, error) { +) (*utils.KeyInfo, error) { args := []string{ "keys", "add", keyConfig.ID, "--keyring-backend", "test", "--keyring-dir", filepath.Join(home, keyConfig.Dir), "--output", "json", } createKeyCommand := exec.Command(keyConfig.ChainBinary, args...) - fmt.Println(createKeyCommand.String()) out, err := utils.ExecBashCommandWithStdout(createKeyCommand) if err != nil { return nil, err } - return utils.ParseAddressFromOutputWithSensisiveOutput(out) + return utils.ParseAddressFromOutput(out) } // func generateRelayerKeys(rollappConfig config.RollappConfig) ([]utils.AddressData, error) { diff --git a/cmd/config/init/output.go b/cmd/config/init/output.go index 5a1753a6..214fdacc 100644 --- a/cmd/config/init/output.go +++ b/cmd/config/init/output.go @@ -3,6 +3,9 @@ package initconfig import ( "fmt" + "github.com/pterm/pterm" + + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" ) @@ -19,7 +22,7 @@ func NewOutputHandler(noOutput bool) *OutputHandler { func (o *OutputHandler) PrintInitOutput( rollappConfig config.RollappConfig, - addresses []utils.AddressData, + addresses []utils.KeyInfo, rollappId string, ) { if o.NoOutput { @@ -31,9 +34,16 @@ func (o *OutputHandler) PrintInitOutput( ) fmt.Println(FormatTokenSupplyLine(rollappConfig)) fmt.Println() - utils.PrintAddressesWithTitle(formatAddresses(rollappConfig, addresses)) + utils.PrintAddressesWithTitle(addresses) - fmt.Printf("\nšŸ”” Please fund these addresses to register and run the rollapp.\n") + if rollappConfig.HubData.ID != consts.MockHubID { + pterm.DefaultSection.WithIndentCharacter("šŸ””"). + Println("Please fund the addresses below to register and run the rollapp.") + fa := formatAddresses(rollappConfig, addresses) + for _, v := range fa { + v.Print(utils.WithName()) + } + } } func (o *OutputHandler) PromptOverwriteConfig(home string) (bool, error) { diff --git a/cmd/config/set/da.go b/cmd/config/set/da.go index 09286160..76dd0810 100644 --- a/cmd/config/set/da.go +++ b/cmd/config/set/da.go @@ -42,7 +42,8 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error { return err } daManager := datalayer.NewDAManager(newDa, rlpCfg.Home) - if err := daManager.InitializeLightNodeConfig(); err != nil { + _, err = daManager.InitializeLightNodeConfig() + if err != nil { return err } rlpCfg.DA = newDa @@ -54,18 +55,18 @@ func updateDaConfig(rlpCfg config.RollappConfig, newDa config.DAType) error { } fmt.Printf("šŸ’ˆ RollApp DA has been successfully set to '%s'\n\n", newDa) if newDa != config.Local { - addresses := make([]utils.AddressData, 0) + addresses := make([]utils.KeyInfo, 0) damanager := datalayer.NewDAManager(newDa, rlpCfg.Home) daAddress, err := damanager.GetDAAccountAddress() if err != nil { return err } - addresses = append(addresses, utils.AddressData{ - Name: damanager.GetKeyName(), - Addr: daAddress, + addresses = append(addresses, utils.KeyInfo{ + Name: damanager.GetKeyName(), + Address: daAddress.Address, }) - fmt.Printf("šŸ”‘ Address:\n\n") - utils.PrintAddresses(addresses) + + utils.PrintAddressesWithTitle(addresses) fmt.Printf("\nšŸ”” Please fund this address to run the DA light client.\n") } return nil diff --git a/cmd/consts/consts.go b/cmd/consts/consts.go index 769dae53..497fb240 100644 --- a/cmd/consts/consts.go +++ b/cmd/consts/consts.go @@ -40,7 +40,7 @@ var KeysIds = struct { HubRelayer string }{ HubSequencer: "hub_sequencer", - RollappSequencer: "rollapp_sequencer", + RollappSequencer: "rollapp_genesis_account", RollappRelayer: "relayer-rollapp-key", HubRelayer: "relayer-hub-key", } @@ -116,20 +116,32 @@ var LocalHubData = config.HubData{ SEQ_MIN_BOND: "100dym", } +var MockHubData = config.HubData{ + API_URL: "", + ID: MockHubID, + RPC_URL: "", + ARCHIVE_RPC_URL: "", + GAS_PRICE: "", + SEQ_MIN_BOND: "", +} + // TODO(#112): The available hub networks should be read from YAML file var Hubs = map[string]config.HubData{ + MockHubName: MockHubData, LocalHubName: LocalHubData, TestnetHubName: TestnetHubData, MainnetHubName: MainnetHubData, } const ( + MockHubName = "mock" LocalHubName = "local" TestnetHubName = "testnet" MainnetHubName = "mainnet" ) const ( + MockHubID = "mock" LocalHubID = "dymension_100-1" TestnetHubID = "blumbus_111-1" MainnetHubID = "dymension_1100-1" diff --git a/cmd/eibc/utils.go b/cmd/eibc/utils.go index be4dda78..e8bb121d 100644 --- a/cmd/eibc/utils.go +++ b/cmd/eibc/utils.go @@ -27,7 +27,7 @@ func ensureWhaleAccount() error { _, err := utils.GetAddressBinary(kc, consts.Executables.Dymension) if err != nil { fmt.Println("whale account not found in the keyring, creating it now") - addressInfo, err := initconfig.CreateAddressBinaryWithSensitiveOutput(kc, home) + addressInfo, err := initconfig.CreateAddressBinary(kc, home) if err != nil { return err } diff --git a/cmd/keys/list/list.go b/cmd/keys/list/list.go index 4413ffed..013ca072 100644 --- a/cmd/keys/list/list.go +++ b/cmd/keys/list/list.go @@ -27,49 +27,54 @@ func Cmd() *cobra.Command { home := cmd.Flag(utils.FlagNames.Home).Value.String() rollappConfig, err := config.LoadConfigFromTOML(home) utils.PrettifyErrorIfExists(err) - addresses := make([]utils.AddressData, 0) + addresses := make([]utils.KeyInfo, 0) damanager := datalayer.NewDAManager(rollappConfig.DA, rollappConfig.Home) daAddr, err := damanager.DataLayer.GetDAAccountAddress() utils.PrettifyErrorIfExists(err) - if daAddr != "" { - addresses = append(addresses, utils.AddressData{ - Addr: daAddr, - Name: damanager.GetKeyName(), + if daAddr != nil { + addresses = append(addresses, utils.KeyInfo{ + Address: daAddr.Address, + Name: damanager.GetKeyName(), }) } - hubSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ + + hubSeqInfo, err := utils.GetAddressBinary(utils.KeyConfig{ Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.HubKeys), ID: consts.KeysIds.HubSequencer, }, consts.Executables.Dymension) utils.PrettifyErrorIfExists(err) - addresses = append(addresses, utils.AddressData{ - Addr: hubSeqAddr, - Name: consts.KeysIds.HubSequencer, + addresses = append(addresses, utils.KeyInfo{ + Address: hubSeqInfo.Address, + Name: consts.KeysIds.HubSequencer, }) - rollappSeqAddr, err := utils.GetAddressBinary(utils.KeyConfig{ + + raSeqInfo, err := utils.GetAddressBinary(utils.KeyConfig{ Dir: filepath.Join(rollappConfig.Home, consts.ConfigDirName.Rollapp), ID: consts.KeysIds.RollappSequencer, }, rollappConfig.RollappBinary) utils.PrettifyErrorIfExists(err) - addresses = append(addresses, utils.AddressData{ - Addr: rollappSeqAddr, - Name: consts.KeysIds.RollappSequencer, + addresses = append(addresses, utils.KeyInfo{ + Address: raSeqInfo.Address, + Name: consts.KeysIds.RollappSequencer, }) + hubRlyAddr, err := utils.GetRelayerAddress(rollappConfig.Home, rollappConfig.HubData.ID) utils.PrettifyErrorIfExists(err) - addresses = append(addresses, utils.AddressData{ - Addr: hubRlyAddr, - Name: consts.KeysIds.HubRelayer, + addresses = append(addresses, utils.KeyInfo{ + Address: hubRlyAddr, + Name: consts.KeysIds.HubRelayer, }) + rollappRlyAddr, err := utils.GetRelayerAddress( rollappConfig.Home, rollappConfig.RollappID, ) utils.PrettifyErrorIfExists(err) - addresses = append(addresses, utils.AddressData{ - Addr: rollappRlyAddr, - Name: consts.KeysIds.RollappRelayer, + + addresses = append(addresses, utils.KeyInfo{ + Address: rollappRlyAddr, + Name: consts.KeysIds.RollappRelayer, }) outputType := cmd.Flag(flagNames.outputType).Value.String() if outputType == "json" { @@ -83,10 +88,10 @@ func Cmd() *cobra.Command { return cmd } -func printAsJSON(addresses []utils.AddressData) error { +func printAsJSON(addresses []utils.KeyInfo) error { addrMap := make(map[string]string) for _, addrData := range addresses { - addrMap[addrData.Name] = addrData.Addr + addrMap[addrData.Name] = addrData.Address } data, err := json.MarshalIndent(addrMap, "", " ") if err != nil { diff --git a/cmd/migrate/v1_0_05.go b/cmd/migrate/v1_0_05.go index 3b8afbca..fbc082fb 100644 --- a/cmd/migrate/v1_0_05.go +++ b/cmd/migrate/v1_0_05.go @@ -46,7 +46,7 @@ func (v *VersionMigratorV1005) PerformMigration(rlpCfg config.RollappConfig) err } // re-init the light node and ask the user to fund the address celestiaClient := celestia.NewCelestia(rlpCfg.Home) - err := celestiaClient.InitializeLightNodeConfig() + _, err := celestiaClient.InitializeLightNodeConfig() if err != nil { return err } diff --git a/cmd/rollapp/init/main.go b/cmd/rollapp/init/main.go index 29ca6ece..df4fff6a 100644 --- a/cmd/rollapp/init/main.go +++ b/cmd/rollapp/init/main.go @@ -44,7 +44,7 @@ var Cmd = &cobra.Command{ isMockBackend := backend == "mock" if isMockBackend { - err := runInit(cmd) + err := runInit(cmd, WithMockSettlement()) if err != nil { fmt.Println("failed to run init: ", err) return diff --git a/cmd/rollapp/init/utils.go b/cmd/rollapp/init/utils.go index e061258e..d84f4104 100644 --- a/cmd/rollapp/init/utils.go +++ b/cmd/rollapp/init/utils.go @@ -1,6 +1,7 @@ package initrollapp import ( + "encoding/json" "errors" "fmt" "os" @@ -21,6 +22,7 @@ import ( type Options struct { configArchivePath string + useMockSettlement bool } type Option func(*Options) @@ -31,6 +33,12 @@ func WithConfig(configArchivePath string) Option { } } +func WithMockSettlement() Option { + return func(o *Options) { + o.useMockSettlement = true + } +} + func defaultOptions() Options { return Options{ configArchivePath: "", @@ -93,16 +101,14 @@ func runInit(cmd *cobra.Command, opts ...Option) error { return err } } else { - // do something config := NewMockRollerConfig() err := WriteMockRollerconfigToFile(config) if err != nil { return err } - } - initConfigPtr, err := initconfig.GetInitConfig(cmd) + initConfigPtr, err := initconfig.GetInitConfig(cmd, options.useMockSettlement) if err != nil { utils.PrettifyErrorIfExists(err) return err @@ -152,7 +158,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error { /* ------------------------ Initialize DA light node ------------------------ */ damanager := datalayer.NewDAManager(initConfig.DA, initConfig.Home) - err = damanager.InitializeLightNodeConfig() + mnemonic, err := damanager.InitializeLightNodeConfig() if err != nil { return err } @@ -162,10 +168,11 @@ func runInit(cmd *cobra.Command, opts ...Option) error { return err } - if daAddress != "" { - addresses = append(addresses, utils.AddressData{ - Name: damanager.GetKeyName(), - Addr: daAddress, + if daAddress != nil { + addresses = append(addresses, utils.KeyInfo{ + Name: damanager.GetKeyName(), + Address: daAddress.Address, + Mnemonic: mnemonic, }) } @@ -222,6 +229,9 @@ func runInit(cmd *cobra.Command, opts ...Option) error { // } /* ------------------------------ Print output ------------------------------ */ + + j, _ := json.MarshalIndent(addresses, "", " ") + fmt.Println(string(j)) outputHandler.StopSpinner() outputHandler.PrintInitOutput(initConfig, addresses, initConfig.RollappID) diff --git a/cmd/tx/claim/claim.go b/cmd/tx/claim/claim.go index c8f6c13a..208693bc 100644 --- a/cmd/tx/claim/claim.go +++ b/cmd/tx/claim/claim.go @@ -51,7 +51,7 @@ func Cmd() *cobra.Command { Binary: consts.Executables.Dymension, Denom: consts.Denoms.Hub, RPC: mainnetHub.RPC_URL, - }, sequencerAddr) + }, sequencerAddr.Address) if err != nil { return err } diff --git a/cmd/tx/fund_faucet/fund_faucet.go b/cmd/tx/fund_faucet/fund_faucet.go index 6182a062..9f37688f 100644 --- a/cmd/tx/fund_faucet/fund_faucet.go +++ b/cmd/tx/fund_faucet/fund_faucet.go @@ -7,12 +7,13 @@ import ( "os/exec" "path/filepath" + "github.com/spf13/cobra" + "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/tx/tx_utils" "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" "github.com/dymensionxyz/roller/relayer" - "github.com/spf13/cobra" ) var flagNames = struct { diff --git a/cmd/utils/balance.go b/cmd/utils/balance.go index dfef8e93..30ac30ee 100644 --- a/cmd/utils/balance.go +++ b/cmd/utils/balance.go @@ -113,6 +113,7 @@ type BalanceResp struct { Denom string `json:"denom"` Amount string `json:"amount"` } + type BalanceResponse struct { Balances []BalanceResp `json:"balances"` } diff --git a/cmd/utils/fetch_accounts_data.go b/cmd/utils/fetch_accounts_data.go index e5a6ad6e..636a9961 100644 --- a/cmd/utils/fetch_accounts_data.go +++ b/cmd/utils/fetch_accounts_data.go @@ -8,7 +8,7 @@ import ( ) func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) { - sequencerAddress, err := GetAddressBinary(KeyConfig{ + seqAddrInfo, err := GetAddressBinary(KeyConfig{ ID: consts.KeysIds.HubSequencer, Dir: filepath.Join(cfg.Home, consts.ConfigDirName.HubKeys), }, consts.Executables.Dymension) @@ -20,13 +20,13 @@ func GetSequencerData(cfg config.RollappConfig) ([]AccountData, error) { Binary: consts.Executables.Dymension, Denom: consts.Denoms.Hub, RPC: cfg.HubData.RPC_URL, - }, sequencerAddress) + }, seqAddrInfo.Address) if err != nil { return nil, err } return []AccountData{ { - Address: sequencerAddress, + Address: seqAddrInfo.Address, Balance: sequencerBalance, }, }, nil diff --git a/cmd/utils/keys.go b/cmd/utils/keys.go index 2f8bb8f6..365f74e4 100644 --- a/cmd/utils/keys.go +++ b/cmd/utils/keys.go @@ -4,41 +4,73 @@ import ( "bytes" "encoding/json" "fmt" - "os" "os/exec" "path/filepath" "strings" - "github.com/olekukonko/tablewriter" + "github.com/pterm/pterm" "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/config" ) +// KeyConfig struct store information about a wallet +// Dir refers to the keyringDir where the key is created +type KeyConfig struct { + Dir string + // TODO: this is not descriptive, Name would be more expressive + ID string + ChainBinary string + Type config.VMType +} + // TODO: KeyInfo and AddressData seem redundant, should be moved into // location + +// KeyInfo struct stores information about a generated wallet type KeyInfo struct { - Address string `json:"address"` + Name string `json:"name"` + Address string `json:"address"` + Mnemonic string `json:"mnemonic"` + PrintName bool + PrintMnemonic bool } -func ParseAddressFromOutput(output bytes.Buffer) (string, error) { - key := &KeyInfo{} - err := json.Unmarshal(output.Bytes(), key) - if err != nil { - return "", err +type KeyInfoOption func(*KeyInfo) + +func WithName() KeyInfoOption { + return func(opts *KeyInfo) { + opts.PrintName = true } - return key.Address, nil } -type SensitiveKeyInfo struct { - Name string `json:"name"` - Address string `json:"address"` - Mnemonic string `json:"mnemonic"` +func WithMnemonic() KeyInfoOption { + return func(opts *KeyInfo) { + opts.PrintMnemonic = true + } } -// TODO: the entire sensitive output thing could be simpler -func ParseAddressFromOutputWithSensisiveOutput(output bytes.Buffer) (*SensitiveKeyInfo, error) { - key := &SensitiveKeyInfo{} +func (ki *KeyInfo) Print(o ...KeyInfoOption) { + for _, opt := range o { + opt(ki) + } + + if ki.PrintName { + pterm.DefaultBasicText.Println(pterm.LightGreen(ki.Name)) + } + + fmt.Printf("\t%s\n", ki.Address) + + if ki.PrintMnemonic { + fmt.Printf("\t%s\n", ki.Mnemonic) + fmt.Println(pterm.LightYellow("save the information and keep it safe")) + } + + fmt.Println() +} + +func ParseAddressFromOutput(output bytes.Buffer) (*KeyInfo, error) { + key := &KeyInfo{} err := json.Unmarshal(output.Bytes(), key) if err != nil { return nil, err @@ -46,17 +78,7 @@ func ParseAddressFromOutputWithSensisiveOutput(output bytes.Buffer) (*SensitiveK return key, nil } -// KeyConfig struct store information about a wallet -// Dir refers to the keyringDir where the key is created -type KeyConfig struct { - Dir string - // TODO: this is not descriptive, Name would be more expressive - ID string - ChainBinary string - Type config.VMType -} - -func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (string, error) { +func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (*KeyInfo, error) { showKeyCommand := exec.Command( binaryPath, "keys", @@ -71,7 +93,7 @@ func GetAddressBinary(keyConfig KeyConfig, binaryPath string) (string, error) { ) output, err := ExecBashCommandWithStdout(showKeyCommand) if err != nil { - return "", err + return nil, err } return ParseAddressFromOutput(output) } @@ -94,35 +116,27 @@ type AddressData struct { Addr string } -// TODO: refactor into options, with title and with mnemonic -func PrintAddressesWithTitle(addresses []AddressData) { - fmt.Printf("šŸ”‘ Addresses:\n\n") - PrintAddresses(addresses) +// TODO: refactor into options, with title +func PrintAddressesWithTitle(addresses []KeyInfo) { + pterm.DefaultSection.WithIndentCharacter("šŸ”‘").Println("Addresses") + for _, address := range addresses { + address.Print(WithMnemonic(), WithName()) + } } +// TODO: remove this function as it's redundant to PrintAddressesWithTitle func PrintSecretAddressesWithTitle(addresses []SecretAddressData) { - fmt.Printf("šŸ”‘ Addresses:\n\n") + fmt.Printf("šŸ”‘ Addresses:\n") PrintSecretAddresses(addresses) } -func PrintAddresses(addresses []AddressData) { - data := make([][]string, 0, len(addresses)) - for _, address := range addresses { - data = append(data, []string{address.Name, address.Addr}) - } - table := tablewriter.NewWriter(os.Stdout) - table.SetAlignment(tablewriter.ALIGN_LEFT) - table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) - table.SetBorder(false) - table.AppendBulk(data) - table.Render() -} - +// TODO: remove this struct as it's redundant to KeyInfo type SecretAddressData struct { AddressData Mnemonic string } +// TODO: remove this function as it's redundant to *KeyInfo.Print func PrintSecretAddresses(addresses []SecretAddressData) { for _, address := range addresses { fmt.Println(address.AddressData.Name) diff --git a/data_layer/celestia/celestia.go b/data_layer/celestia/celestia.go index 595d3e79..8053b2ca 100644 --- a/data_layer/celestia/celestia.go +++ b/data_layer/celestia/celestia.go @@ -1,6 +1,7 @@ package celestia import ( + "bufio" "encoding/json" "fmt" "math/big" @@ -103,7 +104,7 @@ func (c *Celestia) GetLightNodeEndpoint() string { // GetDAAccountAddress implements datalayer.DataLayer. // FIXME: should be loaded once and cached -func (c *Celestia) GetDAAccountAddress() (string, error) { +func (c *Celestia) GetDAAccountAddress() (*utils.KeyInfo, error) { daKeysDir := filepath.Join(c.Root, consts.ConfigDirName.DALightNode, consts.KeysDirName) cmd := exec.Command( consts.Executables.CelKey, "show", c.GetKeyName(), "--node.type", "light", "--keyring-dir", @@ -111,23 +112,50 @@ func (c *Celestia) GetDAAccountAddress() (string, error) { ) output, err := utils.ExecBashCommandWithStdout(cmd) if err != nil { - return "", err + return nil, err } address, err := utils.ParseAddressFromOutput(output) return address, err } -func (c *Celestia) InitializeLightNodeConfig() error { +func (c *Celestia) InitializeLightNodeConfig() (string, error) { initLightNodeCmd := exec.Command(consts.Executables.Celestia, "light", "init", "--p2p.network", DefaultCelestiaNetwork, "--node.store", filepath.Join(c.Root, consts.ConfigDirName.DALightNode)) - err := initLightNodeCmd.Run() + // err := initLightNodeCmd.Run() + out, err := utils.ExecBashCommandWithStdout(initLightNodeCmd) if err != nil { - return err + return "", err + } + + mnemonic := extractMnemonic(out.String()) + + return mnemonic, nil +} + +func extractMnemonic(output string) string { + scanner := bufio.NewScanner(strings.NewReader(output)) + mnemonicLineFound := false + var mnemonicLines []string + + for scanner.Scan() { + line := scanner.Text() + if mnemonicLineFound { + // Collect all subsequent lines as part of the mnemonic + mnemonicLines = append(mnemonicLines, line) + } + if strings.HasPrefix(line, "MNEMONIC") { + mnemonicLineFound = true + } + } + + if err := scanner.Err(); err != nil { + fmt.Println("Error reading output:", err) + return "" } - return nil + return strings.Join(mnemonicLines, " ") } func (c *Celestia) getDAAccData(config.RollappConfig) (*utils.AccountData, error) { @@ -137,7 +165,7 @@ func (c *Celestia) getDAAccData(config.RollappConfig) (*utils.AccountData, error } restQueryUrl := fmt.Sprintf( "%s/cosmos/bank/v1beta1/balances/%s", - CelestiaRestApiEndpoint, celAddress, + CelestiaRestApiEndpoint, celAddress.Address, ) balancesJson, err := utils.RestQueryJson(restQueryUrl) if err != nil { @@ -148,7 +176,7 @@ func (c *Celestia) getDAAccData(config.RollappConfig) (*utils.AccountData, error return nil, err } return &utils.AccountData{ - Address: celAddress, + Address: celAddress.Address, Balance: balance, }, nil } diff --git a/data_layer/da_layer.go b/data_layer/da_layer.go index 9ff094fb..417518b6 100644 --- a/data_layer/da_layer.go +++ b/data_layer/da_layer.go @@ -5,14 +5,13 @@ import ( "github.com/dymensionxyz/roller/cmd/utils" "github.com/dymensionxyz/roller/config" - "github.com/dymensionxyz/roller/data_layer/avail" "github.com/dymensionxyz/roller/data_layer/celestia" "github.com/dymensionxyz/roller/data_layer/damock" ) type DataLayer interface { - GetDAAccountAddress() (string, error) - InitializeLightNodeConfig() error + GetDAAccountAddress() (*utils.KeyInfo, error) + InitializeLightNodeConfig() (string, error) CheckDABalance() ([]utils.NotFundedAddressData, error) GetStartDACmd() *exec.Cmd GetDAAccData(c config.RollappConfig) ([]utils.AccountData, error) @@ -37,8 +36,8 @@ func NewDAManager(datype config.DAType, home string) *DAManager { switch datype { case config.Celestia: dalayer = celestia.NewCelestia(home) - case config.Avail: - dalayer = avail.NewAvail(home) + // case config.Avail: + // dalayer = avail.NewAvail(home) case config.Local: dalayer = &damock.DAMock{} default: diff --git a/data_layer/damock/damock.go b/data_layer/damock/damock.go index 1590c05e..f31d1376 100644 --- a/data_layer/damock/damock.go +++ b/data_layer/damock/damock.go @@ -24,12 +24,12 @@ func NewDAMock() *DAMock { return &DAMock{} } -func (d *DAMock) GetDAAccountAddress() (string, error) { - return "", nil +func (d *DAMock) GetDAAccountAddress() (*utils.KeyInfo, error) { + return nil, nil } -func (d *DAMock) InitializeLightNodeConfig() error { - return nil +func (d *DAMock) InitializeLightNodeConfig() (string, error) { + return "", nil } func (d *DAMock) CheckDABalance() ([]utils.NotFundedAddressData, error) { diff --git a/relayer/config.go b/relayer/config.go index 2ed013ad..bf9762ee 100644 --- a/relayer/config.go +++ b/relayer/config.go @@ -15,6 +15,7 @@ import ( func CreatePath(rlpCfg config.RollappConfig) error { relayerHome := filepath.Join(rlpCfg.Home, consts.ConfigDirName.Relayer) + newPathCmd := exec.Command( consts.Executables.Relayer, "paths", @@ -28,6 +29,7 @@ func CreatePath(rlpCfg config.RollappConfig) error { if err := newPathCmd.Run(); err != nil { return err } + return nil } diff --git a/relayer/create_ibc_channel.go b/relayer/create_ibc_channel.go index 00d53a76..ac27c036 100644 --- a/relayer/create_ibc_channel.go +++ b/relayer/create_ibc_channel.go @@ -8,10 +8,9 @@ import ( "strconv" "time" - "github.com/dymensionxyz/roller/sequencer" - "github.com/dymensionxyz/roller/cmd/consts" "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/sequencer" ) // CreateIBCChannel Creates an IBC channel between the hub and the client, and return the source channel ID. @@ -34,7 +33,7 @@ func (r *Relayer) CreateIBCChannel( if err != nil { return ConnectionChannels{}, err } - sendFundsCmd := seq.GetSendCmd(sequecerAddress) + sendFundsCmd := seq.GetSendCmd(sequecerAddress.Address) utils.RunCommandEvery( ctx, sendFundsCmd.Path,