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-07-04 06:15:58 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
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-07-04 06:15:58 +00:00
|
|
|
type StartParams struct {
|
|
|
|
Name string
|
|
|
|
Port int
|
|
|
|
APIModules string
|
|
|
|
TelemetryURL string
|
|
|
|
KeyUID string
|
|
|
|
Fleet string
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(p StartParams, logger *zap.SugaredLogger) (*StatusCLI, error) {
|
2024-06-12 08:00:51 +00:00
|
|
|
var (
|
2024-07-04 06:15:58 +00:00
|
|
|
rootDataDir = fmt.Sprintf("./test-%s", strings.ToLower(p.Name))
|
2024-06-12 08:00:51 +00:00
|
|
|
password = "some-password"
|
|
|
|
)
|
2024-07-04 06:15:58 +00:00
|
|
|
setupLogger(p.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-07-04 06:15:58 +00:00
|
|
|
if p.KeyUID != "" {
|
|
|
|
if err := getAccountAndLogin(backend, p.Name, rootDataDir, password, p.KeyUID); err != nil {
|
2024-06-12 08:00:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-04 06:15:58 +00:00
|
|
|
logger.Infof("existing account, key UID: %v", p.KeyUID)
|
2024-06-12 08:00:51 +00:00
|
|
|
} else {
|
2024-07-04 06:15:58 +00:00
|
|
|
acc, err := createAccountAndLogin(backend, rootDataDir, password, p)
|
2024-06-12 08:00:51 +00:00
|
|
|
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
|
|
|
|
2024-07-04 06:15:58 +00:00
|
|
|
if p.TelemetryURL != "" {
|
2024-06-14 13:13:31 +00:00
|
|
|
telemetryLogger, err := getLogger(true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-01 03:27:43 +00:00
|
|
|
waku := backend.StatusNode().WakuV2Service()
|
|
|
|
telemetryClient := telemetry.NewClient(telemetryLogger, p.TelemetryURL, backend.SelectedAccountKeyID(), p.Name, "cli", telemetry.WithPeerID(waku.PeerID().String()))
|
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{
|
2024-07-04 06:15:58 +00:00
|
|
|
name: p.Name,
|
2024-03-19 00:31:35 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-04 06:15:58 +00:00
|
|
|
func createAccountAndLogin(b *api.GethStatusBackend, rootDataDir, password string, p StartParams) (*multiaccounts.Account, error) {
|
2024-06-12 08:00:51 +00:00
|
|
|
if err := os.MkdirAll(rootDataDir, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &requests.CreateAccount{
|
2024-07-04 06:15:58 +00:00
|
|
|
DisplayName: p.Name,
|
2024-06-12 08:00:51 +00:00
|
|
|
CustomizationColor: "#ffffff",
|
|
|
|
Emoji: "some",
|
|
|
|
Password: password,
|
|
|
|
RootDataDir: rootDataDir,
|
|
|
|
LogFilePath: "log",
|
|
|
|
APIConfig: &requests.APIConfig{
|
2024-07-04 06:15:58 +00:00
|
|
|
APIModules: p.APIModules,
|
2024-06-26 14:33:22 +00:00
|
|
|
HTTPEnabled: true,
|
|
|
|
HTTPHost: "127.0.0.1",
|
2024-07-04 06:15:58 +00:00
|
|
|
HTTPPort: p.Port,
|
2024-06-12 08:00:51 +00:00
|
|
|
},
|
2024-07-04 06:15:58 +00:00
|
|
|
TelemetryServerURL: p.TelemetryURL,
|
2024-06-12 08:00:51 +00:00
|
|
|
}
|
2024-07-04 06:15:58 +00:00
|
|
|
return b.CreateAccountAndLogin(req,
|
|
|
|
params.WithFleet(p.Fleet),
|
|
|
|
params.WithDiscV5BootstrapNodes(params.DefaultDiscV5Nodes(p.Fleet)),
|
|
|
|
params.WithWakuNodes(params.DefaultWakuNodes(p.Fleet)),
|
|
|
|
)
|
2024-06-12 08:00:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|