forked from Consensys/ethsigner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
404 additions
and
41 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...tegration-test/java/tech/pegasys/ethfirewall/jsonrpcproxy/EthAccountsIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.ethfirewall.jsonrpcproxy; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static java.util.Collections.singletonMap; | ||
|
||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcSuccessResponse; | ||
|
||
import java.util.Map; | ||
|
||
import io.netty.handler.codec.http.HttpHeaderValues; | ||
import io.vertx.core.json.Json; | ||
import org.junit.Test; | ||
import org.web3j.protocol.core.Request; | ||
import org.web3j.protocol.core.methods.response.EthAccounts; | ||
|
||
public class EthAccountsIntegrationTest extends IntegrationTestBase { | ||
|
||
@Test | ||
public void ethAccountsRequestFromWeb3jRespondsWithNodesAddress() { | ||
|
||
final Request<?, EthAccounts> requestBody = jsonRpc().ethAccounts(); | ||
final Map<String, String> expectedHeaders = | ||
singletonMap("Content", HttpHeaderValues.APPLICATION_JSON.toString()); | ||
|
||
final JsonRpcSuccessResponse responseBody = | ||
new JsonRpcSuccessResponse(requestBody.getId(), singletonList(unlockedAccount)); | ||
|
||
sendRequestThenVerifyResponse( | ||
request.ethFirewall(Json.encode(requestBody)), | ||
response.ethFirewall(expectedHeaders, Json.encode(responseBody))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
ethfirewall/src/main/java/tech/pegasys/ethfirewall/jsonrpcproxy/EthAccountsBodyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.ethfirewall.jsonrpcproxy; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import tech.pegasys.ethfirewall.jsonrpc.JsonRpcRequest; | ||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcError; | ||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcErrorResponse; | ||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcSuccessResponse; | ||
|
||
import java.util.Collection; | ||
|
||
import io.vertx.core.json.Json; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class EthAccountsBodyProvider implements BodyProvider { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(EthAccountsBodyProvider.class); | ||
|
||
private final String address; | ||
|
||
public EthAccountsBodyProvider(final String address) { | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public JsonRpcBody getBody(final JsonRpcRequest request) { | ||
final Object params = request.getParams(); | ||
|
||
if (isPopulated(params) && isNotEmptyArray(params)) { | ||
LOG.info("eth_accounts should have no parameters, but has {}", request.getParams()); | ||
return new JsonRpcBody( | ||
new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS)); | ||
} | ||
|
||
final JsonRpcSuccessResponse response = | ||
new JsonRpcSuccessResponse(request.getId(), singletonList(address)); | ||
return new JsonRpcBody(Json.encodeToBuffer(response)); | ||
} | ||
|
||
private boolean isPopulated(final Object params) { | ||
return params != null; | ||
} | ||
|
||
private boolean isNotEmptyArray(final Object params) { | ||
boolean arrayIsEmpty = false; | ||
boolean paramsIsArray = (params instanceof Collection); | ||
if (paramsIsArray) { | ||
Collection<?> collection = (Collection<?>) params; | ||
arrayIsEmpty = collection.isEmpty(); | ||
} | ||
|
||
return !(paramsIsArray && arrayIsEmpty); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
ethfirewall/src/main/java/tech/pegasys/ethfirewall/jsonrpcproxy/HttpResponseFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.ethfirewall.jsonrpcproxy; | ||
|
||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcResponse; | ||
|
||
import io.netty.handler.codec.http.HttpHeaderValues; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.json.Json; | ||
|
||
public class HttpResponseFactory { | ||
|
||
private static final String JSON = HttpHeaderValues.APPLICATION_JSON.toString(); | ||
|
||
public void create( | ||
final HttpServerRequest httpRequest, final int statusCode, final JsonRpcResponse body) { | ||
final HttpServerResponse response = httpRequest.response(); | ||
|
||
response.putHeader("Content", JSON); | ||
response.setStatusCode(statusCode); | ||
response.setChunked(false); | ||
response.end(Json.encodeToBuffer(body)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
ethfirewall/src/main/java/tech/pegasys/ethfirewall/jsonrpcproxy/InternalResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.ethfirewall.jsonrpcproxy; | ||
|
||
import tech.pegasys.ethfirewall.jsonrpc.JsonRpcRequest; | ||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcSuccessResponse; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.json.Json; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class InternalResponseHandler implements JsonRpcRequestHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(InternalResponseHandler.class); | ||
|
||
private final HttpResponseFactory responder; | ||
private final BodyProvider responseBodyProvider; | ||
private final JsonRpcErrorReporter errorReporter; | ||
|
||
public InternalResponseHandler( | ||
final HttpResponseFactory responder, | ||
final BodyProvider responseBodyProvider, | ||
final JsonRpcErrorReporter errorReporter) { | ||
this.responder = responder; | ||
this.responseBodyProvider = responseBodyProvider; | ||
this.errorReporter = errorReporter; | ||
} | ||
|
||
@Override | ||
public void handle(final HttpServerRequest httpServerRequest, final JsonRpcRequest rpcRequest) { | ||
LOG.debug("Internally responding to {}, id={}", rpcRequest.getMethod(), rpcRequest.getId()); | ||
final JsonRpcBody providedBody = responseBodyProvider.getBody(rpcRequest); | ||
|
||
if (providedBody.hasError()) { | ||
errorReporter.send(rpcRequest, httpServerRequest, providedBody.error()); | ||
} else { | ||
final JsonRpcSuccessResponse result = | ||
Json.decodeValue(providedBody.body(), JsonRpcSuccessResponse.class); | ||
responder.create(httpServerRequest, HttpResponseStatus.OK.code(), result); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
ethfirewall/src/main/java/tech/pegasys/ethfirewall/jsonrpcproxy/JsonRpcErrorReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.ethfirewall.jsonrpcproxy; | ||
|
||
import tech.pegasys.ethfirewall.jsonrpc.JsonRpcRequest; | ||
import tech.pegasys.ethfirewall.jsonrpc.response.JsonRpcErrorResponse; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.json.Json; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class JsonRpcErrorReporter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(JsonRpcErrorReporter.class); | ||
|
||
private final HttpResponseFactory responder; | ||
|
||
public JsonRpcErrorReporter(final HttpResponseFactory responder) { | ||
this.responder = responder; | ||
} | ||
|
||
void send( | ||
final JsonRpcRequest jsonRequest, | ||
final HttpServerRequest httpRequest, | ||
final JsonRpcErrorResponse error) { | ||
LOG.info("Dropping request from {}", httpRequest.remoteAddress()); | ||
LOG.debug( | ||
"Dropping request method: {}, uri: {}, body: {}, Error body: {}", | ||
httpRequest.method(), | ||
httpRequest.absoluteURI(), | ||
Json.encodePrettily(jsonRequest), | ||
Json.encode(error)); | ||
|
||
responder.create(httpRequest, HttpResponseStatus.BAD_REQUEST.code(), error); | ||
} | ||
} |
Oops, something went wrong.