Skip to content

Commit d41953f

Browse files
committed
feat: ini sessions
1 parent fcbacb9 commit d41953f

File tree

7 files changed

+615
-36
lines changed

7 files changed

+615
-36
lines changed

cmd/sequence/config.go

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@ func (n *NodeElement) ToTree() v3.WalletConfigTree {
6969
}
7070
}
7171

72+
type ConfigOutput struct {
73+
Threshold string `json:"threshold"`
74+
Checkpoint string `json:"checkpoint"`
75+
Topology TopologyOutput `json:"topology"`
76+
}
77+
78+
type TopologyOutput struct {
79+
Type string `json:"type"`
80+
Address string `json:"address,omitempty"`
81+
Weight string `json:"weight,omitempty"`
82+
}
83+
84+
func treeToMap(tree v3.WalletConfigTree) TopologyOutput {
85+
switch t := tree.(type) {
86+
case *v3.WalletConfigTreeAddressLeaf:
87+
return TopologyOutput{
88+
Type: "signer",
89+
Address: t.Address.Hex(),
90+
Weight: fmt.Sprintf("%d", t.Weight),
91+
}
92+
case *v3.WalletConfigTreeSubdigestLeaf:
93+
return TopologyOutput{
94+
Type: "subdigest",
95+
Address: t.Subdigest.Hex(),
96+
}
97+
case *v3.WalletConfigTreeNestedLeaf:
98+
return TopologyOutput{
99+
Type: "nested",
100+
}
101+
case *v3.WalletConfigTreeNodeLeaf:
102+
return TopologyOutput{
103+
Type: "node",
104+
Address: t.Node.Hex(),
105+
}
106+
default:
107+
log.Printf("Unsupported tree type: %T", tree)
108+
return TopologyOutput{}
109+
}
110+
}
111+
72112
func parseNestedElement(element string) (*NestedElement, error) {
73113
log.Printf("Parsing nested element: %q", element)
74114

@@ -213,7 +253,7 @@ func parseElement(element string) (ConfigElement, error) {
213253
}
214254
}
215255

216-
func createConfig(threshold uint16, checkpoint uint64, elements []string) (*v3.WalletConfig, error) {
256+
func createConfig(threshold uint16, checkpoint uint64, elements []string) (ConfigOutput, error) {
217257
log.Printf("Creating config with threshold: %d, checkpoint: %d, elements: %v", threshold, checkpoint, elements)
218258

219259
var configElements []ConfigElement
@@ -223,23 +263,33 @@ func createConfig(threshold uint16, checkpoint uint64, elements []string) (*v3.W
223263
}
224264
configElement, err := parseElement(element)
225265
if err != nil {
226-
return nil, err
266+
return ConfigOutput{}, err
227267
}
228268
configElements = append(configElements, configElement)
229269
}
230270

231-
var trees []v3.WalletConfigTree
232-
for _, element := range configElements {
233-
trees = append(trees, element.ToTree())
234-
}
235-
236-
config := &v3.WalletConfig{
237-
Threshold_: threshold,
238-
Checkpoint_: checkpoint,
239-
Tree: v3.WalletConfigTreeNodes(trees...),
271+
var tree v3.WalletConfigTree
272+
if len(configElements) == 0 {
273+
return ConfigOutput{}, fmt.Errorf("no valid config elements provided")
274+
} else if len(configElements) == 1 {
275+
tree = configElements[0].ToTree()
276+
} else {
277+
var trees []v3.WalletConfigTree
278+
for _, element := range configElements {
279+
trees = append(trees, element.ToTree())
280+
}
281+
tree = &v3.WalletConfigTreeNestedLeaf{
282+
Threshold: threshold,
283+
Weight: 0,
284+
Tree: v3.WalletConfigTreeNodes(trees...),
285+
}
240286
}
241287

242-
return config, nil
288+
return ConfigOutput{
289+
Threshold: fmt.Sprintf("%d", threshold),
290+
Checkpoint: fmt.Sprintf("%d", checkpoint),
291+
Topology: treeToMap(tree),
292+
}, nil
243293
}
244294

245295
// createNewConfig creates a new configuration from the given parameters
@@ -286,6 +336,38 @@ func calculateImageHash(params *ConfigImageHashParams) (string, error) {
286336
return "", fmt.Errorf("failed to parse raw config: %w", err)
287337
}
288338

339+
// Check if the config is nested under an "input" field
340+
if input, ok := rawConfig["input"].(map[string]interface{}); ok {
341+
rawConfig = input
342+
}
343+
344+
// Convert topology to tree if present
345+
if topology, ok := rawConfig["topology"].(map[string]interface{}); ok {
346+
// Convert topology to the expected tree format
347+
treeConfig := make(map[string]interface{})
348+
if topology["type"] == "signer" {
349+
// Convert string weight to int64
350+
if weight, ok := topology["weight"].(string); ok {
351+
weightInt := new(big.Int)
352+
if _, ok := weightInt.SetString(weight, 10); !ok {
353+
return "", fmt.Errorf("invalid weight: %s", weight)
354+
}
355+
if !weightInt.IsInt64() {
356+
return "", fmt.Errorf("weight too large: %s", weight)
357+
}
358+
treeConfig["weight"] = weightInt.Int64()
359+
} else {
360+
treeConfig["weight"] = topology["weight"]
361+
}
362+
treeConfig["address"] = topology["address"]
363+
} else {
364+
// Handle other types if needed
365+
return "", fmt.Errorf("unsupported topology type: %v", topology["type"])
366+
}
367+
rawConfig["tree"] = treeConfig
368+
delete(rawConfig, "topology")
369+
}
370+
289371
config, err := v3.Core.DecodeWalletConfig(rawConfig)
290372
if err != nil {
291373
return "", fmt.Errorf("failed to decode wallet config: %w", err)

0 commit comments

Comments
 (0)