Skip to content

Commit

Permalink
Exposed function for calculating wallet ID
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jun 10, 2024
1 parent 7407212 commit 810740a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions pkg/chain/ethereum/tbtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions pkg/tbtcpg/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tbtcpg
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 810740a

Please sign in to comment.