-
Notifications
You must be signed in to change notification settings - Fork 1
/
signtx.go
65 lines (60 loc) · 1.38 KB
/
signtx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"os"
"slices"
"github.com/davecgh/go-spew/spew"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)
func signTx(unsignedTxFile string) error {
f, err := os.Open(unsignedTxFile)
if err != nil {
return err
}
defer f.Close()
var rawTx struct {
Body struct {
Messages []map[string]any
}
}
if err := json.NewDecoder(f).Decode(&rawTx); err != nil {
return fmt.Errorf("JSON decode %s: %v", unsignedTxFile, err)
}
var types []string
for _, msg := range rawTx.Body.Messages {
findUnregisteredTypes(msg, &types)
}
fmt.Println("TODO: NEED TO REGISTER THESE TYPES", types)
var tx txtypes.Tx
if _, err := f.Seek(0, 0); err != nil {
return fmt.Errorf("seek %s: %v", unsignedTxFile, err)
}
if err := unmarshaler.Unmarshal(f, &tx); err != nil {
return fmt.Errorf("unmarshal tx: %v", err)
}
spew.Dump(tx)
return nil
}
func findUnregisteredTypes(m map[string]any, types *[]string) {
for k, v := range m {
if k == "@type" {
typeURL := v.(string)
if _, err := registry.Resolve(typeURL); err != nil {
if !slices.Contains(*types, typeURL) {
// type not registered, add to the list
*types = append(*types, typeURL)
}
}
continue
}
switch x := v.(type) {
case []map[string]any:
for _, m := range x {
findUnregisteredTypes(m, types)
}
case map[string]any:
findUnregisteredTypes(x, types)
}
}
}