@@ -8,16 +8,20 @@ import (
88 "net/http"
99 "time"
1010
11+ "github.com/scroll-tech/go-ethereum/common"
12+ "github.com/scroll-tech/go-ethereum/crypto"
13+
1114 ctypes "scroll-tech/common/types"
1215 "scroll-tech/coordinator/internal/config"
1316 "scroll-tech/coordinator/internal/types"
1417)
1518
1619// Client wraps an http client with a preset host for coordinator API calls
1720type upClient struct {
18- httpClient * http.Client
19- baseURL string
20- loginToken string
21+ httpClient * http.Client
22+ baseURL string
23+ loginToken string
24+ compatibileMode bool
2125}
2226
2327// NewClient creates a new Client with the specified host
@@ -26,7 +30,8 @@ func newUpClient(cfg *config.UpStream) *upClient {
2630 httpClient : & http.Client {
2731 Timeout : time .Duration (cfg .ConnectionTimeoutSec ) * time .Second ,
2832 },
29- baseURL : cfg .BaseUrl ,
33+ baseURL : cfg .BaseUrl ,
34+ compatibileMode : cfg .CompatibileMode ,
3035 }
3136}
3237
@@ -40,8 +45,8 @@ type loginSchema struct {
4045 Token string `json:"token"`
4146}
4247
43- // FullLogin performs the complete login process: get challenge then login
44- func (c * upClient ) Login (ctx context.Context , genLogin func (string ) (* types.LoginParameter , error )) (* types. LoginSchema , error ) {
48+ // Login performs the complete login process: get challenge then login
49+ func (c * upClient ) Login (ctx context.Context , genLogin func (string ) (* types.LoginParameter , error )) (* ctypes. Response , error ) {
4550 // Step 1: Get challenge
4651 url := fmt .Sprintf ("%s/coordinator/v1/challenge" , c .baseURL )
4752
@@ -93,26 +98,7 @@ func (c *upClient) Login(ctx context.Context, genLogin func(string) (*types.Logi
9398 if err != nil {
9499 return nil , fmt .Errorf ("failed to perform login request: %w" , err )
95100 }
96-
97- parsedResp , err = handleHttpResp (loginResp )
98- if err != nil {
99- return nil , err
100- } else if parsedResp .ErrCode != 0 {
101- return nil , fmt .Errorf ("login failed: %d (%s)" , parsedResp .ErrCode , parsedResp .ErrMsg )
102- }
103-
104- var loginResult loginSchema
105- err = parsedResp .DecodeData (& loginResult )
106- if err != nil {
107- return nil , fmt .Errorf ("login parsing data fail: %v" , err )
108- }
109- c .loginToken = loginResult .Token
110-
111- // TODO: we need to parse time if we start making use of it
112-
113- return & types.LoginSchema {
114- Token : loginResult .Token ,
115- }, nil
101+ return handleHttpResp (loginResp )
116102}
117103
118104func handleHttpResp (resp * http.Response ) (* ctypes.Response , error ) {
@@ -130,8 +116,40 @@ func handleHttpResp(resp *http.Response) (*ctypes.Response, error) {
130116 return nil , fmt .Errorf ("login request failed with status: %d" , resp .StatusCode )
131117}
132118
119+ func (c * upClient ) proxyLoginCompatibleMode (ctx context.Context , param * types.LoginParameter ) (* ctypes.Response , error ) {
120+ mimePrivK , err := buildPrivateKey ([]byte (param .PublicKey ))
121+ if err != nil {
122+ return nil , err
123+ }
124+ mimePkHex := common .Bytes2Hex (crypto .CompressPubkey (& mimePrivK .PublicKey ))
125+
126+ genLoginParam := func (challenge string ) (* types.LoginParameter , error ) {
127+
128+ // Create login parameter with proxy settings
129+ loginParam := & types.LoginParameter {
130+ Message : param .Message ,
131+ PublicKey : mimePkHex ,
132+ }
133+ loginParam .Message .Challenge = challenge
134+
135+ // Sign the message with the private key
136+ if err := loginParam .SignWithKey (mimePrivK ); err != nil {
137+ return nil , fmt .Errorf ("failed to sign login parameter: %w" , err )
138+ }
139+
140+ return loginParam , nil
141+ }
142+
143+ return c .Login (ctx , genLoginParam )
144+ }
145+
133146// ProxyLogin makes a POST request to /v1/proxy_login with LoginParameter
134147func (c * upClient ) ProxyLogin (ctx context.Context , param * types.LoginParameter ) (* ctypes.Response , error ) {
148+
149+ if c .compatibileMode {
150+ return c .proxyLoginCompatibleMode (ctx , param )
151+ }
152+
135153 url := fmt .Sprintf ("%s/coordinator/v1/proxy_login" , c .baseURL )
136154
137155 jsonData , err := json .Marshal (param )
0 commit comments