@@ -2,29 +2,28 @@ package proxy
22
33import (
44 "bytes"
5- "crypto/ecdsa "
5+ "context "
66 "encoding/json"
77 "fmt"
88 "net/http"
99 "time"
1010
11- "github.com/scroll-tech/go-ethereum/common"
12- "github.com/scroll-tech/go-ethereum/crypto"
11+ "github.com/gin-gonic/gin"
1312
1413 "scroll-tech/coordinator/internal/config"
1514 "scroll-tech/coordinator/internal/types"
1615)
1716
1817// Client wraps an http client with a preset host for coordinator API calls
19- type Client struct {
18+ type upClient struct {
2019 httpClient * http.Client
2120 baseURL string
2221 loginToken string
2322}
2423
2524// NewClient creates a new Client with the specified host
26- func NewClient (cfg * config.UpStream ) * Client {
27- return & Client {
25+ func newUpClient (cfg * config.UpStream ) * upClient {
26+ return & upClient {
2827 httpClient : & http.Client {
2928 Timeout : time .Duration (cfg .ConnectionTimeoutSec ) * time .Second ,
3029 },
@@ -33,7 +32,7 @@ func NewClient(cfg *config.UpStream) *Client {
3332}
3433
3534// FullLogin performs the complete login process: get challenge then login
36- func (c * Client ) Login (param types.LoginParameter ) (* types.LoginSchema , error ) {
35+ func (c * upClient ) Login (ctx context. Context , param types.LoginParameter ) (* types.LoginSchema , error ) {
3736 // Step 1: Get challenge
3837 url := fmt .Sprintf ("%s/coordinator/v1/challenge" , c .baseURL )
3938
@@ -93,15 +92,15 @@ func (c *Client) Login(param types.LoginParameter) (*types.LoginSchema, error) {
9392}
9493
9594// ProxyLogin makes a POST request to /v1/proxy_login with LoginParameter
96- func (c * Client ) ProxyLogin (param types.LoginParameter ) (* http.Response , error ) {
95+ func (c * upClient ) ProxyLogin (ctx * gin. Context , param types.LoginParameter ) (* http.Response , error ) {
9796 url := fmt .Sprintf ("%s/coordinator/v1/proxy_login" , c .baseURL )
9897
9998 jsonData , err := json .Marshal (param )
10099 if err != nil {
101100 return nil , fmt .Errorf ("failed to marshal proxy login parameter: %w" , err )
102101 }
103102
104- req , err := http .NewRequest ( "POST" , url , bytes .NewBuffer (jsonData ))
103+ req , err := http .NewRequestWithContext ( ctx , "POST" , url , bytes .NewBuffer (jsonData ))
105104 if err != nil {
106105 return nil , fmt .Errorf ("failed to create proxy login request: %w" , err )
107106 }
@@ -113,15 +112,15 @@ func (c *Client) ProxyLogin(param types.LoginParameter) (*http.Response, error)
113112}
114113
115114// GetTask makes a POST request to /v1/get_task with GetTaskParameter
116- func (c * Client ) GetTask (param types.GetTaskParameter , token string ) (* http.Response , error ) {
115+ func (c * upClient ) GetTask (ctx * gin. Context , param types.GetTaskParameter , token string ) (* http.Response , error ) {
117116 url := fmt .Sprintf ("%s/coordinator/v1/get_task" , c .baseURL )
118117
119118 jsonData , err := json .Marshal (param )
120119 if err != nil {
121120 return nil , fmt .Errorf ("failed to marshal get task parameter: %w" , err )
122121 }
123122
124- req , err := http .NewRequest ( "POST" , url , bytes .NewBuffer (jsonData ))
123+ req , err := http .NewRequestWithContext ( ctx , "POST" , url , bytes .NewBuffer (jsonData ))
125124 if err != nil {
126125 return nil , fmt .Errorf ("failed to create get task request: %w" , err )
127126 }
@@ -135,15 +134,15 @@ func (c *Client) GetTask(param types.GetTaskParameter, token string) (*http.Resp
135134}
136135
137136// SubmitProof makes a POST request to /v1/submit_proof with SubmitProofParameter
138- func (c * Client ) SubmitProof (param types.SubmitProofParameter , token string ) (* http.Response , error ) {
137+ func (c * upClient ) SubmitProof (ctx * gin. Context , param types.SubmitProofParameter , token string ) (* http.Response , error ) {
139138 url := fmt .Sprintf ("%s/coordinator/v1/submit_proof" , c .baseURL )
140139
141140 jsonData , err := json .Marshal (param )
142141 if err != nil {
143142 return nil , fmt .Errorf ("failed to marshal submit proof parameter: %w" , err )
144143 }
145144
146- req , err := http .NewRequest ( "POST" , url , bytes .NewBuffer (jsonData ))
145+ req , err := http .NewRequestWithContext ( ctx , "POST" , url , bytes .NewBuffer (jsonData ))
147146 if err != nil {
148147 return nil , fmt .Errorf ("failed to create submit proof request: %w" , err )
149148 }
@@ -155,53 +154,3 @@ func (c *Client) SubmitProof(param types.SubmitProofParameter, token string) (*h
155154
156155 return c .httpClient .Do (req )
157156}
158-
159- // transformToValidPrivateKey safely transforms arbitrary bytes into valid private key bytes
160- func (c * Client ) buildPrivateKey (inputBytes []byte ) (* ecdsa.PrivateKey , error ) {
161- // Try appending bytes from 0x0 to 0x20 until we get a valid private key
162- for appendByte := byte (0x0 ); appendByte <= 0x20 ; appendByte ++ {
163- // Append the byte to input
164- extendedBytes := append (inputBytes , appendByte )
165-
166- // Calculate 256-bit hash
167- hash := crypto .Keccak256 (extendedBytes )
168-
169- // Try to create private key from hash
170- if k , err := crypto .ToECDSA (hash ); err == nil {
171- return k , nil
172- }
173- }
174-
175- return nil , fmt .Errorf ("failed to generate valid private key from input bytes" )
176- }
177-
178- func (c * Client ) generateLoginParameter (privateKeyBytes []byte , challenge string ) (* types.LoginParameter , error ) {
179- // Generate private key
180- privKey , err := c .buildPrivateKey (privateKeyBytes )
181- if err != nil {
182- return nil , err
183- }
184-
185- // Generate public key string
186- publicKeyHex := common .Bytes2Hex (crypto .CompressPubkey (& privKey .PublicKey ))
187-
188- // Create login parameter with proxy settings
189- loginParam := & types.LoginParameter {
190- Message : types.Message {
191- Challenge : challenge ,
192- ProverName : "proxy" ,
193- ProverVersion : "proxy" ,
194- ProverProviderType : types .ProverProviderTypeProxy ,
195- ProverTypes : []types.ProverType {}, // Default empty
196- VKs : []string {}, // Default empty
197- },
198- PublicKey : publicKeyHex ,
199- }
200-
201- // Sign the message with the private key
202- if err := loginParam .SignWithKey (privKey ); err != nil {
203- return nil , fmt .Errorf ("failed to sign login parameter: %w" , err )
204- }
205-
206- return loginParam , nil
207- }
0 commit comments