2024-03-19 00:31:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-13 22:31:09 +00:00
|
|
|
"context"
|
2024-03-19 00:31:35 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-03-19 00:31:35 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
|
|
|
"github.com/status-im/status-go/logutils"
|
2024-06-12 08:00:51 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2024-03-19 00:31:35 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
|
|
|
"github.com/status-im/status-go/services/wakuv2ext"
|
2024-06-13 22:31:09 +00:00
|
|
|
"github.com/status-im/status-go/telemetry"
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2024-03-19 00:31:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupLogger(file string) *zap.Logger {
|
|
|
|
logFile := fmt.Sprintf("%s.log", strings.ToLower(file))
|
|
|
|
logSettings := logutils.LogSettings{
|
|
|
|
Enabled: true,
|
|
|
|
MobileSystem: false,
|
|
|
|
Level: "DEBUG",
|
|
|
|
File: logFile,
|
|
|
|
MaxSize: 100,
|
|
|
|
MaxBackups: 3,
|
|
|
|
CompressRotated: true,
|
|
|
|
}
|
|
|
|
if err := logutils.OverrideRootLogWithConfig(logSettings, false); err != nil {
|
2024-06-14 13:13:31 +00:00
|
|
|
zap.S().Fatalf("Error initializing logger: %v", err)
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
return logutils.ZapLogger()
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
func start(name string, port int, apiModules string, telemetryUrl string, keyUID string, logger *zap.SugaredLogger) (*StatusCLI, error) {
|
2024-06-12 08:00:51 +00:00
|
|
|
var (
|
|
|
|
rootDataDir = fmt.Sprintf("./test-%s", strings.ToLower(name))
|
|
|
|
password = "some-password"
|
|
|
|
)
|
|
|
|
setupLogger(name)
|
2024-06-14 13:13:31 +00:00
|
|
|
logger.Info("starting messenger")
|
2024-03-19 00:31:35 +00:00
|
|
|
|
|
|
|
backend := api.NewGethStatusBackend()
|
2024-06-14 13:13:31 +00:00
|
|
|
if keyUID != "" {
|
2024-06-12 08:00:51 +00:00
|
|
|
if err := getAccountAndLogin(backend, name, rootDataDir, password, keyUID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-14 13:13:31 +00:00
|
|
|
logger.Infof("existing account, key UID: %v", keyUID)
|
2024-06-12 08:00:51 +00:00
|
|
|
} else {
|
|
|
|
acc, err := createAccountAndLogin(backend, name, rootDataDir, password, apiModules, telemetryUrl, port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-14 13:13:31 +00:00
|
|
|
logger.Infof("account created, key UID: %v", acc.KeyUID)
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wakuService := backend.StatusNode().WakuV2ExtService()
|
|
|
|
if wakuService == nil {
|
|
|
|
return nil, errors.New("waku service is not available")
|
|
|
|
}
|
2024-06-13 22:31:09 +00:00
|
|
|
|
|
|
|
if telemetryUrl != "" {
|
2024-06-14 13:13:31 +00:00
|
|
|
telemetryLogger, err := getLogger(true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
telemetryClient := telemetry.NewClient(telemetryLogger, telemetryUrl, backend.SelectedAccountKeyID(), name, "cli")
|
2024-06-13 22:31:09 +00:00
|
|
|
go telemetryClient.Start(context.Background())
|
|
|
|
backend.StatusNode().WakuV2Service().SetStatusTelemetryClient(telemetryClient)
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
wakuAPI := wakuv2ext.NewPublicAPI(wakuService)
|
|
|
|
|
|
|
|
messenger := wakuAPI.Messenger()
|
2024-06-12 08:00:51 +00:00
|
|
|
if _, err := wakuAPI.StartMessenger(); err != nil {
|
2024-03-19 00:31:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
logger.Info("messenger started, public key: ", messenger.IdentityPublicKeyString())
|
2024-03-19 00:31:35 +00:00
|
|
|
time.Sleep(WaitingInterval)
|
|
|
|
|
|
|
|
data := StatusCLI{
|
|
|
|
name: name,
|
|
|
|
messenger: messenger,
|
|
|
|
backend: backend,
|
2024-06-14 13:13:31 +00:00
|
|
|
logger: logger,
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 08:00:51 +00:00
|
|
|
func getAccountAndLogin(b *api.GethStatusBackend, name, rootDataDir, password string, keyUID string) error {
|
|
|
|
b.UpdateRootDataDir(rootDataDir)
|
|
|
|
if err := b.OpenAccounts(); err != nil {
|
|
|
|
return fmt.Errorf("name '%v' might not have an account: trying to find: %v: %w", name, rootDataDir, err)
|
|
|
|
}
|
|
|
|
accs, err := b.GetAccounts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(accs) == 0 {
|
|
|
|
return errors.New("no accounts found")
|
|
|
|
}
|
|
|
|
|
2024-06-14 13:13:31 +00:00
|
|
|
var acc multiaccounts.Account
|
|
|
|
found := false
|
|
|
|
for _, a := range accs {
|
|
|
|
if a.KeyUID == keyUID {
|
|
|
|
acc = a
|
|
|
|
found = true
|
|
|
|
break
|
2024-06-12 08:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-14 13:13:31 +00:00
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("account not found for keyUID: %v", keyUID)
|
|
|
|
}
|
2024-06-12 08:00:51 +00:00
|
|
|
|
|
|
|
return b.LoginAccount(&requests.Login{
|
|
|
|
Password: password,
|
|
|
|
KeyUID: acc.KeyUID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAccountAndLogin(b *api.GethStatusBackend, name, rootDataDir, password, apiModules, telemetryUrl string, port int) (*multiaccounts.Account, error) {
|
|
|
|
if err := os.MkdirAll(rootDataDir, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &requests.CreateAccount{
|
|
|
|
DisplayName: name,
|
|
|
|
CustomizationColor: "#ffffff",
|
|
|
|
Emoji: "some",
|
|
|
|
Password: password,
|
|
|
|
RootDataDir: rootDataDir,
|
|
|
|
LogFilePath: "log",
|
|
|
|
APIConfig: &requests.APIConfig{
|
|
|
|
APIModules: apiModules,
|
|
|
|
HTTPHost: "127.0.0.1",
|
|
|
|
HTTPPort: port,
|
|
|
|
},
|
|
|
|
TelemetryServerURL: telemetryUrl,
|
|
|
|
}
|
|
|
|
return b.CreateAccountAndLogin(req)
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
func (cli *StatusCLI) stop() {
|
2024-03-19 00:31:35 +00:00
|
|
|
err := cli.backend.StopNode()
|
|
|
|
if err != nil {
|
2024-06-14 13:13:31 +00:00
|
|
|
cli.logger.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogger(debug bool) (*zap.Logger, error) {
|
|
|
|
at := zap.NewAtomicLevel()
|
|
|
|
if debug {
|
|
|
|
at.SetLevel(zap.DebugLevel)
|
|
|
|
} else {
|
|
|
|
at.SetLevel(zap.InfoLevel)
|
|
|
|
}
|
|
|
|
config := zap.NewDevelopmentConfig()
|
|
|
|
config.Level = at
|
|
|
|
rawLogger, err := config.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing logger: %v", err)
|
|
|
|
}
|
|
|
|
return rawLogger, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSLogger(debug bool) (*zap.SugaredLogger, error) {
|
|
|
|
l, err := getLogger(debug)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return l.Sugar(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagsUsed(cCtx *cli.Context) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
for _, flag := range cCtx.Command.Flags {
|
|
|
|
if flag != nil && len(flag.Names()) > 0 {
|
|
|
|
fName := flag.Names()[0]
|
|
|
|
fmt.Fprintf(&sb, "\t-%s %v\n", fName, cCtx.Value(fName))
|
|
|
|
}
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|
2024-06-14 13:13:31 +00:00
|
|
|
return sb.String()
|
2024-03-19 00:31:35 +00:00
|
|
|
}
|