Skip to content

Commit

Permalink
add e2e test with encrypted keyring (#45)
Browse files Browse the repository at this point in the history
* add e2e test with encrypted keyring
  • Loading branch information
KonradStaniec authored Nov 27, 2024
1 parent 6b2d632 commit 93839a8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
private key to the cosmos keyring
* [#44](https://github.com/babylonlabs-io/covenant-emulator/pull/44) Command
to derive child private keys from the master key
* [#45](https://github.com/babylonlabs-io/covenant-emulator/pull/45) Add e2e test
with encrypted file keyring usage

## v0.9.0

Expand Down
25 changes: 22 additions & 3 deletions covenant-signer/itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func buildDataToSign(t *testing.T, covnenantPublicKey *btcec.PublicKey) signerap
}

func TestGetPublicKey(t *testing.T) {
tm := StartManager(t, 100)
tm := StartManager(t, 100, false)

pubKey, err := signerservice.GetPublicKey(context.Background(), tm.SigningServerUrl(), 10*time.Second)
require.NoError(t, err)
Expand All @@ -141,7 +141,7 @@ func TestGetPublicKey(t *testing.T) {
}

func TestSigningTransactions(t *testing.T) {
tm := StartManager(t, 100)
tm := StartManager(t, 100, false)

dataToSign := buildDataToSign(t, tm.covenantPrivKey.PubKey())

Expand All @@ -160,7 +160,7 @@ func TestSigningTransactions(t *testing.T) {
}

func TestRejectToLargeRequest(t *testing.T) {
tm := StartManager(t, 100)
tm := StartManager(t, 100, false)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tmContentLimit := tm.signerConfig.Server.MaxContentLength
size := tmContentLimit + 1
Expand Down Expand Up @@ -190,3 +190,22 @@ func TestRejectToLargeRequest(t *testing.T) {
defer res.Body.Close()
require.Equal(t, http.StatusRequestEntityTooLarge, res.StatusCode)
}

func TestSigningTransactionsUsingEncryptedFileKeyRing(t *testing.T) {
tm := StartManager(t, 100, true)

dataToSign := buildDataToSign(t, tm.covenantPrivKey.PubKey())

sigs, err := signerservice.RequestCovenantSignaure(
context.Background(),
tm.SigningServerUrl(),
10*time.Second,
&dataToSign,
)

require.NoError(t, err)
require.NotNil(t, sigs)

err = tm.verifyResponse(sigs, &dataToSign)
require.NoError(t, err)
}
9 changes: 9 additions & 0 deletions covenant-signer/itest/testkeyring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Folder for testing encrypted file keyring.

Contains one key with name `test` and passphrase `testtest`.

Key was created by calling `btcec.NewPrivateKey()`, encoded to hex and importing
to cosmos keyring by using :
```
./build/babylond keys --keyring-backend=file --keyring-dir=babylon/testkeyring import-hex test d9d4221f4f1c98d6ce4b9390038f3b08b2c47156fd29bb417d03bf6118923f3f
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyNC0xMS0yNyAwODowMjozMC4zMzM1MTEgKzAxMDAgQ0VUIG09KzguMjUxOTc2NzkzIiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoiQ1NVRjRRNzJpMGhEWWFLZCJ9.3_kww_63YyTRioPbpVZJwSN8uf7gjdTzDg7OnM46netc4hrCSCNouQ.zEdN0b0Tlb4vjgAn.YOVylHtTQK90NDCNfJFE40S48DwStDD-c9orZvWLiHYud1YE_GAhsVYCZccYicL5bEnPvMdGCzMvA_V_5Iz-QwR2-0ofhkyO4a5LNtaS-iir3wS33rj112C_-ajoiMrrq1SkCh3VOMTpGdSCD5lpn5POEQMPAQhBQurNF2BBMCXYsMzwo2bR7oiMeWuKmHy_Z1s7irgukSCUMfe6gBwg-BoemJuNXXn2-pge38NP_chuoQ.2OotwwK0_SJ4OUIOB7dIig
1 change: 1 addition & 0 deletions covenant-signer/itest/testkeyring/keyring-file/keyhash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$2a$10$xXYahf5o27DTopn6gS2hi.TCgO16g6LI4yMRplGBOCTBH6XaFqcN2
1 change: 1 addition & 0 deletions covenant-signer/itest/testkeyring/keyring-file/test.info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJjcmVhdGVkIjoiMjAyNC0xMS0yNyAwODowMjozMC4zMzE2MzMgKzAxMDAgQ0VUIG09KzguMjUwMDk5Mzc2IiwiZW5jIjoiQTI1NkdDTSIsInAyYyI6ODE5MiwicDJzIjoib2ZXRjBhVnhMb01NWDFKdCJ9.bX9gLW-hEN6zmPJgxYKhYoKGuT9CXFxPctqEmeh943ZUBH7tT_mnXg.wUbZHCZ3E7Nx7SSH.1n6drbvjR-x2Fc-Bqx4ejrwOPC_XTBSTl7Z4hoZWHd3AwAbkwiFKxjzlyHGJ1k8ZP5bo3ukXT5f1R_lY7qr35W0HRQrhu1xG5l6lvo4xLQKYejhMG9yfJDVyKtBTrrNk7NlpoAVTeESO3QAIAtD1YAkw5TSdVy2od9RmlnujbelAE2TnydNDPYbECqSecJBzyr-YGvSiMlds0Fg70gBlcGRoVk1ZGDD93DxFha84sv2UIl6AWavcqDbNTYz4Sq2DxZpQLcet8YnirJV3m6a2a30Cz77Qy-83oAymrk5GmaCSL2TmTOuGzW4i1jtuC38EAZxOD1TJ-3aobWbasybg6SPi83y9lDVXogbJ-fOi0tY76ovDol2AypXbQ-r8W3qRLtqGkdhECV9vm1EX3v_b4gwERa_h3ahDUoneoRqiciA0T9Hho5uo1-yqrg.nIIVGQf_Q4I9-fCOa7AITg
63 changes: 42 additions & 21 deletions covenant-signer/itest/testmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ type TestManager struct {

func StartManager(
t *testing.T,
numMatureOutputsInWallet uint32) *TestManager {
numMatureOutputsInWallet uint32,
useEncryptedFileKeyRing bool,
) *TestManager {
m, err := containers.NewManager()
require.NoError(t, err)
t.Cleanup(func() {
Expand All @@ -55,27 +57,46 @@ func StartManager(
_ = h.GenerateBlocks(int(numMatureOutputsInWallet) + 100)

appConfig := config.DefaultConfig()
appConfig.KeyStore.KeyStoreType = "cosmos"
appConfig.KeyStore.CosmosKeyStore.ChainID = "test-chain"
appConfig.KeyStore.CosmosKeyStore.Passphrase = passphrase
appConfig.KeyStore.CosmosKeyStore.KeyName = "test-key"
appConfig.KeyStore.CosmosKeyStore.KeyDirectory = ""
appConfig.KeyStore.CosmosKeyStore.KeyringBackend = "memory"

retriever, err := cosmos.NewCosmosKeyringRetriever(appConfig.KeyStore.CosmosKeyStore)
require.NoError(t, err)

covPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

hexPrivKey := hex.EncodeToString(covPrivKey.Serialize())
var retriever *cosmos.CosmosKeyringRetriever

if useEncryptedFileKeyRing {
appConfig.KeyStore.KeyStoreType = "cosmos"
// ChainID does not influence keyring with key imported from hex string
appConfig.KeyStore.CosmosKeyStore.ChainID = ""
appConfig.KeyStore.CosmosKeyStore.Passphrase = "testtest"
appConfig.KeyStore.CosmosKeyStore.KeyName = "test"
appConfig.KeyStore.CosmosKeyStore.KeyDirectory = "./testkeyring"
appConfig.KeyStore.CosmosKeyStore.KeyringBackend = "file"

retriever, err = cosmos.NewCosmosKeyringRetriever(appConfig.KeyStore.CosmosKeyStore)
require.NoError(t, err)
} else {
appConfig.KeyStore.KeyStoreType = "cosmos"
appConfig.KeyStore.CosmosKeyStore.ChainID = "test-chain"
appConfig.KeyStore.CosmosKeyStore.Passphrase = passphrase
appConfig.KeyStore.CosmosKeyStore.KeyName = "test-key"
appConfig.KeyStore.CosmosKeyStore.KeyDirectory = ""
appConfig.KeyStore.CosmosKeyStore.KeyringBackend = "memory"

retriever, err = cosmos.NewCosmosKeyringRetriever(appConfig.KeyStore.CosmosKeyStore)
require.NoError(t, err)

covPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)

hexPrivKey := hex.EncodeToString(covPrivKey.Serialize())

// Import private key to keyring, from hex string
err = retriever.Kr.GetKeyring().ImportPrivKeyHex(
appConfig.KeyStore.CosmosKeyStore.KeyName,
hexPrivKey,
"secp256k1",
)
require.NoError(t, err)
}

// Import private key to keyring, from hex string
err = retriever.Kr.GetKeyring().ImportPrivKeyHex(
appConfig.KeyStore.CosmosKeyStore.KeyName,
hexPrivKey,
"secp256k1",
)
privKey, err := retriever.PrivKey(context.Background())
require.NoError(t, err)

app := signerapp.NewSignerApp(
Expand Down Expand Up @@ -110,7 +131,7 @@ func StartManager(
t: t,
bitcoindHandler: h,
walletPass: passphrase,
covenantPrivKey: covPrivKey,
covenantPrivKey: privKey,
signerConfig: appConfig,
app: app,
server: server,
Expand Down

0 comments on commit 93839a8

Please sign in to comment.