Skip to content

Commit 45665df

Browse files
authored
Distribute raw tx (LFDT-web3j#1229)
* PrivateTransactionManager Fixed order of parameters to polling receipt transaction processor when passing custom values to the constructor. BesuPrivateTransactionManager Removed unnecessary passing of default polling attempts and frequency in the constructor. Sleep Duration was a magic number and not the symbolic constant used elsewhere. * Add support for priv_distributeRawTransaction API method (https://besu.hyperledger.org/en/stable/Reference/API-Methods/#priv_distributerawtransaction) PrivacyEnclaveKey.java -> new response class for enclave key * Add support for priv_distributeRawTransaction API method (https://besu.hyperledger.org/en/stable/Reference/API-Methods/#priv_distributerawtransaction) PrivacyTransactionManager.java Added new method signAndDistribute which takes a raw private transaction signs it and the distibutes to private transaction rceipis using the BEsu API method privDistributeRawTransaction Added new method sign to support signing but not distributing a raw provate transaction. Refactored the existing sendTransaction methods to reuse the sign method via signAndSend, changes are transparent to existing users. The sign method resolves a bug where if a chainId of ChainIDLong.NONE is used the eeaSendRawTransaction fails with a confusing error. * SpotlessApply * Fix imports * spotlessApply
1 parent 8e94d2b commit 45665df

File tree

7 files changed

+95
-20
lines changed

7 files changed

+95
-20
lines changed

besu/src/main/java/org/web3j/protocol/besu/Besu.java

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.web3j.protocol.besu.response.privacy.PrivGetPrivacyPrecompileAddress;
2929
import org.web3j.protocol.besu.response.privacy.PrivGetPrivateTransaction;
3030
import org.web3j.protocol.besu.response.privacy.PrivGetTransactionReceipt;
31+
import org.web3j.protocol.besu.response.privacy.PrivateEnclaveKey;
3132
import org.web3j.protocol.core.DefaultBlockParameter;
3233
import org.web3j.protocol.core.Request;
3334
import org.web3j.protocol.core.methods.response.EthAccounts;
@@ -105,6 +106,8 @@ Request<?, EthGetTransactionCount> privGetTransactionCount(
105106

106107
Request<?, PrivGetPrivateTransaction> privGetPrivateTransaction(final String transactionHash);
107108

109+
Request<?, PrivateEnclaveKey> privDistributeRawTransaction(final String signedTransactionData);
110+
108111
Request<?, PrivGetPrivacyPrecompileAddress> privGetPrivacyPrecompileAddress();
109112

110113
Request<?, PrivCreatePrivacyGroup> privCreatePrivacyGroup(

besu/src/main/java/org/web3j/protocol/besu/JsonRpc2_0Besu.java

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.web3j.protocol.besu.response.privacy.PrivGetPrivacyPrecompileAddress;
3838
import org.web3j.protocol.besu.response.privacy.PrivGetPrivateTransaction;
3939
import org.web3j.protocol.besu.response.privacy.PrivGetTransactionReceipt;
40+
import org.web3j.protocol.besu.response.privacy.PrivateEnclaveKey;
4041
import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt;
4142
import org.web3j.protocol.core.DefaultBlockParameter;
4243
import org.web3j.protocol.core.Request;
@@ -214,6 +215,16 @@ public Request<?, PrivGetPrivateTransaction> privGetPrivateTransaction(
214215
PrivGetPrivateTransaction.class);
215216
}
216217

218+
@Override
219+
public Request<?, PrivateEnclaveKey> privDistributeRawTransaction(
220+
final String signedTransactionData) {
221+
return new Request<>(
222+
"priv_distributeRawTransaction",
223+
Collections.singletonList(signedTransactionData),
224+
web3jService,
225+
PrivateEnclaveKey.class);
226+
}
227+
217228
@Override
218229
public Request<?, PrivGetPrivacyPrecompileAddress> privGetPrivacyPrecompileAddress() {
219230
return new Request<>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2020 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.protocol.besu.response.privacy;
14+
15+
import org.web3j.protocol.core.Response;
16+
17+
public class PrivateEnclaveKey extends Response<String> {
18+
public String getKey() {
19+
return getResult();
20+
}
21+
}

besu/src/main/java/org/web3j/tx/BesuPrivateTransactionManager.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,8 @@ public BesuPrivateTransactionManager(
4242
final long chainId,
4343
final Base64String privateFrom,
4444
final Base64String privacyGroupId) {
45-
this(
46-
besu,
47-
gasProvider,
48-
credentials,
49-
chainId,
50-
privateFrom,
51-
privacyGroupId,
52-
DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH,
53-
15 * 1000);
45+
super(besu, gasProvider, credentials, chainId, privateFrom);
46+
this.privacyGroupId = privacyGroupId;
5447
}
5548

5649
@Override

besu/src/main/java/org/web3j/tx/PrivateTransactionManager.java

+29-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.web3j.crypto.Credentials;
2323
import org.web3j.protocol.besu.Besu;
24+
import org.web3j.protocol.besu.response.privacy.PrivateEnclaveKey;
2425
import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt;
2526
import org.web3j.protocol.core.DefaultBlockParameter;
2627
import org.web3j.protocol.core.methods.response.EthGetCode;
@@ -80,7 +81,7 @@ protected PrivateTransactionManager(
8081
credentials,
8182
chainId,
8283
privateFrom,
83-
new PollingPrivateTransactionReceiptProcessor(besu, attempts, sleepDuration));
84+
new PollingPrivateTransactionReceiptProcessor(besu, sleepDuration, attempts));
8485
}
8586

8687
protected PrivateTransactionManager(
@@ -160,11 +161,7 @@ public EthSendTransaction sendTransaction(
160161
RESTRICTED);
161162
}
162163

163-
final String rawSignedTransaction =
164-
Numeric.toHexString(
165-
PrivateTransactionEncoder.signMessage(transaction, chainId, credentials));
166-
167-
return besu.eeaSendRawTransaction(rawSignedTransaction).send();
164+
return signAndSend(transaction);
168165
}
169166

170167
public EthSendTransaction sendTransactionEIP1559(
@@ -210,11 +207,7 @@ public EthSendTransaction sendTransactionEIP1559(
210207
RESTRICTED);
211208
}
212209

213-
final String rawSignedTransaction =
214-
Numeric.toHexString(
215-
PrivateTransactionEncoder.signMessage(transaction, chainId, credentials));
216-
217-
return besu.eeaSendRawTransaction(rawSignedTransaction).send();
210+
return signAndSend(transaction);
218211
}
219212

220213
@Override
@@ -264,4 +257,29 @@ public EthGetCode getCode(
264257
this.getPrivacyGroupId().toString(), contractAddress, defaultBlockParameter)
265258
.send();
266259
}
260+
261+
public String sign(RawPrivateTransaction rawTransaction) {
262+
263+
byte[] signedMessage;
264+
265+
if (chainId > ChainIdLong.NONE) {
266+
signedMessage =
267+
PrivateTransactionEncoder.signMessage(rawTransaction, chainId, credentials);
268+
} else {
269+
signedMessage = PrivateTransactionEncoder.signMessage(rawTransaction, credentials);
270+
}
271+
272+
return Numeric.toHexString(signedMessage);
273+
}
274+
275+
public EthSendTransaction signAndSend(RawPrivateTransaction rawTransaction) throws IOException {
276+
String hexValue = sign(rawTransaction);
277+
return this.besu.eeaSendRawTransaction(hexValue).send();
278+
}
279+
280+
public PrivateEnclaveKey signAndDistribute(RawPrivateTransaction rawTransaction)
281+
throws IOException {
282+
String hexValue = sign(rawTransaction);
283+
return this.besu.privDistributeRawTransaction(hexValue).send();
284+
}
267285
}

besu/src/test/java/org/web3j/protocol/besu/RequestTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ public void testPrivGetTransactionCount() throws Exception {
194194
+ "\"id\":1}");
195195
}
196196

197+
@Test
198+
public void testPrivDistributeRawTransaction() throws Exception {
199+
final String rawTx =
200+
"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f"
201+
+ "072445675058bb8eb970870f072445675";
202+
web3j.privDistributeRawTransaction(rawTx).send();
203+
204+
verifyResult(
205+
"{\"jsonrpc\":\"2.0\",\"method\":\"priv_distributeRawTransaction\","
206+
+ "\"params\":[\"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675\"],\"id\":1}");
207+
}
208+
197209
@Test
198210
public void testPrivGetPrivateTransaction() throws Exception {
199211
web3j.privGetPrivateTransaction("EnclaveKey").send();

besu/src/test/java/org/web3j/protocol/besu/ResponseTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.web3j.protocol.besu.response.privacy.PrivGetPrivateTransaction;
3030
import org.web3j.protocol.besu.response.privacy.PrivGetTransactionReceipt;
3131
import org.web3j.protocol.besu.response.privacy.PrivacyGroup;
32+
import org.web3j.protocol.besu.response.privacy.PrivateEnclaveKey;
3233
import org.web3j.protocol.besu.response.privacy.PrivateTransactionLegacy;
3334
import org.web3j.protocol.besu.response.privacy.PrivateTransactionReceipt;
3435
import org.web3j.protocol.besu.response.privacy.PrivateTransactionWithPrivacyGroup;
@@ -204,6 +205,22 @@ public void testPrivGetPrivateTransactionNull() {
204205
assertEquals(privPrivateTransaction.getPrivateTransaction(), (Optional.empty()));
205206
}
206207

208+
@Test
209+
public void testPrivDistributeRawTransaction() {
210+
211+
buildResponse(
212+
"{\n"
213+
+ " \"jsonrpc\": \"2.0\",\n"
214+
+ " \"id\": 1,\n"
215+
+ " \"result\": \"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331\"\n"
216+
+ "}");
217+
218+
PrivateEnclaveKey enclaveKey = deserialiseResponse(PrivateEnclaveKey.class);
219+
assertEquals(
220+
enclaveKey.getKey(),
221+
("0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"));
222+
}
223+
207224
@Test
208225
public void testPrivGetPrivacyPrecompileAddress() {
209226

0 commit comments

Comments
 (0)