Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Signer tests #373

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions signerv2/mockdata/dummy.key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {
"iv": "6087dab2f9fdbbfaddc31a909735c1e6"
},
"ciphertext": "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46",
"kdf": "pbkdf2",
"kdfparams": {
"c": 262144,
"dklen": 32,
"prf": "hmac-sha256",
"salt": "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"
},
"mac": "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2"
},
"id": "3198bc9c-6672-5ab3-d995-4942343ae5b6",
"version": 3
}
96 changes: 96 additions & 0 deletions signerv2/signer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package signerv2_test

import (
"context"
"math/big"
"testing"

"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/Layr-Labs/eigensdk-go/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)

func TestPrivateKeySignerFn(t *testing.T) {
privateKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
privateKey, err := crypto.HexToECDSA(privateKeyHex)
require.NoError(t, err)
chainID := big.NewInt(1)

signer, err := signerv2.PrivateKeySignerFn(privateKey, chainID)
require.NoError(t, err)

address := crypto.PubkeyToAddress(privateKey.PublicKey)
tx := types.NewTx(&types.DynamicFeeTx{
Nonce: 0,
Value: big.NewInt(0),
ChainID: chainID,
Data: common.Hex2Bytes("6057361d00000000000000000000000000000000000000000000000000000000000f4240"),
})
signedTx, err := signer(address, tx)
require.NoError(t, err)

// Verify the sender address of the signed transaction
from, err := types.Sender(types.LatestSignerForChainID(chainID), signedTx)
require.NoError(t, err)
require.Equal(t, address, from)
}

func TestKeyStoreSignerFn(t *testing.T) {
keystorePath := "mockdata/dummy.key.json"
keystorePassword := "testpassword"
chainID := big.NewInt(1)
signer, err := signerv2.KeyStoreSignerFn(keystorePath, keystorePassword, chainID)
require.NoError(t, err)

privateKey, err := crypto.HexToECDSA("7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d")
require.NoError(t, err)

address := crypto.PubkeyToAddress(privateKey.PublicKey)
tx := types.NewTx(&types.DynamicFeeTx{
Nonce: 0,
Value: big.NewInt(0),
ChainID: chainID,
Data: common.Hex2Bytes("6057361d00000000000000000000000000000000000000000000000000000000000f4240"),
})
signedTx, err := signer(address, tx)
require.NoError(t, err)

// Verify the sender address of the signed transaction
from, err := types.Sender(types.LatestSignerForChainID(chainID), signedTx)
require.NoError(t, err)
require.Equal(t, address, from)
}

func TestWeb3SignerFn(t *testing.T) {
anvilC, err := testutils.StartAnvilContainer(testutils.GetDefaultTestConfig().AnvilStateFileName)
require.NoError(t, err)

anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http")
require.NoError(t, err)

signer, err := signerv2.Web3SignerFn(anvilHttpEndpoint)
require.NoError(t, err)

privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
require.NoError(t, err)
anvilChainID := big.NewInt(31337)
address := crypto.PubkeyToAddress(privateKey.PublicKey)
tx := types.NewTx(&types.DynamicFeeTx{
Nonce: 0,
Value: big.NewInt(0),
To: &address,
ChainID: anvilChainID,
Data: common.Hex2Bytes("6057361d00000000000000000000000000000000000000000000000000000000000f4240"),
})

signedTx, err := signer(address, tx)
require.NoError(t, err)

// Verify the sender address of the signed transaction
from, err := types.Sender(types.LatestSignerForChainID(anvilChainID), signedTx)
require.NoError(t, err)
require.Equal(t, address, from)
}