github.com/prysmaticlabs/prysm@v1.4.4/shared/depositutil/deposit_test.go (about)

     1  package depositutil_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
     7  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
     8  	"github.com/prysmaticlabs/prysm/shared/bls"
     9  	"github.com/prysmaticlabs/prysm/shared/depositutil"
    10  	"github.com/prysmaticlabs/prysm/shared/params"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    14  )
    15  
    16  func TestDepositInput_GeneratesPb(t *testing.T) {
    17  	k1, err := bls.RandKey()
    18  	require.NoError(t, err)
    19  	k2, err := bls.RandKey()
    20  	require.NoError(t, err)
    21  
    22  	result, _, err := depositutil.DepositInput(k1, k2, 0)
    23  	require.NoError(t, err)
    24  	assert.DeepEqual(t, k1.PublicKey().Marshal(), result.PublicKey)
    25  
    26  	sig, err := bls.SignatureFromBytes(result.Signature)
    27  	require.NoError(t, err)
    28  	testData := &pb.DepositMessage{
    29  		PublicKey:             result.PublicKey,
    30  		WithdrawalCredentials: result.WithdrawalCredentials,
    31  		Amount:                result.Amount,
    32  	}
    33  	sr, err := testData.HashTreeRoot()
    34  	require.NoError(t, err)
    35  	domain, err := helpers.ComputeDomain(
    36  		params.BeaconConfig().DomainDeposit,
    37  		nil, /*forkVersion*/
    38  		nil, /*genesisValidatorsRoot*/
    39  	)
    40  	require.NoError(t, err)
    41  	root, err := (&pb.SigningData{ObjectRoot: sr[:], Domain: domain}).HashTreeRoot()
    42  	require.NoError(t, err)
    43  	assert.Equal(t, true, sig.Verify(k1.PublicKey(), root[:]))
    44  }
    45  
    46  func TestVerifyDepositSignature_ValidSig(t *testing.T) {
    47  	deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
    48  	require.NoError(t, err)
    49  	deposit := deposits[0]
    50  	domain, err := helpers.ComputeDomain(
    51  		params.BeaconConfig().DomainDeposit,
    52  		params.BeaconConfig().GenesisForkVersion,
    53  		params.BeaconConfig().ZeroHash[:],
    54  	)
    55  	require.NoError(t, err)
    56  	err = depositutil.VerifyDepositSignature(deposit.Data, domain)
    57  	require.NoError(t, err)
    58  }
    59  
    60  func TestVerifyDepositSignature_InvalidSig(t *testing.T) {
    61  	deposits, _, err := testutil.DeterministicDepositsAndKeys(1)
    62  	require.NoError(t, err)
    63  	deposit := deposits[0]
    64  	domain, err := helpers.ComputeDomain(
    65  		params.BeaconConfig().DomainDeposit,
    66  		params.BeaconConfig().GenesisForkVersion,
    67  		params.BeaconConfig().ZeroHash[:],
    68  	)
    69  	require.NoError(t, err)
    70  	deposit.Data.Signature = deposit.Data.Signature[1:]
    71  	err = depositutil.VerifyDepositSignature(deposit.Data, domain)
    72  	if err == nil {
    73  		t.Fatal("Deposit Verification succeeds with a invalid signature")
    74  	}
    75  }