github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/p2p/enode/idscheme_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enode
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/ecdsa"
    22  	"encoding/hex"
    23  	"math/big"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/scroll-tech/go-ethereum/crypto"
    30  	"github.com/scroll-tech/go-ethereum/p2p/enr"
    31  	"github.com/scroll-tech/go-ethereum/rlp"
    32  )
    33  
    34  var (
    35  	privkey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    36  	pubkey     = &privkey.PublicKey
    37  )
    38  
    39  func TestEmptyNodeID(t *testing.T) {
    40  	var r enr.Record
    41  	if addr := ValidSchemes.NodeAddr(&r); addr != nil {
    42  		t.Errorf("wrong address on empty record: got %v, want %v", addr, nil)
    43  	}
    44  
    45  	require.NoError(t, SignV4(&r, privkey))
    46  	expected := "a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7"
    47  	assert.Equal(t, expected, hex.EncodeToString(ValidSchemes.NodeAddr(&r)))
    48  }
    49  
    50  // Checks that failure to sign leaves the record unmodified.
    51  func TestSignError(t *testing.T) {
    52  	invalidKey := &ecdsa.PrivateKey{D: new(big.Int), PublicKey: *pubkey}
    53  
    54  	var r enr.Record
    55  	emptyEnc, _ := rlp.EncodeToBytes(&r)
    56  	if err := SignV4(&r, invalidKey); err == nil {
    57  		t.Fatal("expected error from SignV4")
    58  	}
    59  	newEnc, _ := rlp.EncodeToBytes(&r)
    60  	if !bytes.Equal(newEnc, emptyEnc) {
    61  		t.Fatal("record modified even though signing failed")
    62  	}
    63  }
    64  
    65  // TestGetSetSecp256k1 tests encoding/decoding and setting/getting of the Secp256k1 key.
    66  func TestGetSetSecp256k1(t *testing.T) {
    67  	var r enr.Record
    68  	if err := SignV4(&r, privkey); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	var pk Secp256k1
    73  	require.NoError(t, r.Load(&pk))
    74  	assert.EqualValues(t, pubkey, &pk)
    75  }