@@ -407,82 +407,6 @@ func createNewConfig(params *ConfigNewParams) (string, error) {
407407 log .Printf ("Created config: %s" , string (jsonConfig ))
408408 return string (jsonConfig ), nil
409409}
410-
411- // calculateImageHash calculates the image hash for a given configuration
412- func calculateImageHash (params * ConfigImageHashParams ) (string , error ) {
413- var rawConfig map [string ]interface {}
414- if err := json .Unmarshal (params .Input , & rawConfig ); err != nil {
415- return "" , fmt .Errorf ("failed to parse raw config: %w" , err )
416- }
417-
418- // Check if the config is nested under an "input" field
419- if input , ok := rawConfig ["input" ].(map [string ]interface {}); ok {
420- rawConfig = input
421- }
422-
423- // Convert topology array to tree structure
424- if topology , ok := rawConfig ["topology" ].([]interface {}); ok {
425- // Create a tree structure from the topology array
426- tree := make (map [string ]interface {})
427- for i , row := range topology {
428- rowArray , ok := row .([]interface {})
429- if ! ok {
430- continue
431- }
432- for j , item := range rowArray {
433- itemMap , ok := item .(map [string ]interface {})
434- if ! ok {
435- continue
436- }
437-
438- // Convert topology item to tree node
439- node := make (map [string ]interface {})
440- switch itemMap ["type" ] {
441- case "signer" :
442- node ["weight" ] = itemMap ["weight" ]
443- node ["address" ] = itemMap ["address" ]
444- case "subdigest" :
445- node ["subdigest" ] = itemMap ["digest" ]
446- case "sapient-signer" :
447- node ["weight" ] = itemMap ["weight" ]
448- node ["address" ] = itemMap ["address" ]
449- node ["imageHash" ] = itemMap ["imageHash" ]
450- case "nested" :
451- if nestedTree , ok := itemMap ["tree" ].(map [string ]interface {}); ok {
452- node ["weight" ] = nestedTree ["weight" ]
453- node ["threshold" ] = nestedTree ["threshold" ]
454- node ["tree" ] = nestedTree ["tree" ]
455- }
456- }
457-
458- // Add node to the tree
459- if i == 0 && j == 0 {
460- tree = node
461- } else {
462- // Create a new node with left and right branches
463- newNode := make (map [string ]interface {})
464- newNode ["left" ] = tree
465- newNode ["right" ] = node
466- tree = newNode
467- }
468- }
469- }
470- rawConfig ["tree" ] = tree
471- delete (rawConfig , "topology" )
472- }
473-
474- config , err := v3 .Core .DecodeWalletConfig (rawConfig )
475- if err != nil {
476- return "" , fmt .Errorf ("failed to decode wallet config: %w" , err )
477- }
478-
479- log .Printf ("Decoded config: %+v" , config )
480- spew .Dump (config )
481-
482- imageHash := config .ImageHash ()
483- return "0x" + common .Bytes2Hex (imageHash .Bytes ()), nil
484- }
485-
486410func handleRawConfig (input []byte ) (map [string ]interface {}, error ) {
487411 var rawConfig map [string ]interface {}
488412 if err := json .Unmarshal (input , & rawConfig ); err != nil {
@@ -497,57 +421,117 @@ func handleRawConfig(input []byte) (map[string]interface{}, error) {
497421 // Convert topology array to tree structure matching TypeScript
498422 if topology , ok := rawConfig ["topology" ].([]interface {}); ok {
499423 if len (topology ) != 2 {
500- return nil , fmt .Errorf ("expected exactly two rows in topology " )
424+ return nil , fmt .Errorf ("topology must have exactly two elements " )
501425 }
502426
503- var subtrees []map [string ]interface {}
504- for i , row := range topology {
505- rowArray , ok := row .([]interface {})
506- if ! ok || len (rowArray ) != 2 {
507- return nil , fmt .Errorf ("each topology row must have exactly two elements" )
508- }
427+ var leftNode , rightNode map [string ]interface {}
428+ var err error
509429
510- leftItem , ok := rowArray [0 ].(map [string ]interface {})
511- if ! ok {
512- return nil , fmt .Errorf ("invalid left item in topology row %d" , i )
430+ // Check if the first element is an array
431+ if leftArray , isArray := topology [0 ].([]interface {}); isArray {
432+ if len (leftArray ) != 2 {
433+ return nil , fmt .Errorf ("left topology row must have exactly two elements" )
513434 }
514- leftNode , err := convertTree ( leftItem )
435+ leftSubtree , err := buildSubtree ( leftArray )
515436 if err != nil {
516- return nil , fmt .Errorf ("failed to convert left tree item in row %d : %w" , i , err )
437+ return nil , fmt .Errorf ("failed to build left subtree : %w" , err )
517438 }
439+ leftNode = leftSubtree
440+ } else {
441+ leftNode , err = processLeaf (topology [0 ])
442+ if err != nil {
443+ return nil , fmt .Errorf ("failed to process left leaf: %w" , err )
444+ }
445+ }
518446
519- rightItem , ok := rowArray [1 ].(map [string ]interface {})
520- if ! ok {
521- return nil , fmt .Errorf ("invalid right item in topology row %d" , i )
447+ // Check if the second element is an array
448+ if rightArray , isArray := topology [1 ].([]interface {}); isArray {
449+ if len (rightArray ) != 2 {
450+ return nil , fmt .Errorf ("right topology row must have exactly two elements" )
522451 }
523- rightNode , err := convertTree ( rightItem )
452+ rightSubtree , err := buildSubtree ( rightArray )
524453 if err != nil {
525- return nil , fmt .Errorf ("failed to convert right tree item in row %d : %w" , i , err )
454+ return nil , fmt .Errorf ("failed to build right subtree : %w" , err )
526455 }
527-
528- subtree := map [string ]interface {}{
529- "left" : leftNode ,
530- "right" : rightNode ,
456+ rightNode = rightSubtree
457+ } else {
458+ rightNode , err = processLeaf (topology [1 ])
459+ if err != nil {
460+ return nil , fmt .Errorf ("failed to process right leaf: %w" , err )
531461 }
532- subtrees = append (subtrees , subtree )
533- }
534-
535- if len (subtrees ) != 2 {
536- return nil , fmt .Errorf ("expected exactly two subtrees from topology" )
537462 }
538463
539464 tree := map [string ]interface {}{
540- "left" : subtrees [ 0 ] ,
541- "right" : subtrees [ 1 ] ,
465+ "left" : leftNode ,
466+ "right" : rightNode ,
542467 }
543-
544468 rawConfig ["tree" ] = tree
545469 delete (rawConfig , "topology" )
546470 }
547471
548472 return rawConfig , nil
549473}
550474
475+ // buildSubtree builds a subtree from an array of two elements
476+ func buildSubtree (row []interface {}) (map [string ]interface {}, error ) {
477+ if len (row ) != 2 {
478+ return nil , fmt .Errorf ("subtree row must have exactly two elements" )
479+ }
480+ leftItem , ok := row [0 ].(map [string ]interface {})
481+ if ! ok {
482+ return nil , fmt .Errorf ("invalid left item in subtree" )
483+ }
484+ leftNode , err := convertTree (leftItem )
485+ if err != nil {
486+ return nil , fmt .Errorf ("failed to convert left tree item: %w" , err )
487+ }
488+ rightItem , ok := row [1 ].(map [string ]interface {})
489+ if ! ok {
490+ return nil , fmt .Errorf ("invalid right item in subtree" )
491+ }
492+ rightNode , err := convertTree (rightItem )
493+ if err != nil {
494+ return nil , fmt .Errorf ("failed to convert right tree item: %w" , err )
495+ }
496+ return map [string ]interface {}{
497+ "left" : leftNode ,
498+ "right" : rightNode ,
499+ }, nil
500+ }
501+
502+ func processLeaf (item interface {}) (map [string ]interface {}, error ) {
503+ switch v := item .(type ) {
504+ case map [string ]interface {}:
505+ return convertTree (v )
506+ case string :
507+ return map [string ]interface {}{
508+ "type" : "node" ,
509+ "hash" : v ,
510+ }, nil
511+ default :
512+ return nil , fmt .Errorf ("unsupported leaf type: %T" , v )
513+ }
514+ }
515+
516+ // calculateImageHash calculates the image hash for a given configuration
517+ func calculateImageHash (params * ConfigImageHashParams ) (string , error ) {
518+ rawConfig , err := handleRawConfig (params .Input )
519+ if err != nil {
520+ return "" , fmt .Errorf ("failed to handle raw config: %w" , err )
521+ }
522+
523+ config , err := v3 .Core .DecodeWalletConfig (rawConfig )
524+ if err != nil {
525+ return "" , fmt .Errorf ("failed to decode wallet config: %w" , err )
526+ }
527+
528+ log .Printf ("Decoded config: %+v" , config )
529+ spew .Dump (config )
530+
531+ imageHash := config .ImageHash ()
532+ return "0x" + common .Bytes2Hex (imageHash .Bytes ()), nil
533+ }
534+
551535// encodeConfig encodes a configuration into a signature format
552536func encodeConfig (params * ConfigEncodeParams ) (string , error ) {
553537 rawConfig , err := handleRawConfig (params .Input )
0 commit comments