p2p/enr: all predefined keys in one file

This commit is contained in:
Anton Evangelatov 2017-12-04 14:25:42 +01:00 committed by Felix Lange
parent bd9696c33d
commit e490233414
5 changed files with 68 additions and 147 deletions

View file

@ -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 <http://www.gnu.org/licenses/>.
package enr
type DiscPort uint16
func (DiscPort) ENRKey() string {
return "discv5"
}

View file

@ -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 <http://www.gnu.org/licenses/>.
package enr
type ID string
func (ID) ENRKey() string {
return "id"
}

View file

@ -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 <http://www.gnu.org/licenses/>.
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
}

View file

@ -17,13 +17,29 @@
package enr package enr
import ( import (
"crypto/ecdsa"
"fmt" "fmt"
"io" "io"
"net" "net"
"github.com/btcsuite/btcd/btcec"
"github.com/ethereum/go-ethereum/rlp" "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. // IP4 represents an 4-byte IPv4 address in a node record.
type IP4 net.IP type IP4 net.IP
@ -49,3 +65,55 @@ func (v *IP4) DecodeRLP(s *rlp.Stream) error {
} }
return nil 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
}

View file

@ -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 <http://www.gnu.org/licenses/>.
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
}