@@ -30,11 +30,11 @@ import (
3030)
3131
3232const (
33- jsonrpcVersion = "2.0"
34- serviceMethodSeparator = "_"
35- subscribeMethod = "eth_subscribe "
36- unsubscribeMethod = "eth_unsubscribe "
37- notificationMethod = "eth_subscription "
33+ jsonrpcVersion = "2.0"
34+ serviceMethodSeparator = "_"
35+ subscribeMethodSuffix = "_subscribe "
36+ unsubscribeMethodSuffix = "_unsubscribe "
37+ notificationMethodSuffix = "_subscription "
3838)
3939
4040type jsonRequest struct {
@@ -164,7 +164,7 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) {
164164 }
165165
166166 // subscribe are special, they will always use `subscribeMethod` as first param in the payload
167- if in .Method == subscribeMethod {
167+ if strings . HasSuffix ( in .Method , subscribeMethodSuffix ) {
168168 reqs := []rpcRequest {{id : & in .Id , isPubSub : true }}
169169 if len (in .Payload ) > 0 {
170170 // first param must be subscription name
@@ -174,17 +174,16 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) {
174174 return nil , false , & invalidRequestError {"Unable to parse subscription request" }
175175 }
176176
177- // all subscriptions are made on the eth service
178- reqs [0 ].service , reqs [0 ].method = "eth" , subscribeMethod [0 ]
177+ reqs [0 ].service , reqs [0 ].method = strings .TrimSuffix (in .Method , subscribeMethodSuffix ), subscribeMethod [0 ]
179178 reqs [0 ].params = in .Payload
180179 return reqs , false , nil
181180 }
182181 return nil , false , & invalidRequestError {"Unable to parse subscription request" }
183182 }
184183
185- if in .Method == unsubscribeMethod {
184+ if strings . HasSuffix ( in .Method , unsubscribeMethodSuffix ) {
186185 return []rpcRequest {{id : & in .Id , isPubSub : true ,
187- method : unsubscribeMethod , params : in .Payload }}, false , nil
186+ method : in . Method , params : in .Payload }}, false , nil
188187 }
189188
190189 elems := strings .Split (in .Method , serviceMethodSeparator )
@@ -216,8 +215,8 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error)
216215
217216 id := & in [i ].Id
218217
219- // subscribe are special, they will always use `subscribeMethod ` as first param in the payload
220- if r .Method == subscribeMethod {
218+ // subscribe are special, they will always use `subscriptionMethod ` as first param in the payload
219+ if strings . HasSuffix ( r .Method , subscribeMethodSuffix ) {
221220 requests [i ] = rpcRequest {id : id , isPubSub : true }
222221 if len (r .Payload ) > 0 {
223222 // first param must be subscription name
@@ -227,17 +226,16 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error)
227226 return nil , false , & invalidRequestError {"Unable to parse subscription request" }
228227 }
229228
230- // all subscriptions are made on the eth service
231- requests [i ].service , requests [i ].method = "eth" , subscribeMethod [0 ]
229+ requests [i ].service , requests [i ].method = strings .TrimSuffix (r .Method , subscribeMethodSuffix ), subscribeMethod [0 ]
232230 requests [i ].params = r .Payload
233231 continue
234232 }
235233
236234 return nil , true , & invalidRequestError {"Unable to parse (un)subscribe request arguments" }
237235 }
238236
239- if r .Method == unsubscribeMethod {
240- requests [i ] = rpcRequest {id : id , isPubSub : true , method : unsubscribeMethod , params : r .Payload }
237+ if strings . HasSuffix ( r .Method , unsubscribeMethodSuffix ) {
238+ requests [i ] = rpcRequest {id : id , isPubSub : true , method : r . Method , params : r .Payload }
241239 continue
242240 }
243241
@@ -325,13 +323,13 @@ func (c *jsonCodec) CreateErrorResponseWithInfo(id interface{}, err Error, info
325323}
326324
327325// CreateNotification will create a JSON-RPC notification with the given subscription id and event as params.
328- func (c * jsonCodec ) CreateNotification (subid string , event interface {}) interface {} {
326+ func (c * jsonCodec ) CreateNotification (subid , namespace string , event interface {}) interface {} {
329327 if isHexNum (reflect .TypeOf (event )) {
330- return & jsonNotification {Version : jsonrpcVersion , Method : notificationMethod ,
328+ return & jsonNotification {Version : jsonrpcVersion , Method : namespace + notificationMethodSuffix ,
331329 Params : jsonSubscription {Subscription : subid , Result : fmt .Sprintf (`%#x` , event )}}
332330 }
333331
334- return & jsonNotification {Version : jsonrpcVersion , Method : notificationMethod ,
332+ return & jsonNotification {Version : jsonrpcVersion , Method : namespace + notificationMethodSuffix ,
335333 Params : jsonSubscription {Subscription : subid , Result : event }}
336334}
337335
0 commit comments