Skip to content

Commit dc599cf

Browse files
committed
✨ feat: add config handling to settler application
Introduce a new configuration package to manage SettlerEngine's settings and integrate it into the main application. This update adds functionality to load, save, and prompt for configuration values, which enhances the app's configurability and user experience.
1 parent 4f5e0ef commit dc599cf

2 files changed

Lines changed: 140 additions & 5 deletions

File tree

cmd/settler/main.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/nathfavour/settlerengine/internal/adapters/crypto/mantle"
1818
"github.com/nathfavour/settlerengine/internal/domain"
1919
"github.com/nathfavour/settlerengine/pkg/anyisland"
20+
"github.com/nathfavour/settlerengine/pkg/config"
2021
"github.com/nathfavour/settlerengine/pkg/crypto"
2122
"github.com/nathfavour/settlerengine/pkg/storage"
2223
"github.com/nathfavour/settlerengine/pkg/uds"
@@ -46,6 +47,8 @@ func main() {
4647
runPay(os.Args[2:])
4748
case "demo":
4849
runDemo(os.Args[2:])
50+
case "config":
51+
runConfig(os.Args[2:])
4952
case "help":
5053
printUsage()
5154
default:
@@ -64,27 +67,72 @@ func printUsage() {
6467
fmt.Println(" facilitator Start the settlement facilitator daemon")
6568
fmt.Println(" pay Execute a policy-protected payment")
6669
fmt.Println(" demo Run a full agentic demo with Mantle on-chain anchoring")
70+
fmt.Println(" config Interactively configure SettlerEngine")
6771
fmt.Println(" help Show this help message")
6872
}
6973

74+
func runConfig(args []string) {
75+
cfg, err := config.LoadConfig()
76+
if err != nil {
77+
// Default config if none exists
78+
cfg = &config.SettlerConfig{
79+
RPCURL: "https://rpc.sepolia.mantle.xyz",
80+
RegistryAddress: "0x33aE8331a2406EEc3A33483001aC5650DA2e0662",
81+
AgentID: "42",
82+
}
83+
}
84+
85+
cfg.Prompt()
86+
87+
if err := config.SaveConfig(cfg); err != nil {
88+
log.Fatalf("❌ Failed to save config: %v", err)
89+
}
90+
91+
path, _ := config.GetConfigPath()
92+
fmt.Printf("✅ Configuration saved to: %s\n", path)
93+
}
94+
7095
func runDemo(args []string) {
7196
fmt.Println("🎬 Starting SettlerEngine Agentic Demo...")
7297

98+
cfg, _ := config.LoadConfig()
99+
if cfg == nil {
100+
cfg = &config.SettlerConfig{}
101+
}
102+
73103
privKey := os.Getenv("PRIVATE_KEY")
74104
if privKey == "" {
75-
log.Fatal("❌ PRIVATE_KEY environment variable is required for demo")
105+
privKey = cfg.PrivateKey
106+
}
107+
if privKey == "" {
108+
log.Fatal("❌ PRIVATE_KEY required (set env or run 'settler config')")
109+
}
110+
111+
rpcURL := cfg.RPCURL
112+
if rpcURL == "" {
113+
rpcURL = "https://rpc.sepolia.mantle.xyz"
114+
}
115+
116+
registryAddr := cfg.RegistryAddress
117+
if registryAddr == "" {
118+
registryAddr = "0x33aE8331a2406EEc3A33483001aC5650DA2e0662"
119+
}
120+
121+
agentIDStr := cfg.AgentID
122+
if agentIDStr == "" {
123+
agentIDStr = "42"
76124
}
125+
agentID, _ := new(big.Int).SetString(agentIDStr, 10)
77126

78127
// 1. ERC-8004 Identity Resolution
79128
fmt.Println("🤖 [1/3] Resolving Agent Identity (ERC-8004)...")
80129
registry, _ := erc8004.NewRegistryClient(
81-
"https://rpc.sepolia.mantle.xyz",
130+
rpcURL,
82131
common.HexToAddress("0x8004000000000000000000000000000000000001"),
83132
common.HexToAddress("0x8004000000000000000000000000000000000002"),
84133
common.HexToAddress("0x8004000000000000000000000000000000000003"),
85134
)
86135

87-
agentID := big.NewInt(42)
88136
identity, _ := registry.ResolveAgent(context.Background(), agentID)
89137
fmt.Printf("✅ Identity Verified: %s\n", identity.Metadata.Name)
90138

@@ -104,8 +152,8 @@ func runDemo(args []string) {
104152
// 3. Mantle On-Chain Anchoring
105153
fmt.Println("⚓ [3/3] Anchoring transaction to Mantle Sepolia...")
106154
mantleClient, err := mantle.NewRegistryClient(
107-
"https://rpc.sepolia.mantle.xyz",
108-
common.HexToAddress("0x33aE8331a2406EEc3A33483001aC5650DA2e0662"),
155+
rpcURL,
156+
common.HexToAddress(registryAddr),
109157
big.NewInt(5003),
110158
)
111159
if err != nil {

pkg/config/config.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
type SettlerConfig struct {
11+
RPCURL string `json:"rpc_url"`
12+
PrivateKey string `json:"private_key"`
13+
RegistryAddress string `json:"registry_address"`
14+
AgentID string `json:"agent_id"`
15+
}
16+
17+
func GetConfigPath() (string, error) {
18+
configDir, err := os.UserConfigDir()
19+
if err != nil {
20+
return "", err
21+
}
22+
dir := filepath.Join(configDir, "settlerengine")
23+
if err := os.MkdirAll(dir, 0755); err != nil {
24+
return "", err
25+
}
26+
return filepath.Join(dir, "config.json"), nil
27+
}
28+
29+
func LoadConfig() (*SettlerConfig, error) {
30+
path, err := GetConfigPath()
31+
if err != nil {
32+
return nil, err
33+
}
34+
data, err := os.ReadFile(path)
35+
if err != nil {
36+
return nil, err
37+
}
38+
var cfg SettlerConfig
39+
if err := json.Unmarshal(data, &cfg); err != nil {
40+
return nil, err
41+
}
42+
return &cfg, nil
43+
}
44+
45+
func SaveConfig(cfg *SettlerConfig) error {
46+
path, err := GetConfigPath()
47+
if err != nil {
48+
return err
49+
}
50+
data, err := json.MarshalIndent(cfg, "", " ")
51+
if err != nil {
52+
return err
53+
}
54+
return os.WriteFile(path, data, 0600)
55+
}
56+
57+
func (c *SettlerConfig) Prompt() {
58+
fmt.Println("⚙️ SettlerEngine Configuration Setup")
59+
60+
fmt.Printf("Ethereum RPC URL [%s]: ", c.RPCURL)
61+
var rpc string
62+
fmt.Scanln(&rpc)
63+
if rpc != "" {
64+
c.RPCURL = rpc
65+
}
66+
67+
fmt.Printf("Private Key (hex) [hidden]: ")
68+
var key string
69+
fmt.Scanln(&key)
70+
if key != "" {
71+
c.PrivateKey = key
72+
}
73+
74+
fmt.Printf("Registry Contract Address [%s]: ", c.RegistryAddress)
75+
var reg string
76+
fmt.Scanln(&reg)
77+
if reg != "" {
78+
c.RegistryAddress = reg
79+
}
80+
81+
fmt.Printf("Agent ID [%s]: ", c.AgentID)
82+
var id string
83+
fmt.Scanln(&id)
84+
if id != "" {
85+
c.AgentID = id
86+
}
87+
}

0 commit comments

Comments
 (0)