Skip to content
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
2 changes: 1 addition & 1 deletion pkg/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GenerateMnemonic() (string, error) {
}

// CreateNewAccount creates a new account in the keyring
func CreateNewAccount(kr keyring.Keyring, name string, entropySize int) (string, *keyring.Record, error) {
func CreateNewAccount(kr keyring.Keyring, name string) (string, *keyring.Record, error) {
// Generate a new mnemonic
mnemonic, err := GenerateMnemonic()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions sdk/action/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ type ClientImpl struct {
var _ Client = (*ClientImpl)(nil)

// NewClient creates a new action client
func NewClient(ctx context.Context, config config.Config, logger log.Logger, keyring keyring.Keyring) (Client, error) {
func NewClient(ctx context.Context, config config.Config, logger log.Logger) (Client, error) {
if logger == nil {
logger = log.NewNoopLogger()
}

taskManager, err := task.NewManager(ctx, config, logger, keyring)
taskManager, err := task.NewManager(ctx, config, logger)
if err != nil {
return nil, fmt.Errorf("failed to create task manager: %w", err)
}
Expand All @@ -53,7 +53,6 @@ func NewClient(ctx context.Context, config config.Config, logger log.Logger, key
config: config,
taskManager: taskManager,
logger: logger,
keyring: keyring,
}, nil
}

Expand Down
102 changes: 63 additions & 39 deletions sdk/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,109 @@ package config

import (
"errors"
"time"
"fmt"

"github.com/LumeraProtocol/supernode/pkg/keyring"
cosmoskeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)

const (
DefaultLocalCosmosAddress = "lumera1qv3" // Example address - replace with actual
DefaultChainID = "lumera-testnet" // Example chain ID - replace with actual
DefaultGRPCAddr = "127.0.0.1:9090"
DefaultTimeout = 10
DefaultChainID = "lumera-testnet"
DefaultGRPCAddr = "127.0.0.1:9090"
defaultKeyName = "default"
)

// AccountConfig holds peer-to-peer addresses, ports, etc.
type AccountConfig struct {
LocalCosmosAddress string // REQUIRED - cosmos account address used for signing tx
LocalCosmosAddress string
KeyName string
Keyring cosmoskeyring.Keyring
}

// LumeraConfig wraps all chain-specific dials.
type LumeraConfig struct {
GRPCAddr string // REQUIRED – e.g. "127.0.0.1:9090"
ChainID string // REQUIRED – e.g. "lumera-mainnet"
Timeout time.Duration // OPTIONAL – defaults to DefaultTimeout
KeyName string
GRPCAddr string
ChainID string
}

type Config struct {
Account AccountConfig
Lumera LumeraConfig
}

// Option is a functional option for configuring the Config.
type Option func(*Config)

// New builds a Config, applying the supplied functional options.
func New(opts ...Option) (*Config, error) {
cfg := &Config{
Account: AccountConfig{LocalCosmosAddress: DefaultLocalCosmosAddress},
// Default returns a fully initialized config with default values and an auto-generated keyring
func DefaultConfigWithKr() (Config, error) {
cfg := Config{
Lumera: LumeraConfig{
GRPCAddr: DefaultGRPCAddr,
Timeout: DefaultTimeout,
ChainID: DefaultChainID,
},
}

for _, opt := range opts {
opt(cfg)
}
return WithKeyring(cfg)
}

if err := cfg.Validate(); err != nil {
return nil, err
// WithKeyring ensures the config has a keyring, creating one if needed
func WithKeyring(cfg Config) (Config, error) {
// Apply any defaults for zero values
if cfg.Lumera.GRPCAddr == "" {
cfg.Lumera.GRPCAddr = DefaultGRPCAddr
}
if cfg.Lumera.ChainID == "" {
cfg.Lumera.ChainID = DefaultChainID
}
return cfg, nil
}

func WithLocalCosmosAddress(addr string) Option {
return func(c *Config) { c.Account.LocalCosmosAddress = addr }
}
// Initialize Lumera SDK config
keyring.InitSDKConfig()

func WithGRPCAddr(addr string) Option {
return func(c *Config) { c.Lumera.GRPCAddr = addr }
}
// If keyring is nil, create an in-memory keyring and generate a new account
if cfg.Account.Keyring == nil {
// Set default key name if not provided
keyName := cfg.Account.KeyName
if keyName == "" {
keyName = defaultKeyName
}

func WithChainID(id string) Option {
return func(c *Config) { c.Lumera.ChainID = id }
}
// Create an in-memory keyring
kr, err := keyring.InitKeyring("memory", "")
if err != nil {
return Config{}, fmt.Errorf("failed to create in-memory keyring: %w", err)
}

// Create a new account with generated mnemonic
_, info, err := keyring.CreateNewAccount(kr, keyName)
if err != nil {
return Config{}, fmt.Errorf("failed to create new account: %w", err)
}

func WithTimeout(d time.Duration) Option {
return func(c *Config) { c.Lumera.Timeout = d }
// Get the address of the new account
addr, err := info.GetAddress()
if err != nil {
return Config{}, fmt.Errorf("failed to get address from account: %w", err)
}

// Update config with the new keyring and address
cfg.Account.Keyring = kr
cfg.Account.KeyName = keyName
cfg.Account.LocalCosmosAddress = addr.String()
}

// Validate the config
if err := cfg.Validate(); err != nil {
return Config{}, err
}
return cfg, nil
}

// Validate checks the configuration for required fields and valid values.
func (c *Config) Validate() error {
func (c Config) Validate() error {
switch {
case c.Account.LocalCosmosAddress == "":
return errors.New("config: Network.LocalCosmosAddress is required")
case c.Lumera.GRPCAddr == "":
return errors.New("config: Lumera.GRPCAddr is required")
case c.Lumera.ChainID == "":
return errors.New("config: Lumera.ChainID is required")
case c.Lumera.Timeout <= 0:
return errors.New("config: Lumera.Timeout must be > 0")
default:
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ManagerImpl struct {
keyring keyring.Keyring
}

func NewManager(ctx context.Context, config config.Config, logger log.Logger, kr keyring.Keyring) (Manager, error) {
func NewManager(ctx context.Context, config config.Config, logger log.Logger) (Manager, error) {
// 1 - Logger
if logger == nil {
logger = log.NewNoopLogger()
Expand All @@ -51,8 +51,8 @@ func NewManager(ctx context.Context, config config.Config, logger log.Logger, kr
lumera.ConfigParams{
GRPCAddr: config.Lumera.GRPCAddr,
ChainID: config.Lumera.ChainID,
KeyName: config.Lumera.KeyName,
Keyring: kr,
KeyName: config.Account.KeyName,
Keyring: config.Account.Keyring,
},
logger)

Expand All @@ -72,7 +72,7 @@ func NewManager(ctx context.Context, config config.Config, logger log.Logger, kr
taskCache: taskCache,
eventBus: eventBus,
logger: logger,
keyring: kr,
keyring: config.Account.Keyring,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion supernode/cmd/keys_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Example:

// Generate mnemonic and create new account
// Default to 256 bits of entropy (24 words)
mnemonic, info, err := keyring.CreateNewAccount(kr, keyName, 256)
mnemonic, info, err := keyring.CreateNewAccount(kr, keyName)
if err != nil {
return fmt.Errorf("failed to create new account: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/system/e2e_cascade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ func TestCascadeE2E(t *testing.T) {
// This defines how to connect to network services
accConfig := sdkconfig.AccountConfig{
LocalCosmosAddress: recoveredAddress,
KeyName: testKeyName,
Keyring: keplrKeyring,
}

lumraConfig := sdkconfig.LumeraConfig{
GRPCAddr: lumeraGRPCAddr,
ChainID: lumeraChainID,
Timeout: 300, // 30 seconds timeout
KeyName: testKeyName,
}
actionConfig := sdkconfig.Config{
Account: accConfig,
Expand All @@ -414,8 +414,8 @@ func TestCascadeE2E(t *testing.T) {
actionClient, err := action.NewClient(
ctx,
actionConfig,
nil, // Nil logger - use default
keplrKeyring, // Use the in-memory keyring for signing
nil, // Nil logger - use default

)
require.NoError(t, err, "Failed to create action client")

Expand Down