From 2715155e668af17057a6dfc46d8adb26e4fcfbe3 Mon Sep 17 00:00:00 2001 From: Puneetha Date: Wed, 10 Jul 2019 17:16:17 +0100 Subject: [PATCH 1/3] Change eea_getPrivateTransaction endpoint to accept hex --- ...aGetPrivateTransactionIntegrationTest.java | 145 ++++++++++++++++++ .../privacy/EeaGetPrivateTransaction.java | 6 +- .../orion/testutil/OrionTestHarness.java | 4 + 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java new file mode 100644 index 0000000000..b17e18e96b --- /dev/null +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java @@ -0,0 +1,145 @@ +/* + * 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.pantheon.ethereum.jsonrpc.methods; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import tech.pegasys.orion.testutil.OrionTestHarness; +import tech.pegasys.orion.testutil.OrionTestHarnessFactory; +import tech.pegasys.pantheon.crypto.SECP256K1; +import tech.pegasys.pantheon.enclave.Enclave; +import tech.pegasys.pantheon.enclave.types.SendRequest; +import tech.pegasys.pantheon.enclave.types.SendRequestLegacy; +import tech.pegasys.pantheon.enclave.types.SendResponse; +import tech.pegasys.pantheon.ethereum.core.Address; +import tech.pegasys.pantheon.ethereum.core.PrivacyParameters; +import tech.pegasys.pantheon.ethereum.core.Wei; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.EeaGetPrivateTransaction; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; +import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction; +import tech.pegasys.pantheon.ethereum.privacy.Restriction; +import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; +import tech.pegasys.pantheon.util.bytes.BytesValue; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.Base64; + +import com.google.common.collect.Lists; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class EeaGetPrivateTransactionIntegrationTest { + + @ClassRule public static final TemporaryFolder folder = new TemporaryFolder(); + + private static Enclave enclave; + + private static OrionTestHarness testHarness; + + @BeforeClass + public static void setUpOnce() throws Exception { + folder.create(); + + testHarness = + OrionTestHarnessFactory.create( + folder.newFolder().toPath(), "orion_key_0.pub", "orion_key_0.key"); + + enclave = new Enclave(testHarness.clientUrl()); + } + + @AfterClass + public static void tearDownOnce() { + testHarness.stopOrion(); + } + + @Test + public void testUpCheck() throws IOException { + assertTrue(enclave.upCheck()); + } + + private final Address sender = + Address.fromHexString("0x0000000000000000000000000000000000000003"); + private static final SECP256K1.KeyPair KEY_PAIR = + SECP256K1.KeyPair.create( + SECP256K1.PrivateKey.create( + new BigInteger( + "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", 16))); + + private final PrivateTransaction privateTransaction = + PrivateTransaction.builder() + .nonce(0) + .gasPrice(Wei.of(1000)) + .gasLimit(3000000) + .to(null) + .value(Wei.ZERO) + .payload( + BytesValue.fromHexString( + "0x608060405234801561001057600080fd5b5060d08061001f60003960" + + "00f3fe60806040526004361060485763ffffffff7c01000000" + + "00000000000000000000000000000000000000000000000000" + + "60003504166360fe47b18114604d5780636d4ce63c14607557" + + "5b600080fd5b348015605857600080fd5b5060736004803603" + + "6020811015606d57600080fd5b50356099565b005b34801560" + + "8057600080fd5b506087609e565b6040805191825251908190" + + "0360200190f35b600055565b6000549056fea165627a7a7230" + + "5820cb1d0935d14b589300b12fcd0ab849a7e9019c81da24d6" + + "daa4f6b2f003d1b0180029")) + .sender(sender) + .chainId(BigInteger.valueOf(2018)) + .privateFrom( + BytesValue.wrap("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=".getBytes(UTF_8))) + .privateFor( + Lists.newArrayList( + BytesValue.wrap("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=".getBytes(UTF_8)))) + .restriction(Restriction.RESTRICTED) + .signAndBuild(KEY_PAIR); + + private final JsonRpcParameter parameters = new JsonRpcParameter(); + + private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class); + + @Test + public void returnsStoredPrivateTransaction() throws Exception { + final EeaGetPrivateTransaction eeaGetPrivateTransaction = + new EeaGetPrivateTransaction(enclave, parameters, privacyParameters); + + final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); + privateTransaction.writeTo(bvrlp); + + SendRequest sendRequest = + new SendRequestLegacy( + Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()), + "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=", + Lists.newArrayList("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")); + SendResponse sendResponse = enclave.send(sendRequest); + + String hexKey = BytesValue.wrap(Base64.getDecoder().decode(sendResponse.getKey())).toString(); + final Object[] params = new Object[] {hexKey}; + final JsonRpcRequest request = new JsonRpcRequest("1", "eea_getPrivateTransaction", params); + + final JsonRpcSuccessResponse response = + (JsonRpcSuccessResponse) eeaGetPrivateTransaction.response(request); + final PrivateTransaction result = (PrivateTransaction) response.getResult(); + + assertEquals(privateTransaction, result); + } +} diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java index c2bb1a9e49..794e474ff9 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java @@ -58,7 +58,11 @@ public JsonRpcResponse response(final JsonRpcRequest request) { final String enclaveKey = parameters.required(request.getParams(), 0, String.class); try { ReceiveResponse receiveResponse = - getReceiveResponseFromEnclave(enclaveKey, privacyParameters.getEnclavePublicKey()); + getReceiveResponseFromEnclave( + Base64.getEncoder() + .encodeToString(BytesValue.fromHexString(enclaveKey).extractArray()), + privacyParameters.getEnclavePublicKey()); + LOG.trace("Received transaction information from Enclave"); final BytesValueRLPInput bytesValueRLPInput = diff --git a/testutil/src/main/java/tech/pegasys/orion/testutil/OrionTestHarness.java b/testutil/src/main/java/tech/pegasys/orion/testutil/OrionTestHarness.java index 80f50c8665..571a6932d7 100644 --- a/testutil/src/main/java/tech/pegasys/orion/testutil/OrionTestHarness.java +++ b/testutil/src/main/java/tech/pegasys/orion/testutil/OrionTestHarness.java @@ -42,6 +42,10 @@ public Orion getOrion() { return orion; } + public void stopOrion() { + orion.stop(); + } + public Config getConfig() { return config; } From 546abccbb118afb0ca782bd70c8edbd87ef52b60 Mon Sep 17 00:00:00 2001 From: Puneetha Date: Mon, 15 Jul 2019 14:14:12 +0100 Subject: [PATCH 2/3] Update conversion to base64 using utils. --- .../internal/methods/privacy/EeaGetPrivateTransaction.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java index 794e474ff9..d7d70a2f96 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransaction.java @@ -26,6 +26,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction; import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPInput; +import tech.pegasys.pantheon.util.bytes.BytesValue; import tech.pegasys.pantheon.util.bytes.BytesValues; import org.apache.logging.log4j.Logger; @@ -59,8 +60,7 @@ public JsonRpcResponse response(final JsonRpcRequest request) { try { ReceiveResponse receiveResponse = getReceiveResponseFromEnclave( - Base64.getEncoder() - .encodeToString(BytesValue.fromHexString(enclaveKey).extractArray()), + BytesValues.asBase64String(BytesValue.fromHexString(enclaveKey)), privacyParameters.getEnclavePublicKey()); LOG.trace("Received transaction information from Enclave"); From 2763e347831b7375b824e2ea167b1ac96a4f0bfd Mon Sep 17 00:00:00 2001 From: Ivaylo Kirilov Date: Tue, 16 Jul 2019 11:05:45 +0100 Subject: [PATCH 3/3] Fix test. --- ...aGetPrivateTransactionIntegrationTest.java | 19 +++++++++++-------- .../privacy/EeaGetPrivateTransactionTest.java | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java index b17e18e96b..cd169cc122 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EeaGetPrivateTransactionIntegrationTest.java @@ -13,8 +13,7 @@ package tech.pegasys.pantheon.ethereum.jsonrpc.methods; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import tech.pegasys.orion.testutil.OrionTestHarness; @@ -31,10 +30,12 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.EeaGetPrivateTransaction; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult; import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction; import tech.pegasys.pantheon.ethereum.privacy.Restriction; import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; import tech.pegasys.pantheon.util.bytes.BytesValue; +import tech.pegasys.pantheon.util.bytes.BytesValues; import java.io.IOException; import java.math.BigInteger; @@ -73,7 +74,7 @@ public static void tearDownOnce() { @Test public void testUpCheck() throws IOException { - assertTrue(enclave.upCheck()); + assertThat(enclave.upCheck()).isTrue(); } private final Address sender = @@ -125,21 +126,23 @@ public void returnsStoredPrivateTransaction() throws Exception { final BytesValueRLPOutput bvrlp = new BytesValueRLPOutput(); privateTransaction.writeTo(bvrlp); - SendRequest sendRequest = + final SendRequest sendRequest = new SendRequestLegacy( Base64.getEncoder().encodeToString(bvrlp.encoded().extractArray()), "A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=", Lists.newArrayList("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")); - SendResponse sendResponse = enclave.send(sendRequest); + final SendResponse sendResponse = enclave.send(sendRequest); - String hexKey = BytesValue.wrap(Base64.getDecoder().decode(sendResponse.getKey())).toString(); + final String hexKey = BytesValues.fromBase64(sendResponse.getKey()).toString(); final Object[] params = new Object[] {hexKey}; final JsonRpcRequest request = new JsonRpcRequest("1", "eea_getPrivateTransaction", params); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) eeaGetPrivateTransaction.response(request); - final PrivateTransaction result = (PrivateTransaction) response.getResult(); + final PrivateTransactionLegacyResult result = + (PrivateTransactionLegacyResult) response.getResult(); - assertEquals(privateTransaction, result); + assertThat(new PrivateTransactionLegacyResult(privateTransaction)) + .isEqualToComparingFieldByField(result); } } diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransactionTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransactionTest.java index d823890797..bd8e90aced 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransactionTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetPrivateTransactionTest.java @@ -81,7 +81,8 @@ public class EeaGetPrivateTransactionTest { .privateFrom(BytesValues.fromBase64("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=")) .restriction(Restriction.RESTRICTED); - private final String enclaveKey = "93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng="; + private final String enclaveKey = + BytesValues.fromBase64("93Ky7lXwFkMc7+ckoFgUMku5bpr9tz4zhmWmk9RlNng=").toString(); private final JsonRpcParameter parameters = new JsonRpcParameter(); private final Enclave enclave = mock(Enclave.class);