keycmd.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "net"
  20. "github.com/ethereum/go-ethereum/crypto"
  21. "github.com/ethereum/go-ethereum/p2p/enode"
  22. "gopkg.in/urfave/cli.v1"
  23. )
  24. var (
  25. keyCommand = cli.Command{
  26. Name: "key",
  27. Usage: "Operations on node keys",
  28. Subcommands: []cli.Command{
  29. keyGenerateCommand,
  30. keyToNodeCommand,
  31. },
  32. }
  33. keyGenerateCommand = cli.Command{
  34. Name: "generate",
  35. Usage: "Generates node key files",
  36. ArgsUsage: "keyfile",
  37. Action: genkey,
  38. }
  39. keyToNodeCommand = cli.Command{
  40. Name: "to-enode",
  41. Usage: "Creates an enode URL from a node key file",
  42. ArgsUsage: "keyfile",
  43. Action: keyToURL,
  44. Flags: []cli.Flag{hostFlag, tcpPortFlag, udpPortFlag},
  45. }
  46. )
  47. var (
  48. hostFlag = cli.StringFlag{
  49. Name: "ip",
  50. Usage: "IP address of the node",
  51. Value: "127.0.0.1",
  52. }
  53. tcpPortFlag = cli.IntFlag{
  54. Name: "tcp",
  55. Usage: "TCP port of the node",
  56. Value: 30303,
  57. }
  58. udpPortFlag = cli.IntFlag{
  59. Name: "udp",
  60. Usage: "UDP port of the node",
  61. Value: 30303,
  62. }
  63. )
  64. func genkey(ctx *cli.Context) error {
  65. if ctx.NArg() != 1 {
  66. return fmt.Errorf("need key file as argument")
  67. }
  68. file := ctx.Args().Get(0)
  69. key, err := crypto.GenerateKey()
  70. if err != nil {
  71. return fmt.Errorf("could not generate key: %v", err)
  72. }
  73. return crypto.SaveECDSA(file, key)
  74. }
  75. func keyToURL(ctx *cli.Context) error {
  76. if ctx.NArg() != 1 {
  77. return fmt.Errorf("need key file as argument")
  78. }
  79. var (
  80. file = ctx.Args().Get(0)
  81. host = ctx.String(hostFlag.Name)
  82. tcp = ctx.Int(tcpPortFlag.Name)
  83. udp = ctx.Int(udpPortFlag.Name)
  84. )
  85. key, err := crypto.LoadECDSA(file)
  86. if err != nil {
  87. return err
  88. }
  89. ip := net.ParseIP(host)
  90. if ip == nil {
  91. return fmt.Errorf("invalid IP address %q", host)
  92. }
  93. node := enode.NewV4(&key.PublicKey, ip, tcp, udp)
  94. fmt.Println(node.URLv4())
  95. return nil
  96. }