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

feat: enable multiple frost keyshares #355

Open
wants to merge 4 commits into
base: adding-frost-peers
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func Run() error {
eventHandlers = append(eventHandlers, hubEventHandlers.NewDepositEventHandler(depositListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, msgChan))
eventHandlers = append(eventHandlers, hubEventHandlers.NewKeygenEventHandler(l, tssListener, coordinator, host, communication, keyshareStore, bridgeAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewFrostKeygenEventHandler(l, tssListener, coordinator, host, communication, frostKeyshareStore, frostAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, frostKeyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, topologyProvider, topologyStore, tssListener, coordinator, host, communication, connectionGate, keyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRetryEventHandler(l, tssListener, depositHandler, propStore, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations, msgChan))
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
executor := executor.NewExecutor(host, communication, coordinator, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())
Expand Down
3 changes: 3 additions & 0 deletions chains/btc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RawResource struct {
FeeAmount string
Tweak string
Script string
PublicKey []byte
}

type Resource struct {
Expand All @@ -30,6 +31,7 @@ type Resource struct {
ResourceID [32]byte
Tweak string
Script []byte
PublicKey string
}

type RawBtcConfig struct {
Expand Down Expand Up @@ -136,6 +138,7 @@ func NewBtcConfig(chainConfig map[string]interface{}) (*BtcConfig, error) {
Script: scriptBytes,
Tweak: r.Tweak,
FeeAmount: feeAmount,
PublicKey: hex.EncodeToString(r.PublicKey),
}
}

Expand Down
19 changes: 19 additions & 0 deletions chains/btc/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
feeAddress, _ := btcutil.DecodeAddress("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt", &chaincfg.TestNet3Params)
expectedScript, _ := hex.DecodeString("51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5")

expectedPublicKey := "36c62696a3869cb46b2e6c93be73d039e6ab341853d824efadecd3dcee332c1a"
publicKeyBytes, _ := hex.DecodeString(expectedPublicKey)
rawConfig := map[string]interface{}{
"id": 1,
"endpoint": "ws://domain.com",
Expand All @@ -112,6 +114,14 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
Script: "51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5",
Tweak: "tweak",
},
config.RawResource{
Address: "tb1qln69zuhdunc9stwfh6t7adexxrcr04ppy6thgm",
FeeAmount: "10000000",
ResourceID: "0x0000000000000000000000000000000000000000000000000000000000000300",
Script: "51206a698882348433b57d549d6344f74500fcd13ad8d2200cdf89f8e39e5cafa7d5",
Tweak: "tweak",
PublicKey: publicKeyBytes,
},
},
}

Expand Down Expand Up @@ -141,6 +151,15 @@ func (s *NewBtcConfigTestSuite) Test_ValidConfig() {
Script: expectedScript,
Tweak: "tweak",
FeeAmount: big.NewInt(10000000),
PublicKey: "",
},
{
Address: expectedAddress,
ResourceID: expectedResource,
Script: expectedScript,
Tweak: "tweak",
FeeAmount: big.NewInt(10000000),
PublicKey: expectedPublicKey,
},
},
})
Expand Down
1 change: 1 addition & 0 deletions chains/btc/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (e *Executor) executeResourceProps(props []*BtcTransferProposal, resource c
}
signing, err := signing.NewSigning(
i,
resource.PublicKey,
signingHash,
resource.Tweak,
messageID,
Expand Down
14 changes: 6 additions & 8 deletions chains/btc/listener/deposit-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ func TestRunErc20HandlerTestSuite(t *testing.T) {

func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
deposit := &listener.Deposit{
SenderAddress: "senderAddress",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_1",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_1",
}

sourceID := uint8(1)
Expand Down Expand Up @@ -70,10 +69,9 @@ func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent() {
func (s *Erc20HandlerTestSuite) Test_Erc20HandleEvent_InvalidDestinationDomainID() {

deposit := &listener.Deposit{
SenderAddress: "senderAddress",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_InvalidDestinationDomainID",
ResourceID: [32]byte{0},
Amount: big.NewInt(100),
Data: "0x1c3A03D04c026b1f4B4208D2ce053c5686E6FB8d_InvalidDestinationDomainID",
}

sourceID := uint8(1)
Expand Down
12 changes: 0 additions & 12 deletions chains/evm/listener/eventHandlers/event-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ChainSafe/sygma-relayer/tss/ecdsa/keygen"
"github.com/ChainSafe/sygma-relayer/tss/ecdsa/resharing"
frostKeygen "github.com/ChainSafe/sygma-relayer/tss/frost/keygen"
frostResharing "github.com/ChainSafe/sygma-relayer/tss/frost/resharing"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/libp2p/go-libp2p/core/host"
Expand Down Expand Up @@ -311,7 +310,6 @@ type RefreshEventHandler struct {
communication comm.Communication
connectionGate *p2p.ConnectionGate
ecdsaStorer resharing.SaveDataStorer
frostStorer frostResharing.FrostKeyshareStorer
}

func NewRefreshEventHandler(
Expand All @@ -324,7 +322,6 @@ func NewRefreshEventHandler(
communication comm.Communication,
connectionGate *p2p.ConnectionGate,
ecdsaStorer resharing.SaveDataStorer,
frostStorer frostResharing.FrostKeyshareStorer,
bridgeAddress common.Address,
) *RefreshEventHandler {
return &RefreshEventHandler{
Expand All @@ -336,7 +333,6 @@ func NewRefreshEventHandler(
host: host,
communication: communication,
ecdsaStorer: ecdsaStorer,
frostStorer: frostStorer,
connectionGate: connectionGate,
bridgeAddress: bridgeAddress,
}
Expand Down Expand Up @@ -389,14 +385,6 @@ func (eh *RefreshEventHandler) HandleEvents(
log.Err(err).Msgf("Failed executing ecdsa key refresh")
return nil
}
frostResharing := frostResharing.NewResharing(
eh.sessionID(startBlock), topology.Threshold, eh.host, eh.communication, eh.frostStorer,
)
err = eh.coordinator.Execute(context.Background(), []tss.TssProcess{frostResharing}, make(chan interface{}, 1))
if err != nil {
log.Err(err).Msgf("Failed executing frost key refresh")
return nil
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion example/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Run() error {
eventHandlers = append(eventHandlers, hubEventHandlers.NewDepositEventHandler(depositListener, depositHandler, bridgeAddress, *config.GeneralChainConfig.Id, msgChan))
eventHandlers = append(eventHandlers, hubEventHandlers.NewKeygenEventHandler(l, tssListener, coordinator, host, communication, keyshareStore, bridgeAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewFrostKeygenEventHandler(l, tssListener, coordinator, host, communication, frostKeyshareStore, frostAddress, networkTopology.Threshold))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, nil, nil, tssListener, coordinator, host, communication, connectionGate, keyshareStore, frostKeyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRefreshEventHandler(l, nil, nil, tssListener, coordinator, host, communication, connectionGate, keyshareStore, bridgeAddress))
eventHandlers = append(eventHandlers, hubEventHandlers.NewRetryEventHandler(l, tssListener, depositHandler, propStore, bridgeAddress, *config.GeneralChainConfig.Id, config.BlockConfirmations, msgChan))
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, sygmaMetrics, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
executor := executor.NewExecutor(host, communication, coordinator, bridgeContract, keyshareStore, exitLock, config.GasLimit.Uint64())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/binance-chain/tss-lib v0.0.0-00010101000000-000000000000
github.com/btcsuite/btcd/btcutil v1.1.5
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1
github.com/creasty/defaults v1.6.0
github.com/deckarep/golang-set/v2 v2.1.0
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 h1:R8vQdOQdZ9Y3
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
15 changes: 11 additions & 4 deletions keyshare/frost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package keyshare

import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (ks *FrostKeyshareStore) UnlockKeyshare() {
// StoreFrostKeyshare stores frost keyshare generated by keygen or reshare into file and truncates
// old keyshare.
func (ks *FrostKeyshareStore) StoreKeyshare(keyshare FrostKeyshare) error {
f, err := os.OpenFile(ks.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
f, err := os.OpenFile(fmt.Sprintf("%s.%s", ks.path, hex.EncodeToString(keyshare.Key.PublicKey)), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,13 +112,19 @@ func (ks *FrostKeyshareStore) StoreKeyshare(keyshare FrostKeyshare) error {
return err
}

// GetFrostKeyshare fetches current keyshare from file.
// GetFrostKeyshare fetches keyshare from file by public key.
// Can be a blocking call if keygen or resharing are pending.
func (ks *FrostKeyshareStore) GetKeyshare() (FrostKeyshare, error) {
func (ks *FrostKeyshareStore) GetKeyshare(publicKey string) (FrostKeyshare, error) {
fStore := frostKeyshareStore{}
k := FrostKeyshare{}

kb, err := os.ReadFile(ks.path)
// used for backwards compatibility with keys before public key was stored
path := ks.path
if len(publicKey) > 0 {
path = fmt.Sprintf("%s.%s", ks.path, publicKey)
}

kb, err := os.ReadFile(path)
if err != nil {
return k, fmt.Errorf("error on reading keyshare file: %s", err)
}
Expand Down
10 changes: 6 additions & 4 deletions keyshare/frost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package keyshare_test

import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -32,11 +34,10 @@ func (s *FrostKeyshareStoreTestSuite) SetupTest() {
s.keyshareStore = keyshare.NewFrostKeyshareStore(s.path)
}
func (s *FrostKeyshareStoreTestSuite) TearDownTest() {
os.Remove(s.path)
}

func (s *FrostKeyshareStoreTestSuite) Test_RetrieveInvalidFile() {
_, err := s.keyshareStore.GetKeyshare()
_, err := s.keyshareStore.GetKeyshare("")
s.NotNil(err)
}

Expand All @@ -61,15 +62,16 @@ func (s *FrostKeyshareStoreTestSuite) Test_StoreAndRetrieveShare() {
Threshold: 1,
PrivateShare: privateShare,
VerificationShares: verificationShares,
PublicKey: taproot.PublicKey{},
PublicKey: taproot.PublicKey{1},
ChainKey: []byte{},
}, threshold, peers)

err := s.keyshareStore.StoreKeyshare(keyshare)
s.Nil(err)

storedKeyshare, err := s.keyshareStore.GetKeyshare()
storedKeyshare, err := s.keyshareStore.GetKeyshare(hex.EncodeToString(keyshare.Key.PublicKey))
s.Nil(err)

s.Equal(keyshare, storedKeyshare)
os.Remove(fmt.Sprintf("%s.%s", s.path, hex.EncodeToString(keyshare.Key.PublicKey)))
}
2 changes: 1 addition & 1 deletion tss/frost/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type FrostKeyshareStorer interface {
StoreKeyshare(keyshare keyshare.FrostKeyshare) error
LockKeyshare()
UnlockKeyshare()
GetKeyshare() (keyshare.FrostKeyshare, error)
GetKeyshare(publicKey string) (keyshare.FrostKeyshare, error)
}

type Keygen struct {
Expand Down
5 changes: 3 additions & 2 deletions tss/frost/resharing/resharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type startParams struct {
VerificationShares map[party.ID]*curve.Secp256k1Point
}
type FrostKeyshareStorer interface {
GetKeyshare() (keyshare.FrostKeyshare, error)
GetKeyshare(publicKey string) (keyshare.FrostKeyshare, error)
StoreKeyshare(keyshare keyshare.FrostKeyshare) error
LockKeyshare()
UnlockKeyshare()
Expand All @@ -43,14 +43,15 @@ type Resharing struct {

func NewResharing(
sessionID string,
publicKey string,
threshold int,
host host.Host,
comm comm.Communication,
storer FrostKeyshareStorer,
) *Resharing {
storer.LockKeyshare()
var key keyshare.FrostKeyshare
key, err := storer.GetKeyshare()
key, err := storer.GetKeyshare(publicKey)
if err != nil {
// empty key for parties that don't have one
key = keyshare.FrostKeyshare{
Expand Down
14 changes: 7 additions & 7 deletions tss/frost/resharing/resharing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/ChainSafe/sygma-relayer/tss"
"github.com/ChainSafe/sygma-relayer/tss/frost/resharing"
tsstest "github.com/ChainSafe/sygma-relayer/tss/test"
"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/sourcegraph/conc/pool"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

type ResharingTestSuite struct {
Expand Down Expand Up @@ -53,12 +53,12 @@ func (s *ResharingTestSuite) Test_ValidResharingProcess_OldAndNewSubset() {
}
communicationMap[host.ID()] = &communication
storer := keyshare.NewFrostKeyshareStore(fmt.Sprintf("../../test/keyshares/%d-frost.keyshare", i))
share, err := storer.GetKeyshare()
share, err := storer.GetKeyshare("")
s.MockFrostStorer.EXPECT().LockKeyshare()
s.MockFrostStorer.EXPECT().UnlockKeyshare()
s.MockFrostStorer.EXPECT().GetKeyshare().Return(share, err)
s.MockFrostStorer.EXPECT().GetKeyshare("").Return(share, err)
s.MockFrostStorer.EXPECT().StoreKeyshare(gomock.Any()).Return(nil)
resharing := resharing.NewResharing("resharing2", 1, host, &communication, s.MockFrostStorer)
resharing := resharing.NewResharing("resharing2", "", 1, host, &communication, s.MockFrostStorer)
electorFactory := elector.NewCoordinatorElectorFactory(host, s.BullyConfig)
coordinators = append(coordinators, tss.NewCoordinator(host, &communication, electorFactory))
processes = append(processes, resharing)
Expand Down Expand Up @@ -100,12 +100,12 @@ func (s *ResharingTestSuite) Test_ValidResharingProcess_RemovePeer() {
}
communicationMap[host.ID()] = &communication
storer := keyshare.NewFrostKeyshareStore(fmt.Sprintf("../../test/keyshares/%d-frost.keyshare", i))
share, err := storer.GetKeyshare()
share, err := storer.GetKeyshare("")
s.MockFrostStorer.EXPECT().LockKeyshare()
s.MockFrostStorer.EXPECT().UnlockKeyshare()
s.MockFrostStorer.EXPECT().GetKeyshare().Return(share, err)
s.MockFrostStorer.EXPECT().GetKeyshare("").Return(share, err)
s.MockFrostStorer.EXPECT().StoreKeyshare(gomock.Any()).Return(nil)
resharing := resharing.NewResharing("resharing2", 1, host, &communication, s.MockFrostStorer)
resharing := resharing.NewResharing("resharing2", "", 1, host, &communication, s.MockFrostStorer)
electorFactory := elector.NewCoordinatorElectorFactory(host, s.BullyConfig)
coordinators = append(coordinators, tss.NewCoordinator(host, &communication, electorFactory))
processes = append(processes, resharing)
Expand Down
5 changes: 3 additions & 2 deletions tss/frost/signing/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Signature struct {
}

type SaveDataFetcher interface {
GetKeyshare() (keyshare.FrostKeyshare, error)
GetKeyshare(publicKey string) (keyshare.FrostKeyshare, error)
LockKeyshare()
UnlockKeyshare()
}
Expand All @@ -49,6 +49,7 @@ type Signing struct {

func NewSigning(
id int,
publicKey string,
msg []byte,
tweak string,
messageID string,
Expand All @@ -59,7 +60,7 @@ func NewSigning(
) (*Signing, error) {
fetcher.LockKeyshare()
defer fetcher.UnlockKeyshare()
key, err := fetcher.GetKeyshare()
key, err := fetcher.GetKeyshare(publicKey)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading