|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/LumeraProtocol/sdk-go/cascade" |
| 13 | + sdkcrypto "github.com/LumeraProtocol/sdk-go/pkg/crypto" |
| 14 | + "github.com/cosmos/cosmos-sdk/crypto/keyring" |
| 15 | +) |
| 16 | + |
| 17 | +const defaultCascadeTimeout = 30 * time.Second |
| 18 | + |
| 19 | +// Client bundles the cascade client with its backing keyring and owner address. |
| 20 | +// The keyring is the controller chain keyring; the Lumera address is derived from it. |
| 21 | +type Client struct { |
| 22 | + Cascade *cascade.Client |
| 23 | + Keyring keyring.Keyring |
| 24 | + OwnerAddress string |
| 25 | +} |
| 26 | + |
| 27 | +// NewCascadeClient initializes the SDK cascade client using controller keyring settings. |
| 28 | +// It derives a Lumera bech32 address from the same key name for action registration. |
| 29 | +func NewCascadeClient(ctx context.Context, cfg *Config) (*Client, error) { |
| 30 | + if cfg == nil { |
| 31 | + return nil, fmt.Errorf("config is nil") |
| 32 | + } |
| 33 | + // Create the controller keyring (used for ICA signing and metadata signing). |
| 34 | + controllerKR, err := newControllerKeyring(cfg.Controller) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + // Resolve controller owner address using the configured controller account HRP. |
| 39 | + ownerAddr, err := sdkcrypto.AddressFromKey(controllerKR, cfg.Controller.KeyName, cfg.Controller.AccountHRP) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("derive controller address: %w", err) |
| 42 | + } |
| 43 | + // Resolve Lumera address with the Lumera HRP for on-chain action registration. |
| 44 | + lumeraAddr, err := sdkcrypto.AddressFromKey(controllerKR, cfg.Lumera.KeyName, "lumera") |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("derive lumera address: %w", err) |
| 47 | + } |
| 48 | + // Initialize cascade SDK client with Lumera connection settings and log level. |
| 49 | + casc, err := cascade.New(ctx, cascade.Config{ |
| 50 | + ChainID: cfg.Lumera.ChainID, |
| 51 | + GRPCAddr: cfg.Lumera.GRPCEndpoint, |
| 52 | + Address: lumeraAddr, |
| 53 | + KeyName: cfg.Lumera.KeyName, |
| 54 | + ICAOwnerKeyName: cfg.Controller.KeyName, |
| 55 | + ICAOwnerHRP: cfg.Controller.AccountHRP, |
| 56 | + Timeout: defaultCascadeTimeout, |
| 57 | + LogLevel: cfg.Lumera.LogLevel, |
| 58 | + }, controllerKR) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + return &Client{Cascade: casc, Keyring: controllerKR, OwnerAddress: ownerAddr}, nil |
| 63 | +} |
| 64 | + |
| 65 | +// newControllerKeyring constructs the Cosmos keyring for the controller chain. |
| 66 | +func newControllerKeyring(cfg ControllerConfig) (keyring.Keyring, error) { |
| 67 | + passphrase, err := resolvePassphrase(cfg.KeyringPassphrasePlain, cfg.KeyringPassphraseFile) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + // For test backend, fall back to controller.home when keyring_dir is unset. |
| 72 | + dir := strings.TrimSpace(cfg.KeyringDir) |
| 73 | + if dir == "" && strings.EqualFold(cfg.KeyringBackend, "test") { |
| 74 | + dir = strings.TrimSpace(cfg.Home) |
| 75 | + } |
| 76 | + params := sdkcrypto.KeyringParams{ |
| 77 | + AppName: keyringAppName(cfg), |
| 78 | + Backend: cfg.KeyringBackend, |
| 79 | + Dir: dir, |
| 80 | + Input: passphraseReader(passphrase), |
| 81 | + } |
| 82 | + kr, err := sdkcrypto.NewKeyring(params) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("init keyring: %w", err) |
| 85 | + } |
| 86 | + return kr, nil |
| 87 | +} |
| 88 | + |
| 89 | +// resolvePassphrase selects a single passphrase source or returns empty for interactive prompts. |
| 90 | +func resolvePassphrase(plain, filePath string) (string, error) { |
| 91 | + plain = strings.TrimSpace(plain) |
| 92 | + filePath = strings.TrimSpace(filePath) |
| 93 | + if plain != "" && filePath != "" { |
| 94 | + return "", fmt.Errorf("only one of keyring passphrase plain/file may be set") |
| 95 | + } |
| 96 | + if plain != "" { |
| 97 | + return plain, nil |
| 98 | + } |
| 99 | + if filePath == "" { |
| 100 | + return "", nil |
| 101 | + } |
| 102 | + data, err := os.ReadFile(filePath) |
| 103 | + if err != nil { |
| 104 | + return "", fmt.Errorf("read keyring passphrase file: %w", err) |
| 105 | + } |
| 106 | + pass := strings.TrimSpace(string(data)) |
| 107 | + if pass == "" { |
| 108 | + return "", fmt.Errorf("keyring passphrase file is empty") |
| 109 | + } |
| 110 | + return pass, nil |
| 111 | +} |
| 112 | + |
| 113 | +func passphraseReader(passphrase string) io.Reader { |
| 114 | + if passphrase == "" { |
| 115 | + return nil |
| 116 | + } |
| 117 | + // Repeat the passphrase to satisfy multiple keyring prompts. |
| 118 | + return &repeatReader{data: []byte(passphrase + "\n")} |
| 119 | +} |
| 120 | + |
| 121 | +// keyringAppName selects a stable keyring application name for the controller chain. |
| 122 | +func keyringAppName(cfg ControllerConfig) string { |
| 123 | + if cfg.Binary != "" { |
| 124 | + return filepath.Base(cfg.Binary) |
| 125 | + } |
| 126 | + if cfg.ChainID != "" { |
| 127 | + return cfg.ChainID |
| 128 | + } |
| 129 | + return "lumera" |
| 130 | +} |
| 131 | + |
| 132 | +type repeatReader struct { |
| 133 | + data []byte |
| 134 | + pos int |
| 135 | +} |
| 136 | + |
| 137 | +// Read loops the underlying data to satisfy repeated keyring reads. |
| 138 | +func (r *repeatReader) Read(p []byte) (int, error) { |
| 139 | + if len(r.data) == 0 { |
| 140 | + return 0, io.EOF |
| 141 | + } |
| 142 | + n := 0 |
| 143 | + for n < len(p) { |
| 144 | + remaining := len(r.data) - r.pos |
| 145 | + if remaining == 0 { |
| 146 | + r.pos = 0 |
| 147 | + remaining = len(r.data) |
| 148 | + } |
| 149 | + chunk := remaining |
| 150 | + if chunk > len(p)-n { |
| 151 | + chunk = len(p) - n |
| 152 | + } |
| 153 | + copy(p[n:n+chunk], r.data[r.pos:r.pos+chunk]) |
| 154 | + n += chunk |
| 155 | + r.pos += chunk |
| 156 | + } |
| 157 | + return n, nil |
| 158 | +} |
0 commit comments