enr

package
v0.1.43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ENRCmd = &cobra.Command{
	Use:   "enr [flags]",
	Short: "Convert between ENR and Enode format",
	Long:  usage,
	RunE: func(cmd *cobra.Command, args []string) error {
		rawData, err := getInputData(cmd, args)
		if err != nil {
			log.Error().Err(err).Msg("Unable to read input")
			return err
		}
		lines := strings.Split(string(rawData), "\n")

		for _, l := range lines {
			var node *enode.Node
			var err error
			l = strings.TrimSpace(l)
			if l == "" {
				continue
			}
			isENR := false
			if strings.HasPrefix(l, "enr:") {
				isENR = true
				node, err = enode.Parse(enode.V4ID{}, l)
				if err != nil {
					log.Error().Err(err).Str("line", l).Msg("Unable to parse enr record")
					continue
				}
			} else {
				node, err = enode.ParseV4(l)
				if err != nil {
					log.Error().Err(err).Str("line", l).Msg("Unable to parse node record")
					continue
				}
			}
			genericNode := make(map[string]string, 0)
			if isENR {
				genericNode["enr"] = node.String()
			}
			genericNode["enode"] = node.URLv4()
			genericNode["id"] = node.ID().String()
			genericNode["ip"] = node.IP().String()
			genericNode["tcp"] = fmt.Sprintf("%d", node.TCP())
			genericNode["udp"] = fmt.Sprintf("%d", node.UDP())
			jsonOut, err := json.Marshal(genericNode)
			if err != nil {
				log.Error().Err(err).Msg("Unable to convert node to json")
				continue
			}
			fmt.Println(string(jsonOut))
		}
		return nil
	},
	Args: func(cmd *cobra.Command, args []string) error {
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL