diff --git a/clientapi/auth/login_publickey_ethereum.go b/clientapi/auth/login_publickey_ethereum.go index 90de33d2b..abf1d2032 100644 --- a/clientapi/auth/login_publickey_ethereum.go +++ b/clientapi/auth/login_publickey_ethereum.go @@ -129,7 +129,7 @@ func (pk LoginPublicKeyEthereum) ValidateLoginResponse() (bool, *jsonerror.Matri } // Error if the chainId is not supported by the server. - if !contains(pk.config.PublicKeyAuthentication.Ethereum.ChainIDs, message.GetChainID()) { + if pk.config.PublicKeyAuthentication.Ethereum.ChainID != message.GetChainID() { return false, jsonerror.Forbidden("chainId") } @@ -156,12 +156,3 @@ func (pk LoginPublicKeyEthereum) verifyMessageUserId(message *siwe.Message) bool // one derived from the signed message. return pk.UserId == strings.ToLower(expectedUserId) } - -func contains(list []int, element int) bool { - for _, i := range list { - if i == element { - return true - } - } - return false -} diff --git a/clientapi/auth/login_publickey_ethereum_test.go b/clientapi/auth/login_publickey_ethereum_test.go index 12fae2654..a8d3a710a 100644 --- a/clientapi/auth/login_publickey_ethereum_test.go +++ b/clientapi/auth/login_publickey_ethereum_test.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/internal/mapsutil" "github.com/matrix-org/dendrite/setup/config" - "github.com/matrix-org/dendrite/test" + testutil "github.com/matrix-org/dendrite/test" uapi "github.com/matrix-org/dendrite/userapi/api" "github.com/stretchr/testify/assert" ) @@ -34,20 +34,18 @@ type loginContext struct { userInteractive *UserInteractive } -func createLoginContext(t *testing.T) *loginContext { - chainIds := []int{4} - +func createLoginContext(_ *testing.T) *loginContext { cfg := &config.ClientAPI{ Matrix: &config.Global{ - ServerName: test.TestServerName, + ServerName: testutil.TestServerName, }, Derived: &config.Derived{}, PasswordAuthenticationDisabled: true, PublicKeyAuthentication: config.PublicKeyAuthentication{ Ethereum: config.EthereumAuthConfig{ - Enabled: true, - Version: 1, - ChainIDs: chainIds, + Enabled: true, + Version: 1, + ChainID: testutil.EthereumTestNetworkId, }, }, } @@ -154,9 +152,9 @@ func TestLoginPublicKeyEthereum(t *testing.T) { var userAPI fakePublicKeyUserApi ctx := context.Background() loginContext := createLoginContext(t) - wallet, _ := test.CreateTestAccount() - message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) - signature, _ := test.SignMessage(message.String(), wallet.PrivateKey) + wallet, _ := testutil.CreateTestAccount() + message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress) + signature, _ := testutil.SignMessage(message.String(), wallet.PrivateKey) sessionId := publicKeyTestSession( &ctx, loginContext.config, @@ -165,7 +163,7 @@ func TestLoginPublicKeyEthereum(t *testing.T) { ) // Escape \t and \n. Work around for marshalling and unmarshalling message. - msgStr := test.FromEip4361MessageToString(message) + msgStr := testutil.FromEip4361MessageToString(message) body := fmt.Sprintf(`{ "type": "m.login.publickey", "auth": { @@ -219,8 +217,8 @@ func TestLoginPublicKeyEthereumMissingSignature(t *testing.T) { var userAPI fakePublicKeyUserApi ctx := context.Background() loginContext := createLoginContext(t) - wallet, _ := test.CreateTestAccount() - message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) + wallet, _ := testutil.CreateTestAccount() + message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress) sessionId := publicKeyTestSession( &ctx, loginContext.config, @@ -229,7 +227,7 @@ func TestLoginPublicKeyEthereumMissingSignature(t *testing.T) { ) // Escape \t and \n. Work around for marshalling and unmarshalling message. - msgStr := test.FromEip4361MessageToString(message) + msgStr := testutil.FromEip4361MessageToString(message) body := fmt.Sprintf(`{ "type": "m.login.publickey", "auth": { @@ -280,7 +278,7 @@ func TestLoginPublicKeyEthereumEmptyMessage(t *testing.T) { var userAPI fakePublicKeyUserApi ctx := context.Background() loginContext := createLoginContext(t) - wallet, _ := test.CreateTestAccount() + wallet, _ := testutil.CreateTestAccount() sessionId := publicKeyTestSession( &ctx, loginContext.config, @@ -333,7 +331,7 @@ func TestLoginPublicKeyEthereumWrongUserId(t *testing.T) { var userAPI fakePublicKeyUserApi ctx := context.Background() loginContext := createLoginContext(t) - wallet, _ := test.CreateTestAccount() + wallet, _ := testutil.CreateTestAccount() sessionId := publicKeyTestSession( &ctx, loginContext.config, diff --git a/clientapi/auth/login_publickey_test.go b/clientapi/auth/login_publickey_test.go index 6b95c5553..513616486 100644 --- a/clientapi/auth/login_publickey_test.go +++ b/clientapi/auth/login_publickey_test.go @@ -22,6 +22,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/setup/config" + testutil "github.com/matrix-org/dendrite/test" "github.com/stretchr/testify/assert" ) @@ -72,7 +73,10 @@ func TestLoginPublicKeyNewSession(t *testing.T) { params, "[object]") ethParams := params.(config.EthereumAuthParams) - assert.NotEmptyf(ethParams.ChainIDs, "ChainIDs actual: empty, expected not empty") + assert.Equalf( + testutil.EthereumTestNetworkId, + ethParams.ChainID, + "ChainID actual: %d, expected %d", ethParams.ChainID, testutil.EthereumTestNetworkId) assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty") } diff --git a/clientapi/routing/register_publickey_test.go b/clientapi/routing/register_publickey_test.go index 22727604e..e1a225cbd 100644 --- a/clientapi/routing/register_publickey_test.go +++ b/clientapi/routing/register_publickey_test.go @@ -26,8 +26,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/internal/mapsutil" "github.com/matrix-org/dendrite/setup/config" - "github.com/matrix-org/dendrite/test" - "github.com/matrix-org/dendrite/userapi/api" + testutil "github.com/matrix-org/dendrite/test" uapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" "github.com/stretchr/testify/assert" @@ -40,20 +39,18 @@ type registerContext struct { userInteractive *auth.UserInteractive } -func createRegisterContext(t *testing.T) *registerContext { - chainIds := []int{4} - +func createRegisterContext(_ *testing.T) *registerContext { cfg := &config.ClientAPI{ Matrix: &config.Global{ - ServerName: test.TestServerName, + ServerName: testutil.TestServerName, }, Derived: &config.Derived{}, PasswordAuthenticationDisabled: true, PublicKeyAuthentication: config.PublicKeyAuthentication{ Ethereum: config.EthereumAuthConfig{ - Enabled: true, - Version: 1, - ChainIDs: chainIds, + Enabled: true, + Version: 1, + ChainID: testutil.EthereumTestNetworkId, }, }, } @@ -129,7 +126,7 @@ func (ua *fakePublicKeyUserApi) PerformDeviceCreation( req *uapi.PerformDeviceCreationRequest, res *uapi.PerformDeviceCreationResponse) error { res.DeviceCreated = true - res.Device = &api.Device{ + res.Device = &uapi.Device{ ID: "device_id", UserID: req.Localpart, AccessToken: req.AccessToken, @@ -142,11 +139,11 @@ func (ua *fakePublicKeyUserApi) PerformAccountCreation( req *uapi.PerformAccountCreationRequest, res *uapi.PerformAccountCreationResponse) error { res.AccountCreated = true - res.Account = &api.Account{ + res.Account = &uapi.Account{ AppServiceID: req.AppServiceID, Localpart: req.Localpart, - ServerName: test.TestServerName, - UserID: fmt.Sprintf("@%s:%s", req.Localpart, test.TestServerName), + ServerName: testutil.TestServerName, + UserID: fmt.Sprintf("@%s:%s", req.Localpart, testutil.TestServerName), AccountType: req.AccountType, } return nil @@ -173,8 +170,6 @@ func (*fakePublicKeyUserApi) QueryLoginToken(ctx context.Context, req *uapi.Quer func newRegistrationSession( t *testing.T, userId string, - cfg *config.ClientAPI, - userInteractive *auth.UserInteractive, userAPI *fakePublicKeyUserApi, ) string { body := fmt.Sprintf(`{ @@ -214,20 +209,18 @@ func newRegistrationSession( func TestRegisterEthereum(t *testing.T) { // Setup var userAPI fakePublicKeyUserApi - wallet, _ := test.CreateTestAccount() - message, _ := test.CreateEip4361TestMessage(wallet.PublicAddress) - signature, _ := test.SignMessage(message.String(), wallet.PrivateKey) + wallet, _ := testutil.CreateTestAccount() + message, _ := testutil.CreateEip4361TestMessage(wallet.PublicAddress) + signature, _ := testutil.SignMessage(message.String(), wallet.PrivateKey) registerContext := createRegisterContext(t) sessionId := newRegistrationSession( t, wallet.Eip155UserId, - registerContext.config, - registerContext.userInteractive, &userAPI, ) // Escape \t and \n. Work around for marshalling and unmarshalling message. - msgStr := test.FromEip4361MessageToString(message) + msgStr := testutil.FromEip4361MessageToString(message) body := fmt.Sprintf(`{ "username": "%v", "auth": { @@ -339,7 +332,10 @@ func TestNewRegistrationSession(t *testing.T) { params, "[object]") ethParams := params.(config.EthereumAuthParams) - assert.NotEmptyf(ethParams.ChainIDs, "ChainIDs actual: empty, expected not empty") + assert.Equalf( + testutil.EthereumTestNetworkId, + ethParams.ChainID, + "ChainID actual: %d, expected %d", ethParams.ChainID, testutil.EthereumTestNetworkId) assert.NotEmptyf(ethParams.Version, "Version actual: \"\", expected: not empty") } diff --git a/dendrite-sample.monolith.yaml b/dendrite-sample.monolith.yaml index b030b62eb..2bdee1853 100644 --- a/dendrite-sample.monolith.yaml +++ b/dendrite-sample.monolith.yaml @@ -178,7 +178,7 @@ client_api: ethereum: enabled: false version: 1 - chain_ids: [] + chain_id: 1337 # Whether to require reCAPTCHA for registration. If you have enabled registration # then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used diff --git a/dendrite-sample.polylith.yaml b/dendrite-sample.polylith.yaml index 5be1b6edd..c8db0b045 100644 --- a/dendrite-sample.polylith.yaml +++ b/dendrite-sample.polylith.yaml @@ -174,7 +174,7 @@ client_api: ethereum: enabled: false version: 1 - chain_ids: [] + chain_id: 1337 # Whether to require reCAPTCHA for registration. If you have enabled registration # then this is HIGHLY RECOMMENDED to reduce the risk of your homeserver being used diff --git a/setup/config/config_publickey.go b/setup/config/config_publickey.go index e214163e2..3fb1128ed 100644 --- a/setup/config/config_publickey.go +++ b/setup/config/config_publickey.go @@ -9,21 +9,18 @@ type AuthParams interface { } type EthereumAuthParams struct { - Version uint `json:"version"` - ChainIDs []int `json:"chain_ids"` + Version uint `json:"version"` + ChainID int `json:"chain_id"` } func (p EthereumAuthParams) GetParams() interface{} { - copyP := p - copyP.ChainIDs = make([]int, len(p.ChainIDs)) - copy(copyP.ChainIDs, p.ChainIDs) - return copyP + return p } type EthereumAuthConfig struct { - Enabled bool `yaml:"enabled"` - Version uint `yaml:"version"` - ChainIDs []int `yaml:"chain_ids"` + Enabled bool `yaml:"enabled"` + Version uint `yaml:"version"` + ChainID int `yaml:"chain_id"` } type PublicKeyAuthentication struct { @@ -47,8 +44,8 @@ func (pk *PublicKeyAuthentication) GetPublicKeyRegistrationParams() map[string]i params := make(map[string]interface{}) if pk.Ethereum.Enabled { p := EthereumAuthParams{ - Version: pk.Ethereum.Version, - ChainIDs: pk.Ethereum.ChainIDs, + Version: pk.Ethereum.Version, + ChainID: pk.Ethereum.ChainID, } params[authtypes.LoginTypePublicKeyEthereum] = p } diff --git a/test/publickey_utils.go b/test/publickey_utils.go index 6d3a67186..818121742 100644 --- a/test/publickey_utils.go +++ b/test/publickey_utils.go @@ -25,7 +25,7 @@ import ( "github.com/spruceid/siwe-go" ) -const EthereumTestNetworkId = 4 // Rinkeby test network ID +const EthereumTestNetworkId = 1337 // Localhost chain ID const TestServerName = "localhost" type EthereumTestWallet struct { @@ -66,7 +66,7 @@ func CreateEip4361TestMessage( publicAddress string, ) (*siwe.Message, error) { options := make(map[string]interface{}) - options["chainId"] = 4 // Rinkeby test network + options["chainId"] = EthereumTestNetworkId options["statement"] = "This is a test statement" message, err := siwe.InitMessage( TestServerName,