From e490233414eb9dfd48eade2d23d053f03aed4c47 Mon Sep 17 00:00:00 2001 From: Anton Evangelatov Date: Mon, 4 Dec 2017 14:25:42 +0100 Subject: [PATCH] p2p/enr: all predefined keys in one file --- p2p/enr/discv5.go | 23 ------------- p2p/enr/id.go | 23 ------------- p2p/enr/ip6.go | 48 -------------------------- p2p/enr/{ip4.go => keys.go} | 68 +++++++++++++++++++++++++++++++++++++ p2p/enr/secp256k1.go | 53 ----------------------------- 5 files changed, 68 insertions(+), 147 deletions(-) delete mode 100644 p2p/enr/discv5.go delete mode 100644 p2p/enr/id.go delete mode 100644 p2p/enr/ip6.go rename p2p/enr/{ip4.go => keys.go} (52%) delete mode 100644 p2p/enr/secp256k1.go diff --git a/p2p/enr/discv5.go b/p2p/enr/discv5.go deleted file mode 100644 index 575ff32cab..0000000000 --- a/p2p/enr/discv5.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package enr - -type DiscPort uint16 - -func (DiscPort) ENRKey() string { - return "discv5" -} diff --git a/p2p/enr/id.go b/p2p/enr/id.go deleted file mode 100644 index b9ac0bbfae..0000000000 --- a/p2p/enr/id.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package enr - -type ID string - -func (ID) ENRKey() string { - return "id" -} diff --git a/p2p/enr/ip6.go b/p2p/enr/ip6.go deleted file mode 100644 index 2b5803c2b1..0000000000 --- a/p2p/enr/ip6.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package enr - -import ( - "fmt" - "io" - "net" - - "github.com/ethereum/go-ethereum/rlp" -) - -// IP6 represents an 16-byte IPv6 address in a node record. -type IP6 net.IP - -// ENRKey returns the node record key for an IPv6 address. -func (IP6) ENRKey() string { - return "ip6" -} - -func (v IP6) EncodeRLP(w io.Writer) error { - ip6 := net.IP(v) - return rlp.Encode(w, ip6) -} - -func (v *IP6) DecodeRLP(s *rlp.Stream) error { - if err := s.Decode((*net.IP)(v)); err != nil { - return err - } - if len(*v) != 16 { - return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v) - } - return nil -} diff --git a/p2p/enr/ip4.go b/p2p/enr/keys.go similarity index 52% rename from p2p/enr/ip4.go rename to p2p/enr/keys.go index ae27734d45..28b140ba4b 100644 --- a/p2p/enr/ip4.go +++ b/p2p/enr/keys.go @@ -17,13 +17,29 @@ package enr import ( + "crypto/ecdsa" "fmt" "io" "net" + "github.com/btcsuite/btcd/btcec" "github.com/ethereum/go-ethereum/rlp" ) +// DiscPort represents an UDP port for discovery v5. +type DiscPort uint16 + +func (DiscPort) ENRKey() string { + return "discv5" +} + +// ID is the name of identity scheme, e.g. "secp256k1-keccak". +type ID string + +func (ID) ENRKey() string { + return "id" +} + // IP4 represents an 4-byte IPv4 address in a node record. type IP4 net.IP @@ -49,3 +65,55 @@ func (v *IP4) DecodeRLP(s *rlp.Stream) error { } return nil } + +// IP6 represents an 16-byte IPv6 address in a node record. +type IP6 net.IP + +// ENRKey returns the node record key for an IPv6 address. +func (IP6) ENRKey() string { + return "ip6" +} + +func (v IP6) EncodeRLP(w io.Writer) error { + ip6 := net.IP(v) + return rlp.Encode(w, ip6) +} + +func (v *IP6) DecodeRLP(s *rlp.Stream) error { + if err := s.Decode((*net.IP)(v)); err != nil { + return err + } + if len(*v) != 16 { + return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v) + } + return nil +} + +// Secp256k1 is compressed secp256k1 public key. +type Secp256k1 ecdsa.PublicKey + +func (Secp256k1) ENRKey() string { + return "secp256k1" +} + +func (v Secp256k1) EncodeRLP(w io.Writer) error { + pk := btcec.PublicKey(v) + + return rlp.Encode(w, pk.SerializeCompressed()) +} + +func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error { + buf := make([]byte, 33) + if err := s.Decode(&buf); err != nil { + return err + } + + pk, err := btcec.ParsePubKey(buf, btcec.S256()) + if err != nil { + return err + } + + *v = (Secp256k1)(*pk) + + return nil +} diff --git a/p2p/enr/secp256k1.go b/p2p/enr/secp256k1.go deleted file mode 100644 index 1f694f968d..0000000000 --- a/p2p/enr/secp256k1.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2015 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package enr - -import ( - "crypto/ecdsa" - "io" - - "github.com/btcsuite/btcd/btcec" - "github.com/ethereum/go-ethereum/rlp" -) - -type Secp256k1 ecdsa.PublicKey - -func (Secp256k1) ENRKey() string { - return "secp256k1" -} - -func (v Secp256k1) EncodeRLP(w io.Writer) error { - pk := btcec.PublicKey(v) - - return rlp.Encode(w, pk.SerializeCompressed()) -} - -func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error { - buf := make([]byte, 33) - if err := s.Decode(&buf); err != nil { - return err - } - - pk, err := btcec.ParsePubKey(buf, btcec.S256()) - if err != nil { - return err - } - - *v = (Secp256k1)(*pk) - - return nil -}