diff --git a/pkg/chain/ethereum/tbtc.go b/pkg/chain/ethereum/tbtc.go index a970bbff6e..ec5c29d40f 100644 --- a/pkg/chain/ethereum/tbtc.go +++ b/pkg/chain/ethereum/tbtc.go @@ -1415,6 +1415,24 @@ func (tc *TbtcChain) PastNewWalletRegisteredEvents( return convertedEvents, err } +func (tc *TbtcChain) CalculateWalletID( + walletPublicKey *ecdsa.PublicKey, +) ([32]byte, error) { + return calculateWalletID(walletPublicKey) +} + +func calculateWalletID(walletPublicKey *ecdsa.PublicKey) ([32]byte, error) { + walletPublicKeyBytes, err := convertPubKeyToChainFormat(walletPublicKey) + if err != nil { + return [32]byte{}, fmt.Errorf( + "error while converting wallet public key to chain format: [%v]", + err, + ) + } + + return crypto.Keccak256Hash(walletPublicKeyBytes[:]), nil +} + func (tc *TbtcChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) { isWalletRegistered, err := tc.walletRegistry.IsWalletRegistered( EcdsaWalletID, diff --git a/pkg/chain/ethereum/tbtc_test.go b/pkg/chain/ethereum/tbtc_test.go index 996e06a199..1c9eef1be0 100644 --- a/pkg/chain/ethereum/tbtc_test.go +++ b/pkg/chain/ethereum/tbtc_test.go @@ -16,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/keep-network/keep-core/internal/testutils" + "github.com/keep-network/keep-core/pkg/chain/local_v1" "github.com/keep-network/keep-core/pkg/protocol/group" ) @@ -279,6 +280,49 @@ func TestCalculateInactivityClaimHash(t *testing.T) { ) } +func TestCalculateWalletID(t *testing.T) { + hexToByte32 := func(hexStr string) [32]byte { + if len(hexStr) != 64 { + t.Fatal("hex string length incorrect") + } + + decoded, err := hex.DecodeString(hexStr) + if err != nil { + t.Fatal(err) + } + + var result [32]byte + copy(result[:], decoded) + + return result + } + + xBytes := hexToByte32( + "9a0544440cc47779235ccb76d669590c2cd20c7e431f97e17a1093faf03291c4", + ) + + yBytes := hexToByte32( + "73e661a208a8a565ca1e384059bd2ff7ff6886df081ff1229250099d388c83df", + ) + + walletPublicKey := &ecdsa.PublicKey{ + Curve: local_v1.DefaultCurve, + X: new(big.Int).SetBytes(xBytes[:]), + Y: new(big.Int).SetBytes(yBytes[:]), + } + + actualWalletID, err := calculateWalletID(walletPublicKey) + if err != nil { + t.Fatal(err) + } + + expectedWalletID := hexToByte32( + "a6602e554b8cf7c23538fd040e4ff3520ec680e5e5ce9a075259e613a3e5aa79", + ) + + testutils.AssertBytesEqual(t, expectedWalletID[:], actualWalletID[:]) +} + func TestParseDkgResultValidationOutcome(t *testing.T) { isValid, err := parseDkgResultValidationOutcome( &struct { diff --git a/pkg/tbtc/chain.go b/pkg/tbtc/chain.go index ad030b5bae..1e9c51ed40 100644 --- a/pkg/tbtc/chain.go +++ b/pkg/tbtc/chain.go @@ -245,6 +245,10 @@ type WalletClosedEvent struct { // BridgeChain defines the subset of the TBTC chain interface that pertains // specifically to the tBTC Bridge operations. type BridgeChain interface { + // CalculateWalletID calculates the wallet's ECDSA ID based on the provided + // wallet public key. + CalculateWalletID(walletPublicKey *ecdsa.PublicKey) ([32]byte, error) + // IsWalletRegistered checks whether the given wallet is registered in the // ECDSA wallet registry. IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) diff --git a/pkg/tbtc/chain_test.go b/pkg/tbtc/chain_test.go index 64effd2b3b..a2a6921b8a 100644 --- a/pkg/tbtc/chain_test.go +++ b/pkg/tbtc/chain_test.go @@ -853,6 +853,12 @@ func (lc *localChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) { panic("unsupported") } +func (lc *localChain) CalculateWalletID( + walletPublicKey *ecdsa.PublicKey, +) ([32]byte, error) { + panic("unsupported") +} + func (lc *localChain) GetWallet(walletPublicKeyHash [20]byte) ( *WalletChainData, error, diff --git a/pkg/tbtcpg/chain_test.go b/pkg/tbtcpg/chain_test.go index a87ba98f56..52f6ef4137 100644 --- a/pkg/tbtcpg/chain_test.go +++ b/pkg/tbtcpg/chain_test.go @@ -3,6 +3,7 @@ package tbtcpg import ( "bytes" "context" + "crypto/ecdsa" "crypto/sha256" "encoding/binary" "encoding/hex" @@ -1024,6 +1025,12 @@ func (lc *LocalChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) { panic("unsupported") } +func (lc *LocalChain) CalculateWalletID( + walletPublicKey *ecdsa.PublicKey, +) ([32]byte, error) { + panic("unsupported") +} + func (lc *LocalChain) GetWallet(walletPublicKeyHash [20]byte) ( *tbtc.WalletChainData, error,