diff --git a/zetaclient/tss/config.go b/zetaclient/tss/config.go index fcf89ce7cf..06a24e87ed 100644 --- a/zetaclient/tss/config.go +++ b/zetaclient/tss/config.go @@ -11,6 +11,8 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog" tsscommon "gitlab.com/thorchain/tss/go-tss/common" + + "github.com/zeta-chain/node/cmd" ) const ( @@ -20,6 +22,10 @@ const ( Algo = tsscommon.ECDSA ) +func init() { + cmd.SetupCosmosConfig() +} + // MultiAddressFromString parses a string into a slice of addresses (for convenience). func MultiAddressFromString(peer string) ([]multiaddr.Multiaddr, error) { if peer == "" { diff --git a/zetaclient/tss/config_test.go b/zetaclient/tss/config_test.go index e21ab2b2c3..1c220a70f6 100644 --- a/zetaclient/tss/config_test.go +++ b/zetaclient/tss/config_test.go @@ -1,8 +1,10 @@ package tss import ( + _ "embed" "fmt" "os" + "path/filepath" "testing" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -40,6 +42,58 @@ func Test_ParsePubKeysFromPath(t *testing.T) { } } +func Test_ResolvePreParamsFromPath(t *testing.T) { + t.Run("file not found", func(t *testing.T) { + // ARRANGE + path := filepath.Join(os.TempDir(), "hello-123.json") + + // ACT + _, err := ResolvePreParamsFromPath(path) + + // ASSERT + require.Error(t, err) + require.Contains(t, err.Error(), "unable to read pre-params") + }) + + t.Run("invalid file", func(t *testing.T) { + // ARRANGE + tmpFile, err := os.CreateTemp(os.TempDir(), "pre-params-*.json") + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, os.Remove(tmpFile.Name())) + }) + + _, err = tmpFile.WriteString(`invalid-json`) + require.NoError(t, err) + tmpFile.Close() + + // ACT + _, err = ResolvePreParamsFromPath(tmpFile.Name()) + + // ASSERT + require.Error(t, err) + require.Contains(t, err.Error(), "unable to decode pre-params") + }) + + t.Run("AllGood", func(t *testing.T) { + // ARRANGE + tmpFile, err := os.CreateTemp(os.TempDir(), "pre-params-*.json") + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, os.Remove(tmpFile.Name())) + }) + + createPreParams(t, tmpFile.Name()) + + // ACT + resolvedPreParams, err := ResolvePreParamsFromPath(tmpFile.Name()) + + // Assert + require.NoError(t, err) + require.NotNil(t, resolvedPreParams) + }) +} + func generateKeyShareFiles(t *testing.T, n int, dir string) { err := os.Chdir(dir) require.NoError(t, err) @@ -61,3 +115,17 @@ func generateKeyShareFiles(t *testing.T, n int, dir string) { require.NoError(t, err) } } + +//go:embed testdata/pre-params.json +var preParamsFixture []byte + +// createPreParams creates a pre-params file at the given path. +// uses fixture to skip long setup. +func createPreParams(t *testing.T, filePath string) { + file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0600) + require.NoError(t, err) + + _, err = file.Write(preParamsFixture) + require.NoError(t, err) + require.NoError(t, file.Close()) +} diff --git a/zetaclient/tss/crypto_test.go b/zetaclient/tss/crypto_test.go index 2b14a01e58..b34e0dc21e 100644 --- a/zetaclient/tss/crypto_test.go +++ b/zetaclient/tss/crypto_test.go @@ -1,24 +1,37 @@ package tss import ( + "encoding/hex" "strings" "testing" + "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/zeta-chain/node/cmd" "github.com/zeta-chain/node/pkg/chains" ) func TestPubKey(t *testing.T) { - cmd.SetupCosmosConfig() - t.Run("Invalid", func(t *testing.T) { - _, err := NewPubKeyFromBech32("") - require.ErrorContains(t, err, "empty bech32 address") + cases := []struct { + name string + input string + errMsg string + }{ + {"empty string", "", "empty bech32 address"}, + {"invalid prefix", "invalid1addwnpepq...", "unable to GetPubKeyFromBech32"}, + {"malformed bech32", "zetapub1invalid", "decoding bech32 failed"}, + } + + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + _, err := NewPubKeyFromBech32(tt.input) + require.ErrorContains(t, err, tt.errMsg) + }) + } }) - t.Run("Valid", func(t *testing.T) { + t.Run("Valid NewPubKeyFromBech32", func(t *testing.T) { // ARRANGE const sample = `zetapub1addwnpepqtadxdyt037h86z60nl98t6zk56mw5zpnm79tsmvspln3hgt5phdc79kvfc` @@ -42,4 +55,30 @@ func TestPubKey(t *testing.T) { require.NoError(t, err) require.Equal(t, pk.Bech32String(), pk2.Bech32String()) }) + + t.Run("Valid NewPubKeyFromECDSAHexString", func(t *testing.T) { + // ARRANGE + pk, err := crypto.GenerateKey() + require.NoError(t, err) + + pubKeyHex := hex.EncodeToString(crypto.FromECDSAPub(&pk.PublicKey)) + evmAddr := crypto.PubkeyToAddress(pk.PublicKey) + + // ACT + actual, err := NewPubKeyFromECDSAHexString(pubKeyHex) + + // ASSERT + require.NoError(t, err) + assert.Equal(t, evmAddr, actual.AddressEVM()) + assert.True(t, strings.HasPrefix(actual.Bech32String(), "zetapub")) + + t.Run("With 0x prefix", func(t *testing.T) { + // ACT + actual2, err := NewPubKeyFromECDSAHexString("0x" + pubKeyHex) + + // ASSERT + require.NoError(t, err) + assert.Equal(t, actual.Bech32String(), actual2.Bech32String()) + }) + }) } diff --git a/zetaclient/tss/service_test.go b/zetaclient/tss/service_test.go index e3770c88ee..e0ccde6954 100644 --- a/zetaclient/tss/service_test.go +++ b/zetaclient/tss/service_test.go @@ -15,7 +15,6 @@ import ( "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/zeta-chain/node/cmd" "github.com/zeta-chain/node/pkg/cosmos" "github.com/zeta-chain/node/zetaclient/testutils/mocks" "github.com/zeta-chain/node/zetaclient/tss" @@ -30,8 +29,6 @@ var ( ) func TestService(t *testing.T) { - cmd.SetupCosmosConfig() - t.Run("NewService", func(t *testing.T) { t.Run("Invalid pub key", func(t *testing.T) { s, err := tss.NewService(nil, "hello", nil, zerolog.Nop()) diff --git a/zetaclient/tss/testdata/pre-params.json b/zetaclient/tss/testdata/pre-params.json new file mode 100644 index 0000000000..7de86020b9 --- /dev/null +++ b/zetaclient/tss/testdata/pre-params.json @@ -0,0 +1,14 @@ +{ + "PaillierSK": { + "N": 19999163952130747960789760351783788437756051987951355569970036401244501934315272438705577988944467922102492970155408252851533852794097112218266886553586076347804115557757017279985449126837870194546917950194158856088817869376589018796114667864086028598566847973861695589083004303196605694312963960967331453606698750471421102641235873021677307589731091486775521460999458082171513731685071918681293270385071153104334613903503586831444874346531652054016464538970392294909014876824634633871501554184148693655829401315639013286485392467863637678737554558389111485917683916225530045382764297294039119124600051351150814211249, + "LambdaN": 9999581976065373980394880175891894218878025993975677784985018200622250967157636219352788994472233961051246485077704126425766926397048556109133443276793038173902057778878508639992724563418935097273458975097079428044408934688294509398057333932043014299283423986930847794541502151598302847156481980483665726803207844439805564908826908299383131049732286281017413122919529611290634931314211468373848862938241694978602167585393762136625122435580962508878486729843009777923588471302593033407043805894076580681612304239013813560948585228383852537633607722118782082256871296347729191161115127804109535019576521175406883452282, + "PhiN": 19999163952130747960789760351783788437756051987951355569970036401244501934315272438705577988944467922102492970155408252851533852794097112218266886553586076347804115557757017279985449126837870194546917950194158856088817869376589018796114667864086028598566847973861695589083004303196605694312963960967331453606415688879611129817653816598766262099464572562034826245839059222581269862628422936747697725876483389957204335170787524273250244871161925017756973459686019555847176942605186066814087611788153161363224608478027627121897170456767705075267215444237564164513742592695458382322230255608219070039153042350813766904564 + }, + "NTildei": 27751913785731317561401314588152253217728532341018033146570952349857948266439815804220817588815467446516307716665513549241775208783937988610417256936545867146994706743041371293345131680171448151086694170309671283486292778243426639412725044938992517873701725006330137070569134489394538234586796972674797327961558527707455425675762370744764931280145514019295136133284528084915871623701598565821330577968131284073671734400158797160800982150954634593880871530904229027402996176777363730066986245815341196905861357768072141609870245229405943812356754513322852513347434681734563805660510650984453048567481135942146732097813, + "H1i": 27561956258855841139395484606566450906572976054374340673395795572404700162978995778059515299485228139352425772962548084873215102616280128304708426026182349406021553685331715999823302504724574932475811252902805356309567749243825500782237349965272997278195732138043060594209278009387869413139123717258217760369558773026393837335151469655513584729163915760124835545159150578571574024242102852384898501031964398164508802427280019310461284676702762930983763592670681066746406519089087470866348555388978893174745329039290345402036056193885195412683078301439714912339014313158992229100758385774991450029591789628252080327559, + "H2i": 3728689158497086873141839685259803753136983319082693856963169726229733672131113136177490814524417255268612620467671366860707614533735757821359190975354246119660178366078082228715114521844446138293061922109534401626012196214235577165710689292018952534202208597250579673907469566293203565367500274532873330046549157395771416744602137701132458921345439713375591947533341059776041205066197520027367159385233699483111938755605800865028185017605864002287172205806900860500164647247828301974000635262077178937664668984154402040254899639546189675216600248280397892382489630326684820331235755629614367050680421011963919488203, + "Alpha": 12023422531214934687129519427336751009035984170561091401995316889932876861127740921311384022403289655727852687123651207179666808127441651093217171624401095992093222930319273470697036100721360499771918420525219595960686726656705243231703751035078246024613303037202379961560592763983837502239948740870420991159486603826605562036000339449263579950064527369636354957917745420111193837730149039024242588057472210146671375468285242828724250764829561800066210708917523476384078739610379308948169897901580561000198663019628193284829226489567395610248716149143318042255879731755585576865932443849122426445714440683236201534278, + "Beta": 5216977155731439778993461291447860640876449566364806008925260703987381460229691893122780873808995223223163744678138135687392695130955126431701144250850716493320323144762645954407840190267536639011747593827794567732053411766358287215370001772078774800656600220349293422391251849464836894526916992757090542649120892603149475768335549307299570724464502418555347605473575788163359985939167794314377636554352661162839551390523751777303683251484579852979700993668013952132183651009302732527581967714766865349243790338837470546064122938436752503268419712265039843732584942630907069295257952472051299690267622482814099402925, + "P": 88381187817350292364427405327147330703367287364982016684112982632296448146824918798880619726847439553397534386831375002209019191421648634648182506013318106307619159619988111117766095040541079223450318951393068166232842380382197780515682551401156068869281037660390394768227141311012971150741063274439667450459, + "Q": 78500624598652662628885184600358941803683076507070394222241292020467077683298789581328374906370256776628646773897006681776229150698724417192594456702517705509422164083826765704615434585654776671817770799114758241307176860136745886869834508666912875775642490370066102181451552981996440342363450244264823933513 +} \ No newline at end of file