From 795d35502930352d0a485c07bd0572ca25da5aac Mon Sep 17 00:00:00 2001
From: KonradStaniec <konrad.staniec@gmail.com>
Date: Mon, 23 Dec 2024 09:21:31 +0100
Subject: [PATCH 1/3] bump signer dependecy to latest version

---
 go.mod | 2 +-
 go.sum | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/go.mod b/go.mod
index a184a5c..3297382 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
 	cosmossdk.io/math v1.4.0
 	github.com/avast/retry-go/v4 v4.5.1
 	github.com/babylonlabs-io/babylon v1.0.0-rc.1
-	github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.2.0
+	github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.3.0
 	github.com/btcsuite/btcd v0.24.2
 	github.com/btcsuite/btcd/btcec/v2 v2.3.2
 	github.com/btcsuite/btcd/btcutil v1.1.6
diff --git a/go.sum b/go.sum
index 29e8d16..885ebef 100644
--- a/go.sum
+++ b/go.sum
@@ -1421,8 +1421,8 @@ github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8
 github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
 github.com/babylonlabs-io/babylon v1.0.0-rc.1 h1:esV0UJ1TmXfVkfKWOee/2ny+GMiqZXNni8f2H4ASD38=
 github.com/babylonlabs-io/babylon v1.0.0-rc.1/go.mod h1:B8ma8IjGUEKhmoRfwv60Qa7DtUXssCgtmD89huQ4+5I=
-github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.2.0 h1:bIosLYR/HUDN4gtUMqbPZjDKapcTHVJHFs0o9AGJt+U=
-github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.2.0/go.mod h1:9lAyEcdpfS21bMLMEa8WjTyLVfwHJABRh5TmoxC9LKU=
+github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.3.0 h1:WS0Hcb8VZHTJJ470NyFKrqOZfEpN7DG4x7RYLe8nXs4=
+github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.3.0/go.mod h1:Ri87bBAOtpKeNz7BtQcHs4poob+OBX/ALgZJxOINq2g=
 github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=

From 8ea6196530bbf5f61a7932602020dd1a14d67bf3 Mon Sep 17 00:00:00 2001
From: KonradStaniec <konrad.staniec@gmail.com>
Date: Mon, 23 Dec 2024 09:55:48 +0100
Subject: [PATCH 2/3] fix param version translation

---
 clientcontroller/babylon.go      |  1 +
 clientcontroller/babylon_test.go | 68 ++++++++++++++++++++++++++++++++
 covenant/covenant_test.go        |  2 +
 3 files changed, 71 insertions(+)
 create mode 100644 clientcontroller/babylon_test.go

diff --git a/clientcontroller/babylon.go b/clientcontroller/babylon.go
index 19adb0d..2b7efaa 100644
--- a/clientcontroller/babylon.go
+++ b/clientcontroller/babylon.go
@@ -290,6 +290,7 @@ func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*ty
 		CovenantSigs:     covenantSigs,
 		UnbondingTime:    uint16(del.UnbondingTime),
 		BtcUndelegation:  undelegation,
+		ParamsVersion:    del.ParamsVersion,
 	}, nil
 }
 
diff --git a/clientcontroller/babylon_test.go b/clientcontroller/babylon_test.go
new file mode 100644
index 0000000..6a3cf8e
--- /dev/null
+++ b/clientcontroller/babylon_test.go
@@ -0,0 +1,68 @@
+package clientcontroller_test
+
+import (
+	"math/rand"
+	"testing"
+
+	"github.com/babylonlabs-io/babylon/testutil/datagen"
+	bbntypes "github.com/babylonlabs-io/babylon/types"
+	"github.com/babylonlabs-io/babylon/x/btcstaking/types"
+	"github.com/babylonlabs-io/covenant-emulator/clientcontroller"
+	"github.com/babylonlabs-io/covenant-emulator/testutil"
+	"github.com/stretchr/testify/require"
+)
+
+func TestBabylonController(t *testing.T) {
+
+	// datagen.GenRandomBTCDelegation()
+
+	require.Equal(t, 1, 1)
+}
+
+func FuzzAddCovenantSig(f *testing.F) {
+	testutil.AddRandomSeedsToFuzzer(f, 10)
+	f.Fuzz(func(t *testing.T, seed int64) {
+		r := rand.New(rand.NewSource(seed))
+
+		delPub, err := datagen.GenRandomBIP340PubKey(r)
+		require.NoError(t, err)
+
+		fpPub, err := datagen.GenRandomBIP340PubKey(r)
+		require.NoError(t, err)
+
+		randSat := datagen.RandomInRange(r, 10000, 10000000)
+		randVersion := datagen.RandomInRange(r, 1, 10)
+		stakingTx := datagen.GenRandomHexStr(r, 100)
+		slashingTx := datagen.GenRandomHexStr(r, 100)
+		stakingTime := datagen.RandomInRange(r, 100, 1000000)
+		startHeight := datagen.RandomInRange(r, 100, 1000000)
+		endHeight := datagen.RandomInRange(r, 100, 1000000)
+		stakingOutputIdx := datagen.RandomInRange(r, 100, 1000000)
+
+		response := types.BTCDelegationResponse{
+			BtcPk:            delPub,
+			FpBtcPkList:      []bbntypes.BIP340PubKey{*fpPub},
+			TotalSat:         uint64(randSat),
+			ParamsVersion:    uint32(randVersion),
+			StakingTxHex:     stakingTx,
+			SlashingTxHex:    slashingTx,
+			StakingTime:      uint32(stakingTime),
+			StartHeight:      uint32(startHeight),
+			EndHeight:        uint32(endHeight),
+			StakingOutputIdx: uint32(stakingOutputIdx),
+		}
+
+		del, err := clientcontroller.DelegationRespToDelegation(&response)
+		require.NoError(t, err)
+		require.NotNil(t, del)
+		require.Equal(t, response.ParamsVersion, del.ParamsVersion)
+		require.Equal(t, response.StakingTime, del.StakingTime)
+		require.Equal(t, response.StartHeight, del.StartHeight)
+		require.Equal(t, response.EndHeight, del.EndHeight)
+		require.Equal(t, response.StakingOutputIdx, del.StakingOutputIdx)
+		require.Equal(t, response.StakingTxHex, del.StakingTxHex)
+		require.Equal(t, response.SlashingTxHex, del.SlashingTxHex)
+		require.Equal(t, response.BtcPk, bbntypes.NewBIP340PubKeyFromBTCPK(del.BtcPk))
+		require.Equal(t, response.FpBtcPkList, bbntypes.NewBIP340PKsFromBTCPKs(del.FpBtcPks))
+	})
+}
diff --git a/covenant/covenant_test.go b/covenant/covenant_test.go
index 62ad46d..6785288 100644
--- a/covenant/covenant_test.go
+++ b/covenant/covenant_test.go
@@ -91,6 +91,7 @@ func FuzzAddCovenantSig(f *testing.F) {
 			startHeight := uint32(datagen.RandomInt(r, 1000) + 100)
 			stakingOutputIdx, err := bbntypes.GetOutputIdxInBTCTx(testInfo.StakingTx, testInfo.StakingInfo.StakingOutput)
 			require.NoError(t, err)
+			randParamsVersion := datagen.RandomInRange(r, 1, 10)
 			btcDel := &types.Delegation{
 				BtcPk:            delPK,
 				FpBtcPks:         fpPks,
@@ -102,6 +103,7 @@ func FuzzAddCovenantSig(f *testing.F) {
 				StakingTxHex:     hex.EncodeToString(stakingTxBytes),
 				StakingOutputIdx: stakingOutputIdx,
 				SlashingTxHex:    testInfo.SlashingTx.ToHexStr(),
+				ParamsVersion:    uint32(randParamsVersion),
 			}
 			btcDels = append(btcDels, btcDel)
 			// generate covenant staking sigs

From 9e2835adc12f19ccb49db7d02a09ed49bc62d433 Mon Sep 17 00:00:00 2001
From: KonradStaniec <konrad.staniec@gmail.com>
Date: Mon, 23 Dec 2024 09:57:43 +0100
Subject: [PATCH 3/3] clean up and change log update

---
 CHANGELOG.md                     | 5 +++++
 clientcontroller/babylon_test.go | 9 +--------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78afe6a..04fb7c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -37,6 +37,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 
 ## Unreleased
 
+### Bug fixes
+
+* [#71](https://github.com/babylonlabs-io/covenant-emulator/pull/71) Fix delegation
+response to delegation conversion
+
 ## v0.11.0
 
 ### Improvements
diff --git a/clientcontroller/babylon_test.go b/clientcontroller/babylon_test.go
index 6a3cf8e..8a99f8f 100644
--- a/clientcontroller/babylon_test.go
+++ b/clientcontroller/babylon_test.go
@@ -12,14 +12,7 @@ import (
 	"github.com/stretchr/testify/require"
 )
 
-func TestBabylonController(t *testing.T) {
-
-	// datagen.GenRandomBTCDelegation()
-
-	require.Equal(t, 1, 1)
-}
-
-func FuzzAddCovenantSig(f *testing.F) {
+func FuzzDelegationRespToDelegation(f *testing.F) {
 	testutil.AddRandomSeedsToFuzzer(f, 10)
 	f.Fuzz(func(t *testing.T, seed int64) {
 		r := rand.New(rand.NewSource(seed))